1

I'm trying to compile the following short C++ program (not written by me) in Debian:

$ 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?

$ 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++. How can I compile this small program in a 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. 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.

anyway, trying with -fPIC and -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
8
  • 1
    Install "apt-file" and then use it to locate a package containing "QtCore/qrefcount.h". Also, make sure you understand what the problem is in all those hundreds of other questions online with similar error messages. Do a websearch to locate those. May 23, 2015 at 7:36
  • -I /usr/include/i386-linux-gnu/qt5/QtCore/ might be going in one directory too far. It looks to me like the compiler is hunting for /usr/include/i386-linux-gnu/qt5/QtCore/QtCore/qrefcount.h May 23, 2015 at 7:42
  • i thought that too but no luck. i will add this alternative attempt to the op May 23, 2015 at 7:44
  • What happens if you specify both with and without QtCore? Where is qrefcount.h relative to qbytearray.h? Bet they are in the same folder. May 23, 2015 at 7:54
  • @user4581301 the results are the same actually if i remove -lQtCore. is that what you mean? May 23, 2015 at 7:58

2 Answers 2

1

I'd recommend to build Qt with either CMake or QMake, here is the solution with QMake:

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:

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
4
  • excellent. compiles and works :) is it possible to do it in a 1-liner as per my original attempt or is this the only way? May 23, 2015 at 8:54
  • I have no idea why you need 1-liner, but I've updated it. But, this is not portable and not extensible solution.
    – m0nhawk
    May 23, 2015 at 9:09
  • the 1-liner doesn't work - it has the same errors as the op. but its no problem to use the .pro file May 23, 2015 at 9:52
  • 1
    @mulllhausen As I said, it works for me and didn't work for you: then just use portable QMake solution.
    – m0nhawk
    May 23, 2015 at 9:55
1

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

Add result path to QtCore/ in your project or your system PATH

3
  • there is no project - just a single c++ file May 23, 2015 at 7:48
  • 1
    -I /usr/include/i386-linux-gnu/qt5/ - it's path. Check it or add new path. May 23, 2015 at 7:51
  • i got further using that. there are new errors now. i will update the op May 23, 2015 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.