Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile C++ and OpenSSL on Ubuntu 11.10

Tags:

c++

openssl

g++

I got a serious problem compiling my C++ and OpenSSL project on my Ubuntu 11.10. The compiling command is:

g++ -Wall -lssl -lm -lcrypto -I ./src ./src/server.cpp -o ./bin/server

I receive these errors:

server.cpp:(.text+0x8ff): undefined reference to `RSA_new'
server.cpp:(.text+0x92d): undefined reference to `PEM_read_RSAPrivateKey'
server.cpp:(.text+0xa85): undefined reference to `RSA_size'
server.cpp:(.text+0xaa1): undefined reference to `RSA_size'
server.cpp:(.text+0xae7): undefined reference to `RSA_private_decrypt'
server.cpp:(.text+0xd84): undefined reference to `BF_set_key'
server.cpp:(.text+0xf1d): undefined reference to `BF_ecb_encrypt'
server.cpp:(.text+0x13c6): undefined reference to `BF_ecb_encrypt'
collect2: ld returned 1 exit status
make: *** [server] Error 1

I successfully installed openssl and libssl-dev but the problem persists. I tried to compile the project on Linux Mint 12 with the kernel 3.0 and I had the same problem. On my old Linux OS with the kernel 2.6 the project compiled and worked fine (using the same Makefile and the same sources). Please help me!

like image 605
neoben Avatar asked Oct 25 '25 23:10

neoben


2 Answers

Generally you need to have the -l link flags after the code that references them. Try

g++ -Wall  -I ./src ./src/server.cpp -o ./bin/server -lssl -lm -lcrypto
like image 159
smparkes Avatar answered Oct 28 '25 14:10

smparkes


As the comment to this answer states, the linker only looks for undefined symbols to include in the order the parameters are listed.

That is, if your cpp file uses the libraries, the libraries have to be listed after the cpp file.

like image 33
Joachim Isaksson Avatar answered Oct 28 '25 14:10

Joachim Isaksson