歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Boost 庫安裝、編譯問題筆記

Boost 庫安裝、編譯問題筆記

日期:2017/3/1 9:41:09   编辑:Linux編程

環境: Linux s12084 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:58:04 EST 2007 i686 i686 i386 GNU/Linux

gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-47.3)

boost 1.37.0

去年10月份用過一次再沒用過了。今天要用 regex 庫,生疏了。小記一下。以備以後參考。

boost 庫做得真好。在Windows 平台,Linux 平台下編譯都很順利。hp aCC 也宣稱對 boost 1.35 完全支持 。

全部編譯是很痛苦的過程。如果要使用哪個庫,只需要在 $(boost_root)/libs/下找到感興趣的庫,在 build 目錄中,選擇編譯器使用的 makefile,編譯即可。

例如,我這裡使用 boost 1.37 的 regex 庫。解壓 boost 後根目錄是 c:\boost_1_37_0。

然後到下面的目錄

c:\boost_1_37_0\libs\regex\build

看到一大堆的 .mak 文件。根據名字就可以看出來自己需要哪個 .mak 文件。這裡我用gcc編譯器,所以選擇 gcc.mak 。

編譯器可以根據 makefile 文件或參數生成 8 個庫。即靜態\動態、release\debug 、多線程\單線程 庫。從名字上看, debug 版本比其它版本的多一個 ”_d“, 多線程比其它版本的多一個"_mt"。regex 庫在 linux 平台下生成的庫文件名列表如下:

//動態庫的兩個版本

libboost_regex-gcc-1_37.so libboost_regex-gcc-d-1_37.so

//靜態庫的兩個版本

ibboost_regex-gcc-1_37.a libboost_regex-gcc-d-1_37.a

// 多線程動態的兩個版本

libboost_regex-gcc-mt -1_37.SO libboost_regex-gcc-mt-d-1_37.so

//多線程靜態的兩個版本

libboost_regex-gcc-mt -1_37.a libboost_regex-gcc-mt-d-1_37.a

一小會兒就編譯好了。編譯後生成的庫文件在。

使用時,需要在 makefile 中用 -I 選項添加 boost 根目錄的路徑。如果使用動態連接庫,還需要在 -L選項中添加對 .so 文件的引用。詳細的解釋援引下面的論述。

使用boost::regex的問題

單獨編譯了regex,生成了libboost_regex-gcc-1_34.a,現在試驗regex能否生效,代碼如下:

#include<boost/regex.hpp>

int main(int argc,char * argv[])
{
boost::regex e("test");
return 0;
}
#g++ regex.cpp -I /path/to/boostroot -L/path/to/libboost_regex-gcc-1_34.a -o regex
報錯:
/tmp/ccf4WLI8.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j+0x13): In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
collect2: ld returned 1 exit status

頭文件可以找到,但庫的連接好像有問題,然而庫的路徑應該是對的,請問這是怎麼回事?

boost文檔不是說的很清楚了嗎?(boost_1_34_1/more/getting_started/unix-variants.html 第6節)

A.
You can specify the full path to each library:
$ c++ -I path/to/boost_1_34_1 example.cpp -o example "
~/boost/lib/libboost_regex-gcc34-mt-d-1_34.a

B.
You can separately specify a directory to search (with -Ldirectory) and a library name to search for (with -llibrary,2 dropping the filename's leading lib and trailing suffix (.a in this case):

$ c++ -I path/to/boost_1_34_1 example.cpp -o example "
-L~/boost/lib/ -lboost_regex-gcc34-mt-d-1_34

我使用的是 .so 。所以我的 makefile是:

CXXFLAGS = -lgcc_s $(LIB) -lboost_regex-gcc-d-1_37
COMPILE.C = $(CC) -c $(INCLUDE)
MAKEEXE = $(CC) $(CXXFLAGS)
#可執行文件所依賴的.o 如果希望.o生成在依賴文件所在目錄下,可以使用路徑,如../pub/b.o
OBJ = regex_match_example.o
EXE = winner

all: $(EXE)
$(EXE): ${OBJ}
#'$^','$@' 叫作“自動變量”(Automatic Variables),會使用VPATH提供的信息來查找對應的文件
$(MAKEEXE) $^ -o $@
%.o: %.cpp
$(COMPILE.C) $^ -o $@
clean:
rm -f *.o $(EXE) core

我編譯成功後,將動態連接庫復制到當前目錄下,運行時提示找不到動態連接庫

提示找不到動態連接庫./winner: error while loading shared libraries: libboost_regex-gcc-d-1_37.so: cannot open shared object file: No such file or directory

看一下程序的引用和連接庫的版本信息,確實沒問題:

用 ldd 查看文件的動態連接庫信息,用 file 查看動態連接庫版本信息[fancp@s12084 test]$ ldd winner
/lib/libcwait.so (0x00de9000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00db1000)
libboost_regex-gcc-d-1_37.so => not found
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x00340000)
libm.so.6 => /lib/tls/libm.so.6 (0x00b51000)
libc.so.6 => /lib/tls/libc.so.6 (0x00a23000)
/lib/ld-linux.so.2 (0x00a04000)
[fancp@s12084 test]$ file libboost_regex-gcc-d-1_37.so
libboost_regex-gcc-d-1_37.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
[fancp@s12084 test]$

查看了一下,原來是環境變量中 LIB 中沒有添加當前目錄為搜索路徑。

修改 .bash_profile 文件,在 LIB 後面加一個冒號加一個點,保存,退出 shell ,再重新進入。OK!

Boost 的詳細介紹:請點這裡
Boost 的下載地址:請點這裡

Boost程序庫完全開發指南——深入C++“准”標准庫高清PDF版 http://www.linuxidc.com/Linux/2013-07/87574.htm

Ubuntu下編譯安裝boost庫 http://www.linuxidc.com/Linux/2013-07/87573.htm

Ubuntu下編譯boost 1.52b http://www.linuxidc.com/Linux/2013-02/79004.htm

Ubuntu編譯安裝boost並在eclipse C/C++中使用 http://www.linuxidc.com/Linux/2011-04/34790.htm

Copyright © Linux教程網 All Rights Reserved