2009-08-20 107 views
5

我正在學習Boost,並且在製作文件時遇到問題。 這裏是我的基本的makefile:在make文件中包含boost庫

accesstimer: acctime.o btimer.o 
    g++ acctime.o btimer.o -o accesstimer 

acctime.o: acctime.cpp btimer.h 
    g++ -c acctime.cpp 

bentimer.o: btimer.cpp btimer.h 
    g++ -c btimer.cpp 

當acctime.cpp沒有提高文件系統元素,它這個男,AKE文件工作正常。 只要我添加boost文件系統元素,我顯然需要引用make文件中的boost libray,這是我遇到問題的地方。

下面一行適用於單個文件編譯:

g++ -I /usr/local/boost/boost_1_39_0 boosttest1.cpp -o bt1 /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a 

現在,我想這個融入make文件。我試過很多基於什麼樣的信息,我可以在網上找到,但沒有正在這是我最新的:

accesstimer: acctime.o bentimer.o 
    g++ acctime.o bentimer.o -o accesstimer 

acctime.o: acctime.cpp bentimer.h 
    g++ -c -I /usr/local/boost/boost_1_39_0 acctime.cpp /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a 

bentimer.o: bentimer.cpp bentimer.h 
    g++ -c bentimer.cpp 

不幸的是,stlill找不到Boost庫,任何人都可以幫忙嗎? 感謝

看了誰已經回答了人們現在我有了這個的建議:

accesstimer: acctime.o bentimer.o 
    g++ -L /usr/local/boost/boost_1_39_0 acctime.o /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a bentimer.o -o accesstimer 

acctime.o: acctime.cpp bentimer.h 
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h 
    g++ -c bentimer.cpp 

但是,這仍然無法鏈接。

這是錯誤消息我得到:(!感謝大加讚賞)

g++ -L /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer 
acctime.o: In function boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)': 
acctime.cpp:(.text._ZN5boost10filesystem6existsINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EEbE4typeERKS7_[boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x26): undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int&)' 
collect2: ld returned 1 exit status 
make: *** [accesstimer] Error 1 

繼orsogufo的意見,現在有這樣的:

accesstimer: acctime.o bentimer.o 
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer 

acctime.o: acctime.cpp bentimer.h 
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h 
    g++ -c bentimer.cpp 

迎向美好的,但還是不太找到庫:

g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer 
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt.a 
collect2: ld returned 1 exit status 
make: *** [accesstimer] Error 1 

我仔細檢查過該位置和庫是肯定的: /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a

仍然沒有喜悅,現在usimg這樣的:

accesstimer: acctime.o bentimer.o 
    g++ -L/usr/local/boost/boost_1_39_0 -lboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer  

acctime.o: acctime.cpp bentimer.h 
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h 
    g++ -c bentimer.cpp 

獲取:

g++ -L/usr/local/boost/boost_1_39_0/stage/lib/ -llibboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer 
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt 
collect2: ld returned 1 exit status 
make: *** [accesstimer] Error 1 

這是這個工作:

accesstimer: acctime.o bentimer.o 
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer  

acctime.o: acctime.cpp bentimer.h 
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h 
    g++ -c bentimer.cpp 

感謝您的幫助

+0

能否請您發表您收到錯誤消息? – 2009-08-20 12:16:27

+0

當你使用-l標誌鏈接庫時,你總是離開前面的「lib」。因此,鏈接libm.a是-lm,libboost是-lboost等。 – 2009-08-20 13:35:35

回答

9

EDITED
當您鏈接對象文件來創建可執行文件(你的第一個Makefile中的規則),你必須通過升壓庫的位置與-L標誌和庫與-l標誌名稱。

accesstimer: acctime.o bentimer.o 
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer 

其中/usr/local/boost/boost_1_39_0/stage/lib是包含庫和boost_filesystem庫的文件名,不開始lib(修改這兩個適當時)的目錄。

另一編輯 您試圖鏈接的.a文件是錯誤的...該庫應該沒有擴展名。

2

您需要將boost庫添加到鏈接階段(訪問者目標行),而不是編譯階段(僅需要包含路徑)。

0

嗨以下是爲cmake/make文件添加提升的完整步驟。這個答案是專門爲cpp的新手程序員開發的。

如果您想在Makefile的幫助下添加boost庫支持,您需要指定庫路徑(使用-L選項)和庫(使用-l選項)。

-L路徑/到/的/庫 -l llibrary

**現在怎麼找路徑庫**

以下是絕招:

  1. 開放式終端和消防命令

    $定位升壓> libboost.txt

    $ gedit中libboost.txt

  2. 此命令打開包含所有Boost庫路徑的文本文件。

  3. 現在找到(「CTR + F」)libboost,這凸顯升壓的庫文件擴展名爲u和。所以在這個文本文件。

  4. 複製此.so文件存在的路徑。

    例如:如果存在於/usr/lib/x86_64-linux-gnu/libboost_filesystem.so

    .so文件再指定路徑爲:-L/usr/lib中/ x86_64的-Linux的GNU/

現在如何找到相應的庫?

  1. 這取決於您使用

    例如什麼升壓功能/模塊:如果您正在使用升壓線程,你會需要以下庫

    libboost_filesystem.so

    libboost_thread。所以

    libboost_system.so

  2. 添加上述庫與-l選項如下:

    -l lboost_filesystem

    -l lboost_thread

    - lboost_system

希望這將幫助你,如果還存在更簡單的方法,請建議