Undefined reference to CryptoPP::AlignedAllocate(unsigned int)
Asked Answered
K

3

9

I am using crypto++ in c++ linux. Here is my simple code:

#include <iostream>
#include <fstream>
#include <string.h>

#include "crypto++/cryptlib.h"
#include "crypto++/modes.h"
#include "crypto++/filters.h"
#include "crypto++/aes.h"
#include "crypto++/osrng.h"
#include "crypto++/strciphr.h"

using namespace std;
using namespace CryptoPP;

ifstream::pos_type size;
char * memblock;
int length;
char * _iv[AES::BLOCKSIZE];
char * keys[AES::MAX_KEYLENGTH];


void encriptCTR(byte * outbyte, const byte * inbyte, const byte * key, const byte * iv);

void encriptCTR(byte * outbyte, const byte * inbyte, const byte * key, const byte * iv)
{
    size_t inbyte_len = strlen((const char *)inbyte);
    CTR_Mode<AES>::Encryption ctr_encription(key, strlen((const char*)key), iv);
    ctr_encription.ProcessData(outbyte, inbyte, inbyte_len);
}

int main()
{
    ifstream file;
    file.open("testaja", ios::binary);
    if (file.is_open())
    {
        file.seekg (0, ios::end);
        length = file.tellg();
        memblock = new char [length];
        file.seekg (0, ios::beg);
        file.read (memblock, length);


        if (!file)
        {
            int a;
            a = (int)file.gcount();
            file.clear();
        }
        else
        {
            file.close();

            for (int i = 0; i < length; ++i)
            {
                cout << hex << (int)memblock[i] << " ";
            }

        }
    }
}

When I run it , some error occured:

 undefined reference to `CryptoPP::AlignedAllocate(unsigned int)'
 undefined reference to `CryptoPP::UnalignedAllocate(unsigned int)'
 undefined reference to `CryptoPP::AlignedDeallocate(unsigned int)'
 undefined reference to `CryptoPP::UnalignedDeallocate(unsigned int)'

Then, I used command

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

but this error still there :

undefined reference to `CryptoPP::AlignedAllocate(unsigned int)'
undefined reference to `CryptoPP::UnalignedAllocate(unsigned int)'
undefined reference to `CryptoPP::AlignedDeallocate(unsigned int)'
undefined reference to `CryptoPP::UnalignedDeallocate(unsigned int)'

How can I fix this error? Is there something wrong with my code?

I am installing crypto++ using synaptic package manager for this package:

libcrypto++-utils
libcrypto++8
libcrypto++8-dbg
libcrypto++-dev
libcrypto++-doc

and libcrypto++.a and libcrypto++.so can be found in /usr/lib/

Thanks in advance.

Kilar answered 21/7, 2012 at 9:28 Comment(5)
I tried use g++ to compile, but those error still there. what C++ code should i link? thanks.Kilar
i believe AlignedAllocate(unsigned int) used in crypto++/secblock.h that include crypto++/misc.h where AlignedAllocate(unsigned int) is declared, but somehow AlignedAllocate(unsigned int) implementation not found, and this error occured. what should i do?Kilar
i've tried to include crypto++/misc.h in my program, but those error still ocuured.Kilar
This implies a problem with how the libs are installed, could you update the question with the output of gcc -o test test.cpp -lcrypto++ -Wl,-v (I took -L/usr/lib/crypto++ out of that command because if the libs are installed in /usr/lib then telling the linker to look in the non-existent dir /usr/lib/crypto++ is a waste of time)Koestler
@jonathan: wow! it works! i change -L/usr/lib/crypto++ to -L/usr/lib/ and it works! you're right, i think the compiler look for the non-existent -L/usr/lib/crypto++ dir, after change it to -L/usr/lib/, the compiler look for the right dir thanks :)Kilar
K
7

This command looks wrong:

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

If (as you say) the libs are in /usr/lib then you shouldn't be saying -L/usr/lib/crypto++

I think the libcrypto++8 package installs its libs in the -L/usr/lib/crypto++ directory, and presumably they are incompatible and don't provide the undefined symbols your program needs.

You should compile with simply:

gcc -o test test.cpp -lcrypto++

(There's no need to say -L/usr/lib as it's the default location for libraries anyway)

Koestler answered 22/7, 2012 at 2:17 Comment(0)
K
7

it solved! i change my command from:

g++ -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

to this command:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp -lpthread

i add -lpthread because after i used this command:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp

i get these errors:

./libcryptopp.so: undefined reference to `pthread_getspecific'
./libcryptopp.so: undefined reference to `pthread_key_delete'
./libcryptopp.so: undefined reference to `pthread_key_create'
./libcryptopp.so: undefined reference to `pthread_setspecific'

i misunderstood about -L/usr/lib/crypto++ arg, i thought compiler will search for crypto++ in /usr/lib/ dir, it turned out the compiler will search for crypto++ in -L/usr/lib/crypto++ dir, whereas the package installed in -L/usr/lib/ dir.

thanks to @jonathan wakely.

Kilar answered 22/7, 2012 at 2:27 Comment(0)
P
0

i have this problem too. your compiler need to bind library files to your program , so because it cannot find any implementation of your decleration!

i still not solve my problem. but you have a way other!!! you can instead .cpp original files with library files.

you can download Cryptopp originally from below link :

https://www.cryptopp.com/cryptopp563.zip

Predestinate answered 17/4, 2016 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.