0

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

milaniez
  • 1,051
  • 1
  • 9
  • 21

1 Answers1

1

-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)

Henno Brandsma
  • 2,116
  • 11
  • 12
  • Thanks! Do you know why -lcrypto should be at the end? – milaniez May 25 '18 at 01:52
  • 1
    @milaniez Historically linkers have been prone to toss out symbols per module when they're not referenced by previous or current object code in the link process, but resolve externs in later linked object code in referenced but not yet resolved. In other words, `main.o` needs the `SHA1` entrypoints, but they're not yet resolved, so look for them in the upcoming object code. Thankfully, most linkers no longer pursue this insanity and actually resolve fore and aft. – WhozCraig May 25 '18 at 04:11