【问题标题】:C++ with Qt compile fail带有 Qt 的 C++ 编译失败
【发布时间】:2015-05-23 07:25:37
【问题描述】:

我正在尝试在 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 中编译这个小程序?


根据 cmets 的要求:

$ 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

【问题讨论】:

  • 安装“apt-file”,然后使用它来定位包含“QtCore/qrefcount.h”的包。此外,请确保您了解在线上数百个带有类似错误消息的其他问题中的问题所在。进行网络搜索以找到这些。
  • -I /usr/include/i386-linux-gnu/qt5/QtCore/ 可能进入一个目录太远了。在我看来编译器正在寻找/usr/include/i386-linux-gnu/qt5/QtCore/QtCore/qrefcount.h
  • 我也这么认为,但没有运气。我会将这种替代尝试添加到操作中
  • 如果同时指定有和没有 QtCore 会发生什么? qrefcount.h 相对于 qbytearray.h 在哪里?打赌它们在同一个文件夹中。
  • @user4581301 如果我删除-lQtCore,结果实际上是相同的。你是这个意思吗?

标签: c++ qt compiler-errors


【解决方案1】:

我建议使用 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

【讨论】:

  • 优秀。编译和工作:) 是否有可能按照我最初的尝试在 1-liner 中完成,还是这是唯一的方法?
  • 我不知道你为什么需要 1-liner,但我已经更新了它。但是,这不是可移植且不可扩展的解决方案。
  • 1-liner 不起作用 - 它与 op 具有相同的错误。但是使用.pro文件没有问题
  • @mulllhausen 正如我所说,它对我有用,对你不起作用:那么只需使用便携式 QMake 解决方案。
【解决方案2】:

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

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

【讨论】:

  • 没有项目 - 只有一个 c++ 文件
  • -I /usr/include/i386-linux-gnu/qt5/ - 这是路径。检查它或添加新路径。
  • 我进一步使用了它。现在有新的错误。我会更新操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多