2013-01-24 81 views
1

我正在嘗試使用靜態版本的測試庫來建立源代碼。我有libtest.a和libtest.so可用,所以我使用「-static」選項。但是,它看起來像gcc鏈接器也試圖搜索靜態版本的標準數學庫。 任何想法我可以使用什麼選項來鏈接標準庫的共享版本?如何強制ld使用靜態庫而不是共享庫?

g++ -static main.cpp -o a.out -L. -ltest 

錯誤:

/usr/bin/ld: cannot find -lm 
+0

你在當前目錄下有libtest.so? – billz

+0

是的,我確實在當前目錄中有這兩個庫 – Rajat

+0

你有靜態版本的數學庫,比如libm.a嗎? – jogojapan

回答

8

如果要強制鏈接使用特定庫的靜態版本可以使用:filename強制某個庫,而不是隻給連接一個'基地」庫名,讓它使用它找到的第一個:

g++ main.cpp -o a.out -l:./libtest.a 

http://sourceware.org/binutils/docs-2.23.1/ld/Options.html

-l namespec 
--library=namespec 

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename , ld will search the library path for a file called filename , otherwise it will search the library path for a file called libnamespec.a .

On systems which support shared libraries, ld may also search for files other than libnamespec.a . Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a . (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename , which always specifies a file called filename .

+1

謝謝邁克。 「-l:./ libtest.a」也行得通。 – Rajat

0

我從來沒有使用過邁克爾的建議,但我會把它拿走以備將來使用。

我使用充分控制庫鏈接的技術是通過完全指定欲使用的庫,以避免-Ll-Bstatic-Bdynamic完全。該命令將類似於:

g++ main.cpp -o a.out /usr/local/lib/test.a 

g++ main.cpp -o a.out /usr/local/lib/test.so 

g++ main.cpp -o a.out /usr/local/lib/test.so.1.0.0