解决Ubuntu中Boost C ++代码的“未定义引用”编译错误

时间:2013-02-06 13:12:46

标签: c++ ubuntu boost ubuntu-12.04

从我的终端会议:

Go Trojans >make all
g++ -static -I/usr/include/boost -I/usr/include/boost/filesystem get_sys_info.cpp
/tmp/cc6nK9EV.o: In function `__static_initialization_and_destruction_0(int, int)':
get_sys_info.cpp:(.text+0x13a): undefined reference to `boost::system::generic_category()'
get_sys_info.cpp:(.text+0x146): undefined reference to `boost::system::generic_category()'
get_sys_info.cpp:(.text+0x152): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Go Trojans >

导入Boost C ++ Filesystem库的C ++代码:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

// Include Boost C++ libraries
#include <boost/filesystem.hpp>
using namespace boost::filesystem;


using namespace std;

int main() {
    string my_str = "This is a string.";

    cout << my_str << endl;
/*
    my_str = system("pwd");
    my_str.append("\b\b\b\b\b\b\b\b extra");
    cout << my_str << "a\b\b\b\b\b\b=" << endl;
*/

    path p(".");
    cout << p << "==" << endl;



    return 0;
}

来自我的Boost C ++库所在目录的终端会话的片段。

Go Trojans >pwd
/usr/include/boost
Go Trojans >ls -al
total 1308
drwxr-xr-x  86 root root 12288 Jan 29 09:30 .
drwxr-xr-x 119 root root 20480 Feb  4 08:08 ..
...
drwxr-xr-x   5 root root  4096 Jan 29 09:30 filesystem
-rw-r--r--   1 root root  1340 Jan  5  2012 filesystem.hpp

如何解决未定义的引用?我正确导入Boost C ++文件系统库吗?我还正确编译代码吗?

我的错误是什么?能帮助我吗?

非常感谢,祝你有个美好的一天!侨!

1 个答案:

答案 0 :(得分:3)

您需要在-L/path/to/your/library调用前使用-lboost_system,这将告诉编译器在哪里可以找到共享对象。但是,即使你可以编译代码,它也不会运行,因为运行时无论如何都找不到文件,所以你不得不更新你的路径。

我假设您在学校计算机上,因此编辑LD_LIBRARY_PATH或使用/sbin/ldconfig是非法的。

如果他们不是非法的,你可以

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/boost

~/.bashrc或同等的

或者你可以

touch /etc/ld.so.conf.d/boost.conf

vi /etc/ld.so.conf/boost.conf

将路径放到此处的boost库中,然后保存文件。然后运行:

/sbin/ldconfig

/etc/ld.so.conf.d中的所有内容进行重新分析,并更新运行时路径。

祝你好运!