Andry
Andry

Reputation: 16845

C++ linker has problems finding openssl MD4 function

I have a program that I am writing and that needs to calculate some hashes. I need SHA, MD, HMAC algorithms. That is why I chose openssl as solution.

My code is the following:

#include <openssl/md4.h>

void calc();

void calc(unsigned char* data, unsigned long len) {
  unsigned char* h = new unsigned char[128];
  MD4(data, len, h);
}

Compiler returns me the following:

myfile.cpp:(.text+0x3e): undefined reference to `MD4' collect2: ld returned 1 exit status

I compile simply using:

g++ myfile.cpp -o myapp.o

under Linux Fedora.

I downloaded openssl libraries from here and compiled them cby using ./configure and then make install in the downloaded untarpalled directory. I also copied in /usr/local/include directory the include directory in the one I downloaded so that headers can be found by compiler because /usr/local/include is in my $PATH env var.

However the problem is that the linker cannot find the function. I understand that the reason might be two:

How should I proceeed? Thankyou

Edit1

I actually changed something in my openssl installation.

I installed openssl again and I could see that it places everything under /usr/local/ssl where I can find /usr/local/ssl/include and /usr/local/ssl/lib directories. I change my compilation string in:

g++ -I/usr/local/ssl/include -L/usr/local/ssl/lib -lssl -lcrypto

In the directories that I mentioned before I can find, respectively, /usr/local/ssl/include/openssl directory with all headers there and /usr/local/ssl/lib/libssl.a and /usr/local/ssl/lib/libcrypto.a libraries.

Before I did this change when I used the old compilation command, the compiler was telling me: Cannot find -lssl. With these changes, now it can find libs and headers, but ld always fails in the same way:

myfile.cpp:(.text+0x3e): undefined reference to `MD4' collect2: ld returned 1 exit status

A little disappointed. What do you think?

Upvotes: 1

Views: 1255

Answers (2)

Geoff Montee
Geoff Montee

Reputation: 2597

Linking against openssl usually requires -lssl.

g++ -o myapp myfile.cpp -lssl 

By the way, it sounds like you may have done the installation a little incorrectly.

You shouldn't have to copy header files anywhere. And you may not have copied the shared libraries anyway.

The compilation should go something like this:

./configure --prefix=/usr/local/openssl
make
make install

And then you compile your program like:

g++ -c -o myapp1.o myfile1.cpp -I/usr/local/openssl/include
g++ -c -o myapp2.o myfile2.cpp -I/usr/local/openssl/include
g++ -o myapp myapp1.o myapp2.o -I/usr/local/openssl/include -L/usr/local/openssl/lib -lssl -lcrypto

Upvotes: 2

Philipp
Philipp

Reputation: 535

The error is caused because you do not link the program to the openssl library during compilation.

Fix it with

g++ myfile.cpp -o myapp.o -lssl

See OpenSSL link options -lssl and -lcrypto in GCC

for how to link a program to openssl.

Upvotes: 1

Related Questions