Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

c - Compiling and linking OpenSSL on Ubuntu vs OSX

Attempt 1, Vanilla Link to Library

I'm trying to use a patched version of OpenSSL (so DTLS is easier to use). OpenSSL is in

/usr/local/openssl-1.0.1c

The ./include/openssl subfolder has a ton of header files (as I assume it should):

lrwxrwxrwx 1 root root   22 Dec 25 05:49 aes.h -> ../../crypto/aes/aes.h
lrwxrwxrwx 1 root root   24 Dec 25 05:49 asn1.h -> ../../crypto/asn1/asn1.h
lrwxrwxrwx 1 root root   28 Dec 25 05:49 asn1_mac.h -> ../../crypto/asn1/asn1_mac.h
...

GCC links to the include folder and gives me an error- it cannot find SSL. I'm using more or less the same thing other people are. This works, on OSX (10.6), but not on Ubuntu:

~$ gcc -L/usr/local/openssl-1.0.1c/include -lssl -lcrypto  -o server server.c
server.c:20:25: fatal error: openssl/ssl.h: No such file or directory
compilation terminated.

Attempt 2, Symbolic Link to Library in /usr/include

So, then I try creating a symbolic link to OpenSSL in my /usr/include:

sudo ln -s /usr/local/openssl-1.0.1c/include/openssl /usr/include/openssl

and re-attempting compilation:

~$ gcc -L/usr/local/openssl-1.0.1c/include -lssl -lcrypto  -o server server.c
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: ld returned 1 exit status
make: *** [server] Error 1

Why does the first method (ie no symbolic link) not work? Permissions? Why does the second method not work? Why does this work on OSX???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with the header file not being found seems to be you mixing up your options. -L adds a path to the linker library search paths, while -I adds a directory to the preprocessor header file search path. Change the -L to -I to solve that problem:

$ gcc -I/usr/local/openssl-1.0.1c/include server.c -o server.o

Now the linker problem is because you erroneously use the -L option to tell the linker to look for libraries in the include path. You need to change that path to the directory where the libraries are, usually a lib subdirectory. Also, the linker wants libraries in reverse order of their dependencies, so place the libraries you want to link with last on the command line:

$ gcc -I/usr/local/openssl-1.0.1c/include server.c -o server.o 
    -L/usr/local/openssl-1.0.1c/lib -lssl -lcrypto

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...