Ubuntu上的Boost :: system链接错误

时间:2012-11-06 22:48:51

标签: c++ boost linker-errors

我正在尝试编译以下实现上下文树切换(More info on the download page)的c ++代码:

Zip archive, 0.2 MB

需要一些boost库。我从boost.org下载了最新版本,并按照网站上的说明构建了所有需要构建的库。我还修改了归档中包含的makefile来添加boost lib路径和boost_system,但是我仍然遇到错误。这是我正在使用的makefile:

PROGRAM = cts
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
CFLAGS = -Wall
LDFLAGS = -lboost_program_options -lboost_filesystem -lboost_system

$(PROGRAM): $(OBJECTS) Makefile
    g++ $(CFLAGS) -L/home/users/mnembrini/opt/boost/lib $(LDFLAGS) -o $(PROGRAM) $(OBJECTS)

# Include known dependecies from -MMD
#-include $(OBJECTS:.o=.d)

%.o: %.cpp
    g++ -MMD $(CFLAGS) -I/home/users/mnembrini/opt/boost/include -c $<

clean:
    rm -f $(OBJECTS) *.d

.PHONY: clean

其中boost位于〜/ opt / boost(constains lib和include subdir)。这是链接错误:

mnembrini@meem:~/src/cts-v1 $ make
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c ac.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c cts.cpp
cts.cpp: In member function ‘virtual void SwitchingTree::update(bit_t)’:
cts.cpp:402:12: warning: variable ‘snc’ set but not used [-Wunused-but-set-variable]
cts.cpp: In member function ‘virtual double SwitchingTree::prob(bit_t)’:
cts.cpp:432:12: warning: variable ‘snc’ set but not used [-Wunused-but-set-variable]
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c ctw.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c icsilog.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c main.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c PowFast.cpp
g++ -Wall -L/home/users/mnembrini/opt/boost/lib -lboost_program_options -lboost_filesystem -lboost_system -o cts ac.o cts.o ctw.o icsilog.o main.o PowFast.o
cts.o: In function `__static_initialization_and_destruction_0(int, int)':
cts.cpp:(.text+0x1743): undefined reference to `boost::system::generic_category()'
cts.cpp:(.text+0x174f): undefined reference to `boost::system::generic_category()'
cts.cpp:(.text+0x175b): undefined reference to `boost::system::system_category()'
ctw.o: In function `__static_initialization_and_destruction_0(int, int)':
ctw.cpp:(.text+0xfcf): undefined reference to `boost::system::generic_category()'
ctw.cpp:(.text+0xfdb): undefined reference to `boost::system::generic_category()'
ctw.cpp:(.text+0xfe7): undefined reference to `boost::system::system_category()'
main.o: In function `showHelp()':
main.cpp:(.text+0x1c): undefined reference to `boost::program_options::operator<<(std::basic_ostream<char, std::char_traits<char> >&, boost::program_options::options_description const&)'
main.o: In function `initOptions(int, char**, boost::program_options::variables_map&)':
main.cpp:(.text+0x10f): undefined reference to `boost::program_options::options_description::add_options()'
main.cpp:(.text+0x129): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
main.cpp:(.text+0x13e): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x153): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x166): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x1d6): undefined reference to `boost::program_options::store(boost::program_options::basic_parsed_options<char> const&, boost::program_options::variables_map&, bool)'
main.cpp:(.text+0x200): undefined reference to `boost::program_options::notify(boost::program_options::variables_map&)'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1f13): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x1f1f): undefined reference to `boost::system::generic_category()'
[snip (2-3 screens like above)]
collect2: ld returned 1 exit status
make: *** [cts] Error 1

我在Ubuntu 12.04 64bit上使用Gcc 4.6.3。

2 个答案:

答案 0 :(得分:5)

将所有库放在命令行中的所有目标文件之后。与其他一些操作系统不同,这里的顺序很重要。

答案 1 :(得分:0)

你需要(就像先前写的那样)确保你有正确的链接顺序。 基本上,一些实现关心链接对象的顺序以及库。

如果您链接的库尚未被之前的代码引用,则会被丢弃。我记得通过引用相同的符号但在第一个符号之后链接的对象中然后重新链接另一个库版本来提出一个解决方案来破解程序中不同(版本)的库:

-lyourprojwantingv1 -llibraryofv1 -lyourprojwantingv2 -llibraryofv2

我个人认为这只是疯狂! (全部!)