简体   繁体   English

带有 Qt 的 C++ 编译失败

[英]C++ with Qt compile fail

I'm trying to compile the following short C++ program (not written by me) in Debian:我正在尝试在 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;
}

But I don't think I'm using the right includes, or maybe I need to install more Qt libraries?但我认为我没有使用正确的包含,或者我可能需要安装更多的 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>
                             ^

and alternatively, using includes and libraries from one directory back:或者,使用一个目录中的包含和库:

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.

I'm new to C++.我是 C++ 的新手。 How can I compile this small program in a 1-liner?如何在 1-liner 中编译这个小程序?


As requested in the comments:根据评论中的要求:

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

thanks to Michael Popovich 's answer i tried the following and got further.感谢Michael Popovich的回答,我尝试了以下方法并取得了进一步进展。 but now i have new errors:但现在我有新的错误:

$ 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. "\
    ^

i have no idea if qt was built with -reduce-relocations i don't think i even installed it myself - it was probably pulled in as a dependency for another install.我不知道 qt 是否是用-reduce-relocations构建的,我什至不认为我自己安装了它 - 它可能是作为另一个安装的依赖项-reduce-relocations

anyway, trying with -fPIC and -fPIE :无论如何,尝试使用-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

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

Add result path to QtCore/ in your project or your system PATH 在您的项目或系统PATH中将结果路径添加到QtCore /

I'd recommend to build Qt with either CMake or QMake, here is the solution with QMake: 我建议使用CMake或QMake来构建Qt,这是QMake的解决方案:

checksig.pro : checksig.pro

QT += core

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = checksig
TEMPLATE = app

SOURCES += checksig.cpp

LIBS += -lssl -lcrypto

And compile like this: 像这样编译:

qmake checksig.pro
make

Will compile a correct binary. 将编译正确的二进制文件。


And if you really need some 1-liner: 如果您确实需要一些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

For people like me, that uses windows OS and they accidently came to this page and the above solutions did not solve their problem, can try the following solution:对于像我这样使用 windows 操作系统的人,他们不小心来到了这个页面,上面的解决方案没有解决他们的问题,可以尝试以下解决方案:
I downloaded the OpenSSL executable from the official website , and selected the file with the description that contanins the following line ( Recommended for software developers by the creators of OpenSSL ).我从官方网站下载了 OpenSSL 可执行文件,并选择了包含以下行描述的文件( OpenSSL 创建者推荐给软件开发人员)。
At the time of writing this solution, the file version is (Win64 OpenSSL v3.0.0), and it can be directly downloaded from the following link .在编写本解决方案时,文件版本为(Win64 OpenSSL v3.0.0),可直接从以下链接下载。

Here is an Image for more declaration:这是更多声明的图像:
在此处输入图片说明

After that, I did the following steps:之后,我做了以下步骤:

  1. Install the OpenSSL form the .exe file.从 .exe 文件安装 OpenSSL。
  2. Copy the bin folder directory path (in my case it was C:\\Program Files\\OpenSSL-Win64\\bin) and add it to the system enviroment variables.复制bin文件夹目录路径(在我的例子中是 C:\\Program Files\\OpenSSL-Win64\\bin)并将其添加到系统环境变量中。
  3. Copy the include folder directory path (in my case it was C:\\Program Files\\OpenSSL-Win64\\include) and add it to the system enviroment variables.复制包含文件夹目录路径(在我的例子中是 C:\\Program Files\\OpenSSL-Win64\\include)并将其添加到系统环境变量中。

Then compile the source code that used the openssl library and header files, they should be compiled with no errors.然后编译使用 openssl 库和头文件的源代码,它们应该被编译没有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM