Boost 库安装、编译问题笔记


环境: 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版

Ubuntu下编译安装boost库

Ubuntu下编译boost 1.52b

Ubuntu编译安装boost并在eclipse C/C++中使用

本文永久更新链接地址:

相关内容