我正在尝试设置一个patched version的OpenSSL来使用DTLS,而且我遇到了很多麻烦。我假设这是由于我对gcc和链接c库缺乏了解。特别是,我一直看到人们说要链接到lib /子文件夹,但我找不到OpenSSL。我也是question on building 32 bit OpenSSL,但我正试图做64位。
OSX
获取源代码和补丁:
wget ftp://ftp.openssl.org/source/openssl-1.0.1c.tar.gz # get latest stable OpenSSL
mv ~/Downloads/openssl-1.0.1c.tar.gz /usr/local/openssl-1.0.1c.tar.gz
cd /usr/local/openssl-1.0.1c.tar.gz
wget http://sctp.fh-muenster.de/dtls/dtls-bugs-1.0.1.patch # get the patch file
构建(64位,OpenSSL默认为32位):
export CFLAGS="-arch x86_64"
export LDFLAGS="-arch x86_64"
./Configure darwin64-x86_64-cc # 64 bit config command
make # .a files should be built, great
很好,我在OpenSSL根目录中有一些库:
/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r-- 1 nflacco staff 3286136 Jan 4 12:43 libcrypto.a
-rw-r--r-- 1 nflacco staff 260 Jan 4 12:43 libcrypto.pc
-rw-r--r-- 1 nflacco staff 570200 Jan 4 12:43 libssl.a
-rw-r--r-- 1 nflacco staff 275 Jan 4 12:43 libssl.pc
现在我将尝试编译a simple piece of code that uses the patched OpenSSL:
~$ gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
ld: warning: _OPENSSL_ia32cap_P has different visibility (hidden) in /usr/local/openssl-1.0.1c/libcrypto.a(x86_64cpuid.o) and (default) in /usr/local/openssl-1.0.1c/libcrypto.a(cryptlib.o)
Undefined symbols for architecture x86_64:
"_BIO_dgram_get_peer", referenced from:
_generate_cookie_callback in ccfldIrE.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [server] Error 1
Ubuntu的
大厦:
./config
make
检查库(忽略日期,Ubuntu认为这是圣诞节):
/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r-- 1 root root 3170340 Dec 25 17:45 libcrypto.a
-rw-r--r-- 1 root root 264 Dec 25 17:46 libcrypto.pc
-rw-r--r-- 1 root root 534092 Dec 25 17:45 libssl.a
-rw-r--r-- 1 root root 279 Dec 25 17:46 libssl.pc
然后,尝试编译:
gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
/tmp/cc0DgDl1.o: In function `generate_cookie_callback':
server.c:(.text+0x8b): undefined reference to `RAND_bytes'
server.c:(.text+0xba): undefined reference to `SSL_get_rbio'
server.c:(.text+0xdc): undefined reference to `BIO_ctrl'
server.c:(.text+0x112): undefined reference to `CRYPTO_malloc'
/tmp/cc0DgDl1.o: In function `main':
server.c:(.text+0x163): undefined reference to `SSL_library_init'
server.c:(.text+0x168): undefined reference to `SSL_load_error_strings'
server.c:(.text+0x16d): undefined reference to `SSL_library_init'
/tmp/cc0DgDl1.o: In function `configure_server_ssl':
server.c:(.text+0x2f5): undefined reference to `SSL_CTX_set_cipher_list'
server.c:(.text+0x318): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x333): undefined reference to `SSL_CTX_use_certificate_file'
server.c:(.text+0x35e): undefined reference to `SSL_CTX_use_PrivateKey_file'
server.c:(.text+0x379): undefined reference to `SSL_CTX_check_private_key'
server.c:(.text+0x3a4): undefined reference to `SSL_CTX_set_verify'
server.c:(.text+0x3c7): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x3da): undefined reference to `SSL_CTX_set_cookie_generate_cb'
server.c:(.text+0x3ed): undefined reference to `SSL_CTX_set_cookie_verify_cb'
/tmp/cc0DgDl1.o: In function `start_server':
server.c:(.text+0x40b): undefined reference to `DTLSv1_server_method'
server.c:(.text+0x413): undefined reference to `SSL_CTX_new'
collect2: ld returned 1 exit status
更新:
在Ubuntu上,我通过将库移动到编译命令的末尾来编译它并将标志-ldl
添加到编译时没有警告:
gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto -ldl
在OSX上,此命令给出了与之前相同的错误,但没有找到_BIO_dgram_get_peer
。
答案 0 :(得分:1)
您必须将库放在命令行的最后:
gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto
# ^^^^^^^^^^^^^^
这个网站上有这个问题的重复数字,解释了这个问题。 ld
的文档很好地解释了它,并且是最终的参考。