Compile error with g++ (using DefaultEncryptor)

394 views
Skip to first unread message

saeed.gnu

unread,
Jul 27, 2009, 6:38:46 AM7/27/09
to Crypto++ Users
Hello
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);
encryptor.MessageEnd();

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
const*)':
crypt-test.cpp:(.text+0x138): undefined reference to
`CryptoPP::DefaultEncryptor::DefaultEncryptor(char const*,
CryptoPP::BufferedTransformation*)'
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)':
crypt-test.cpp:
(.text._ZN8CryptoPP8SecBlockIhNS_20AllocatorWithCleanupIhLb0EEEEC1Ej
[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):

/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libcrypto++.so:
undefined reference to `pthread_key_create'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libcrypto++.so:
undefined reference to `pthread_getspecific'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libcrypto++.so:
undefined reference to `pthread_key_delete'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libcrypto++.so:
undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status


Please help me. Thanks

Eugene Zolenko

unread,
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).

saeed

unread,
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
Forward
0 new messages