2010-12-04 59 views
12

我已經從源代碼(一個故意舊版本;用./config && make && make test構建)構建OpenSSL,並且寧願使用我已經構建而沒有執行make install來鏈接我的程序。將OpenSSL庫鏈接到程序

多數民衆贊成失敗的命令是:

gcc -Wall -Wextra -Werror -static -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto 
-Iopenssl/openssl-0.9.8k/include -o myApp source1.o source2.o common.o` 

而且我收到了一系列類似的錯誤:

common.c:(.text+0x1ea): undefined reference to `SSL_write' 

這讓我覺得有一些時髦與我的OpenSSL。如果我忽略-Lopenssl/openssl-0.9.8k/從我的命令,錯誤更改爲暫時無法:

/usr/bin/ld: cannot find -lssl 
/usr/bin/ld: cannot find -lcrypto 

我是不是編譯OpenSSL的錯誤?或者我該如何最好地解決這個問題?

回答

23

愚蠢的「Linux主義」罷工了!很顯然,我需要改變我的命令使得-L-l東西是像(儘管什麼man gcc似乎表明):

gcc -Wall -Wextra -Werror -static -o myApp source1.o source2.o common.o -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto -Iopenssl/openssl-0.9.8k/include

+15

這是`-lssl -lcrypto`,需要在最後。當鏈接器掃描庫時,只鏈接已經存在未定義引用的函數。所以你需要把「消費」對象放在鏈接行的開始處,並在末尾放置「提供」對象。 – caf 2010-12-06 01:35:40

+1

我總是很奇怪接受我自己的答案。我現在會切換它。 – mrduclaw 2014-03-29 15:10:50

+0

它對llvm族有什麼不同? – saruftw 2017-06-13 19:05:26

4

爲什麼你不希望使用make install?它可以在你想要的目錄複製生成的二進制文件,如果你以前它傳遞給./configure --prefix $HOME/target_library_install_directory

如果你使用這種伎倆與每個庫構建和安裝,那麼你可以加入目標目錄到LIBRARY_PATH環境變量,避免使用 - L選項。

1

如果您使用Autotools,或者您正在構建一個像cURL這樣的Autools項目,那麼您應該可以使用pkg-config。這個想法是Autotools軟件包將讀取OpenSSL的軟件包配置,並且事情將「爲您工作」。

OpenSSL程序包配置庫名稱爲openssl

您可以在基於makefile的項目中使用它。

%.o: %.c 
     $(CC) -o [email protected] -c `pkg-config --cflags openssl` $^ 

target: foo.o bar.o baz.o 
     $(CC) -o [email protected] `pkg-config --libs openssl` $^ 

另見How to use pkg-config in MakeHow to use pkg-config to link a library statically