C++ 与 Qt 编译失败

C++ with Qt compile fail

我正在尝试在 Debian 中编译以下简短的 C++ 程序(不是我写的):

$ cat checksig.cpp
#include <QByteArray>
#include <QDebug>

#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/ecdsa.h>
#include <openssl/sha.h>

static EC_KEY* EC_KEY_pub_key ( const QByteArray& pub )
{
  static EC_KEY* eckey = EC_KEY_new_by_curve_name ( NID_secp256k1 );
  const quint8* ppub = (const quint8*)pub.constData ( );
  o2i_ECPublicKey ( &eckey, &ppub, pub.size ( ) );
  return eckey;
}
//--------------------------------------------------------------
int main(int argc, char *argv[])
{
  const QByteArray data ( QByteArray::fromHex ( argv [1] ) );
  const QByteArray sign ( QByteArray::fromHex ( argv [2] ) );
  const QByteArray pubk ( QByteArray::fromHex ( argv [3] ) );

  quint8 tmp [32];
  ::SHA256 ( (const quint8*)data.constData ( ), data.size ( ), tmp );
  quint8 digest [32];
  ::SHA256 ( tmp, 32, digest );
  qDebug ( ) << "data=" << QString ( data.toHex ( ) );
  qDebug ( ) << "sign=" << QString ( sign.toHex ( ) );
  qDebug ( ) << "pubk=" << QString ( pubk.toHex ( ) );

  qDebug ( ) << "digest=" << QString ( QByteArray ( (const char*)digest, 32 ).toHex ( ) );

  const bool v ( ::ECDSA_verify ( 0, digest, 32, (const quint8*)sign.constData ( ), sign.size ( ), EC_KEY_pub_key ( pubk ) ) );
  qDebug ( ) << "result=" << v;

  return 0;
}

但我认为我没有使用正确的包含,或者我可能需要安装更多的 Qt 库?

$ g++ -Wall checksig.cpp -o checksig -L /usr/include/i386-linux-gnu/qt5/QtCore/ -lQtCore -I /usr/include/i386-linux-gnu/qt5/QtCore/
In file included from /usr/include/i386-linux-gnu/qt5/QtCore/QByteArray:1:0,
from checksig.cpp:4:/usr/include/i386-linux-gnu/qt5/QtCore/qbytearray.h:45:30: fatal error: QtCore/qrefcount.h: No such file or directory
#include <QtCore/qrefcount.h>
                             ^

或者,使用来自一个目录的包含和库:

g++ -Wall checksig.cpp -o checksig -L /usr/include/i386-linux-gnu/qt5/ -lQtCore -I /usr/include/i386-linux-gnu/qt5/
checksig.cpp:4:22: fatal error: QByteArray: No such file or directory
#include <QByteArray>
                      ^
compilation terminated.

我是 C++ 新手。如何在 1-liner 中编译这个小程序?


根据评论中的要求:

$ sudo apt-file search "QtCore/qrefcount.h"
qtbase5-dev: /usr/include/i386-linux-gnu/qt5/QtCore/qrefcount.h

感谢 Michael Popovich 的回答,我尝试了以下方法并取得了进一步的进展。但现在我有新的错误:

$ g++ -Wall checksig.cpp -o checksig -L /usr/include/i386-linux-gnu/qt5/QtCore/ -lQtCore -I /usr/include/i386-linux-gnu/qt5/ -I /usr/include/i386-linux-gnu/qt5/QtCore/
In file included from /usr/include/i386-linux-gnu/qt5/QtCore/qatomic.h:42:0,
                 from /usr/include/i386-linux-gnu/qt5/QtCore/qrefcount.h:45,
                 from /usr/include/i386-linux-gnu/qt5/QtCore/qbytearray.h:45,
                 from /usr/include/i386-linux-gnu/qt5/QtCore/QByteArray:1,
                 from checksig.cpp:4:
/usr/include/i386-linux-gnu/qt5/QtCore/qglobal.h:1034:4: error: #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC or -fPIE."
 #  error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\
    ^

我不知道 qt 是否是用 -reduce-relocations 构建的我不认为我自己安装了它 - 它可能是作为另一个安装的依赖项引入的。

无论如何,尝试 -fPIC-fPIE:

$ g++ -Wall checksig.cpp -o checksig -L /usr/include/i386-linux-gnu/qt5/QtCore/ -lQtCore -I /usr/include/i386-linux-gnu/qt5/ -I /usr/include/i386-linux-gnu/qt5/QtCore/ -fPIC
/usr/bin/ld: cannot find -lQtCore
collect2: error: ld returned 1 exit status
$ g++ -Wall checksig.cpp -o checksig -L /usr/include/i386-linux-gnu/qt5/QtCore/ -lQtCore -I /usr/include/i386-linux-gnu/qt5/ -I /usr/include/i386-linux-gnu/qt5/QtCore/ -fPIE
/usr/bin/ld: cannot find -lQtCore
collect2: error: ld returned 1 exit status

检查#find /usr/include/i386-linux-gnu/qt5/ -name qrefcount.h

将结果路径添加到您的项目或系统 PATH 中的 QtCore/

我建议使用 CMake 或 QMake 构建 Qt,这是使用 QMake 的解决方案:

checksig.pro:

QT += core

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = checksig
TEMPLATE = app

SOURCES += checksig.cpp

LIBS += -lssl -lcrypto

然后像这样编译:

qmake checksig.pro
make

将编译正确的二进制文件。


如果你真的需要一些 1-liner:

g++ checksig.cpp -o checksig -fPIC -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -lssl -lcrypto -lQt5Core -lpthread