Compile error with g++ (using DefaultEncryptor)

Skip to first unread message


Jul 27, 2009, 6:38:46 AM7/27/09
to Crypto++ Users
I will a simple code to encrypt and decrypt a string. I wrote
following Code:

#include <iostream>
#include <stdlib.h>
//#include <cryptopp/cryptlib.h>
#include <cryptopp/default.h>
#include <cryptopp/hex.h>
//#include "cryptopp/modes.h"
//#include "cryptopp/aes.h"

using namespace CryptoPP;

using namespace std;

// encode a string using passPhrase and encode it in hex
// returns the ciphertext, which should be deleted by caller
char *EncryptString(const char *instr, const char *passPhrase) {
unsigned int len=strlen(instr);
char* outstr;

DefaultEncryptor encryptor(passPhrase, new HexEncoder);
//DefaultEncryptorWithMAC encryptor(passPhrase, new HexEncoder);
encryptor.Put((byte *)instr, len);

unsigned int outputLength = encryptor.MaxRetrievable();
outstr = new char[outputLength+1];
encryptor.Get((byte *)outstr, outputLength);
outstr[outputLength] = 0;
return outstr;

int main()
//cout << crypt("firstpas", "as"); << "\n";
cout << EncryptString("test string", "passwd") << "\n";
return 0;

And tried to compile it with g++ (without any command options, is
there needed any?)
I use GNU/Linux(Ubuntu 8.10).
I tested with g++ 4.1 and 4.2 and 4.3. but I get a long error output
that says undefined reference to `CryptoPP::....

A part of error:

/tmp/ccZbCSRe.o: In function `EncryptString(char const*, char
crypt-test.cpp:(.text+0x138): undefined reference to
`CryptoPP::DefaultEncryptor::DefaultEncryptor(char const*,
crypt-test.cpp:(.text+0x34d): undefined reference to
`CryptoPP::BufferedTransformation::MaxRetrievable() const'
crypt-test.cpp:(.text+0x37d): undefined reference to
`CryptoPP::BufferedTransformation::Get(unsigned char*, unsigned int)'
/tmp/ccZbCSRe.o: In function `CryptoPP::SecBlock<unsigned char,
CryptoPP::AllocatorWithCleanup<unsigned char, false> >::SecBlock
(unsigned int)':
[CryptoPP::SecBlock<unsigned char,
CryptoPP::AllocatorWithCleanup<unsigned char, false> >::SecBlock
(unsigned int)]+0x25): undefined reference to
`CryptoPP::AllocatorWithCleanup<unsigned char, false>::allocate
(unsigned int, void const*)'
/tmp/ccZbCSRe.o: In function `CryptoPP::SecBlock<unsigned char,
CryptoPP::AllocatorWithCleanup<unsigned char, false> >::~SecBlock()':

And when I give option "-lcryptopp" to g++, running command:
g++ crypt-test.cpp -lcryptopp
Then I get this error (newly after previous like long error):

undefined reference to `pthread_key_create'
undefined reference to `pthread_getspecific'
undefined reference to `pthread_key_delete'
undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status

Please help me. Thanks

Eugene Zolenko

Jul 28, 2009, 3:31:44 PM7/28/09
to Crypto++ Users
You'll need to link to cryptopp and to all its dependencies.

So make that
-lcryptopp -lpthread

and other libs it might need (just look up those undefined symbols and
find out which library are they from).


Jul 29, 2009, 7:44:58 AM7/29/09
to Eugene Zolenko, Crypto++ Users
Eugene, Thanks a lot! My problem solved :)
Reply all
Reply to author
0 new messages