【问题标题】:Resolving "undefined reference" compilation errors of Boost C++ code in Ubuntu [duplicate]解决 Ubuntu 中 Boost C++ 代码的“未定义引用”编译错误 [重复]
【发布时间】:2013-01-21 16:54:00
【问题描述】:

来自我的终端会话:

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++ 文件系统库的 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++ 文件系统库?我是否也正确编译了代码?

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

非常感谢您,祝您有美好的一天!咻!

【问题讨论】:

  • Simonc,我使用“-lboost_filesystem -lboost_system”序列来链接上述 Boost C++ 库。但是,我仍然有链接错误。
  • Go Bears >make all g++ -L/usr/lib -lboost_filesystem -lboost_system get_sys_info.cpp /tmp/cchfM9Y3.o:在函数__static_initialization_and_destruction_0(int, int)': get_sys_info.cpp:(.text+0x13f): undefined reference to boost::system::generic_category()'get_sys_info .cpp:(.text+0x14b): 未定义引用boost::system::generic_category()' get_sys_info.cpp:(.text+0x157): undefined reference to boost::system::system_category()'
  • /tmp/cchfM9Y3.o: 在函数boost::filesystem3::path::codecvt()': get_sys_info.cpp:(.text._ZN5boost11filesystem34path7codecvtEv[boost::filesystem3::path::codecvt()]+0x5): undefined reference to boost::filesystem3::path::wchar_t_codecvt_facet()' collect2: ld 返回 1 退出状态 make: *** [all] 错误 1去熊>
  • 我是否未能在我的 C++ 代码中包含适当的头文件,或者我是否未能包含另一个 Boost C++ 库?我怎么知道出了什么问题,我哪里出了问题,并弄清楚我该如何解决这个问题?你能和我分享你的智慧和见解吗?非常感谢,祝您有美好的一天!
  • 我,C++ dingbat,终于设法弄明白了。我不得不使用:g++ get_sys_info.cpp -L/usr/lib -lboost_filesystem -lboost_system 非常感谢大家对我的帮助......我很感激。

标签: c++ ubuntu boost ubuntu-12.04


【解决方案1】:

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

我假设您使用的是学校计算机,因此编辑 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 中的所有内容并更新您的运行时路径。

祝你好运!

【讨论】:

  • 从终端:Go Trojans &gt;exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/include/boostGo Trojans &gt;echo $LD_LIBRARY_PATH:/usr/include/boostGo Trojans &gt;make allGo Trojans &gt;make allexport LD_LIBRARY_PATH=D_LIBRARY_PATH:/usr/include/boostg++ -L/usr/include/boost -lboost_system get_sys_info.cpp/usr/bin/ld: cannot find -lboost_system/usr/bin/ld: cannot find -lboost_systemcollect2: ld returned 1 exit statuscollect2: ld returned 1 exit status98765414339@9876541439@98765441439@98765441439@98765414 shell环境下的LD_LIBRARY_PATH,没用。
  • 来自我的 makefile:all: ` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/include/boost` ` g++ -L/usr/include/boost -lboost_system get_sys_info.cpp` ` ./a.出去`
  • 你的图书馆在/usr/include/boost吗?通常,/usr/include/boost 中只包含标头,而您想要的库可能位于 /usr/lib/boost 之类的某个地方。您需要告诉编译器在哪里可以找到 libraries
  • 来自我的终端-rw-r--r-- 1 root root 122280 Jan 2 2012 libboost_filesystem.so.1.46.1-rw-r--r-- 1 root root 324192 Jan 2 2012 libboost_graph.so.1.46.1-rw-r--r-- 1 root root 101592 Jan 2 2012 libboost_iostreams.so.1.46.1-rw-r--r-- 1 root root 414280 Jan 2 2012 libboost_program_options.so.1.46.1-rw-r--r-- 1 root root 311168 Jan 2 2012 libboost_python-py27.so.1.46.1-rw-r--r-- 1 root root 307072 Jan 2 2012 libboost_python-py32.so.1.46.1-rw-r--r-- 1 root root 1054456 Jan 2 2012 libboost_regex.so.1.46.1
  • -rw-r--r-- 1 root root 446584 Jan 2 2012 libboost_serialization.so.1.46.1-rw-r--r-- 1 root root 80600 Jan 2 2012 libboost_signals.so.1.46.1-rw-r--r-- 1 root root 14568 Jan 2 2012 libboost_system.so.1.46.1-rw-r--r-- 1 root root 101304 Jan 2 2012 libboost_thread.so.1.46.1-rw-r--r-- 1 root root 323128 Jan 2 2012 libboost_wserialization.so.1.46.1Go Trojans &gt;pwd/usr/libGo Trojans &gt;还是有编译错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2017-09-05
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
相关资源
最近更新 更多