vivekv
vivekv

Reputation: 2298

Linking challenges with gcc and crypto libraries

I am attempting to compile the sample app with opneSSL and I cant get the linker to cooperate. Need a fresh pair of eyes to look at this probably simple problem. Please help.

I have tried reading through all the questions and I cannot find what is wrong. Here is the command line and linker output

$ gcc  -t -I/usr/include/openssl -L/usr/lib/x86_64-linux-gnu -o algotest -lcrypto -lssl3 -lssl algotest.c

/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o
-lcrypto (/usr/lib/x86_64-linux-gnu/libcrypto.so)
-lssl3 (/usr/lib/x86_64-linux-gnu/libssl3.so)
-lssl (/usr/lib/x86_64-linux-gnu/libssl.so)
/tmp/ccSLR6rb.o
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/lib/x86_64-linux-gnu/libc.so.6
(/usr/lib/x86_64-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o/tmp/ccSLR6rb.o: In function `main':
algotest.c:(.text+0x5c): undefined reference to `OpenSSL_add_all_digests'
algotest.c:(.text+0x99): undefined reference to `EVP_get_digestbyname'
algotest.c:(.text+0xdc): undefined reference to `EVP_MD_CTX_create'
algotest.c:(.text+0xfb): undefined reference to `EVP_DigestInit_ex'
algotest.c:(.text+0x13b): undefined reference to `EVP_DigestUpdate'
algotest.c:(.text+0x17b): undefined reference to `EVP_DigestUpdate'
algotest.c:(.text+0x192): undefined reference to `EVP_DigestFinal_ex'
algotest.c:(.text+0x19e): undefined reference to `EVP_MD_CTX_destroy'
/usr/bin/ld: link errors found, deleting executable `algotest'
collect2: ld returned 1 exit status

Here is the source code...

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

main(int argc, char *argv[])
{
EVP_MD_CTX *mdctx;
const EVP_MD *md;
char mess1[] = "Test Messagen";
char mess2[] = "Hello Worldn";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
printf("Usage: mdtest digestnamen");
exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
printf("Unknown message digest %sn", argv[1]);
exit(1);
}

mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("n");
}

Upvotes: 2

Views: 10299

Answers (2)

vivekv
vivekv

Reputation: 2298

I found the problem! The credit goes to @Marc for pointing it out. The C file should be before the -l files.

Upvotes: 3

jww
jww

Reputation: 102205

$ gcc -t -I/usr/include/openssl -L/usr/lib/x86_64-linux-gnu -o algotest -lcrypto -lssl3 -lssl algotest.c

Try:

$ gcc algotest.c -o algotest -lssl  -lcrypto

Since you are using #include <openssl/evp.h>, you can:

$ gcc algotest.c -o algotest -lssl  -lcrypto

Also, be sure you have the development version of OpenSSL installed. On Ubuntu the command is:

$ sudo apt-get install libssl-dev

On Fedora, I believe the command is:

$ sudo yum install openssl-devel

Upvotes: 7

Related Questions