cgccopenssllibssl

compiling with libssl does not work


I have a very simple code:

#include <openssl/sha.h>

int main() {
    SHA_CTX sha1;
    SHA_Init(&sha1);
}

I have installed both libssl-dev and libcrypto++-dev:

However I have a build failure using the following command:

$ gcc -lcrypto -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status
$
$ gcc -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status

Platform: Ubuntu 16.04


Solution

  • -lssl is not needed, -lcrypto is enough, and it must be at the end:

    gcc -o main main.c -lcrypto
    

    (or whatever you want your program to be called goes after -o)