1

我正在尝试编译以下实现上下文树切换的 c++ 代码(下载页面上的更多信息):

压缩档案,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 中(包含 lib 并包含子目录)。这是链接错误:

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 64 位上使用 Gcc 4.6.3。

4

2 回答 2

5

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

于 2012-11-06T23:01:24.993 回答
0

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

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

-lyourprojwantingv1 -llibraryofv1 -lyourprojwantingv2 -llibraryofv2

我个人认为这一切都只是疯狂!(所有的!)

于 2012-11-06T23:22:01.840 回答