0

Nearly every function from Crypto++ produces undefined reference in Qt Creator, code::blocks however runs just great.

LIBS+= -lcryptopp  

in .pro file seems to work as I can include needed files and declare variables unless constructor is overloaded.

For example

CryptoPP::Integer integer;  
std::string str=CryptoPP::IntToString(integer, 10);  

Throws

.../main.cpp:54: undefined reference to `std::string CryptoPP::IntToString<CryptoPP::Integer>(CryptoPP::Integer, unsigned int)'  
collect2: error: ld returned 1 exit status  
make: *** [PDBM] Error 1  
07:10:21: The process "/usr/bin/make" exited with code 2.  

<a href="https://pastebin.com/c9nWekZR">cryptest.pro</a>  
<a href="https://pastebin.com/0ku8Dncw">Makefile</a>  
<a href="https://pastebin.com/ii1AM1Dx">full rebuild</a> //sorry, stackoverflow wants those links to be a code 

/usr/lib/ contains libcrypto++.a, libcrypto++.so, libcryptopp.so and link to libcrypto++.a named libcryptopp.a

New findings: I have tried to compile this library before and now my project folder has all the .h and .cpp files in cryptopp folder. To provide all the code without showing the code new project has been created and more errors appeared:

In file included from /usr/include/cryptopp/secblock.h:7:0,
                 from /usr/include/cryptopp/integer.h:7,
                 from ../cryptest/main.cpp:7:
/usr/include/cryptopp/misc.h: In instantiation of ‘std::string CryptoPP::IntToString(T, unsigned int) [with T = CryptoPP::Integer; std::string = std::basic_string<char>]’:
../cryptest/main.cpp:54:54:   required from here
/usr/include/cryptopp/misc.h:424:58: error: invalid cast from type ‘CryptoPP::Integer’ to type ‘char’
   result = char((digit < 10 ? '0' : ('a' - 10)) + digit) + result;
                                                          ^
Makefile:1113: recipe for target 'main.o' failed

Which means that cryptopp has been using those new .h files. So it is not only a problem with linker as I previously thought.

I'm on Ubuntu 16.04, using Qt 5.8 and Crypto++ from repository.

  • 1
    You could provide the error messages, also explain what relationship CodeBlocks has with Qt. – eyllanesc Feb 22 '18 at 21:58
  • 1
    Which are the undefined references? Add the verbatim linker output. – Murphy Feb 22 '18 at 22:03
  • Show your Qt project file and a sample of the error messages. Also see [Using Crypto++ static library in a QT project](https://stackoverflow.com/q/38893334/608639), [QT console application with Crypto++ library](https://stackoverflow.com/q/23702414/608639), [Crypto++ can't build Qt Application](https://stackoverflow.com/q/17055990/608639), [Crypto++ in Qt application and linker errors?](https://stackoverflow.com/q/16119090/608639), etc. – jww Feb 23 '18 at 01:49
  • Code::Blocks uses the same compiler(can be different version but I have tried on bith with the same results) and the same cryptopp. The main difference is that qt uses make while C::B doesn't. Running make from console gives the same results. – user9398723 Feb 23 '18 at 07:01
  • Please post your Qt makefile somewhere. We need to see the relevant parts. Please state where the library is located. Please show a typical compile command, and please show the link command. We need more than just the error. – jww Feb 23 '18 at 07:42
  • Edited question accordingly. I have tried to install from zip again (v6.0.0) and this issue appeared in Code::Block as well this time. so qtCreator is incompatible with my repo version and both IDEs don't like the zip. make static dynamic cryptest.exe went smoothly. – user9398723 Feb 23 '18 at 18:59
  • 1
    `std::string str=CryptoPP::IntToString(integer, 10);` is relatively new. It was added at Crypto++ 6.0. It sounds like the `libcryptopp.a` you are linking to in your Qt project is an old distro provided one; and not the new one you are building. This make sense because your compile paths include `-I/usr/local/include` (new headers), but you link path *does not* include `-L/usr/local/lib` (new libs). So add `-L/usr/local/lib` to your Qt linker flags. Or, instead of `-l:libcryptopp.a`, use the full `/usr/local/lib/libcryptopp.a`. – jww Feb 24 '18 at 05:13
  • One final thing you should state is, where you installed Crypto++. You might also consider removing the distro provided version of the library so you don't accidentally compile and link against it. – jww Feb 24 '18 at 05:29
  • It's not just about this function, every function I try throws undefined reference. Looks like patch distro chose is /usr/lib and for make I used PREFIX=/usr/local Ok, I removed distro's lib and now Code::Block doesn't complain, qt still does. – user9398723 Feb 24 '18 at 10:22
  • @user9398723 You need to find out exactly where Code::Blocks searches for libcryptopp. Then, tell your Qt project to look in the same folder using `LIBS += -L...`. You must also re-run qmake after you update your .pro file. – JKSH Mar 01 '18 at 03:28

1 Answers1

1
-lcryptopp

This option simply tells your linker that you want to link to a shared library called libcryptopp.so. However, it does not tell your linker where to find the shared library.

You must specify the folder which contains the developer's copy of the library, using the -L option. For example, if the library is in /usr/lib, then write this:

LIBS += -L/usr/lib/ -lcryptopp
JKSH
  • 2,658
  • 15
  • 33
  • 1
    Another option is `OBJECTS += /usr/lib/libcryptopp.a`. That will use static linking to the library, and possibly avoid the next set of questions related to path problems. Another way to use static linking and avoid path problems is `LIBS += -L/usr/lib/ -l:libcryptopp.a`. Also see the [`ld(1)` man page](https://linux.die.net/man/1/ld) – jww Feb 23 '18 at 07:47
  • I have already tried adding this patch to LIBS and libcryptopp.a to OBJECTS, nothing changes. I'll try second option when I get back home. – user9398723 Feb 23 '18 at 07:59
  • @user9398723 `/usr/lib/` was just an example. You need to find out the correct location of libcryptopp in your computer. Also, remember to re-run qmake after you edit your .pro file, so that you Makefile gets updated. – JKSH Mar 01 '18 at 03:30