【发布时间】: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 toboost::system::generic_category()'get_sys_info .cpp:(.text+0x14b): 未定义引用boost::system::generic_category()' get_sys_info.cpp:(.text+0x157): undefined reference toboost::system::system_category()' -
/tmp/cchfM9Y3.o: 在函数
boost::filesystem3::path::codecvt()': get_sys_info.cpp:(.text._ZN5boost11filesystem34path7codecvtEv[boost::filesystem3::path::codecvt()]+0x5): undefined reference toboost::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