对"boost::system::system_category()"的未定义引用

undefined reference to `boost::system::system_category()'

本文关键字:system 未定义 引用 boost category      更新时间:2023-10-16

虽然有很多关于这个问题的问题,但我找不到解决问题的正确方法。我在项目中使用了libboost_system,并在 makefile 中添加了Libs:=libconfig++ libboost_thread libboost_system,但它仍然不起作用。

我的机器环境:

- Ubuntu 14.04.5 LTS
- gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
- libboost_1.54

到目前为止,我已经尝试了以下方法

  1. 我写了一个测试文件看看boost库是否安装正确,测试文件是这样的: [测试.cpp][1] https://i.stack.imgur.com/Tb4I0.png,然后我用这个命令编译:g++ test.cpp -g -o main -lboost_system -lboost_thread,它确实以正确的方式工作。这是否意味着提升库安装正确?在我看来,我认为它已安装。
  2. 我搜索堆栈溢出,发现很多答案都提到库的顺序很重要,所以我检查了库的顺序并将libbost_system放在 Libs.It 的末尾,但仍然得到同样的错误。
  3. 然后我发现很少有答案推断#define BOOST_ERROR_CODE_HEADER_ONLY,我不知道如何添加这个定义,所以我最终未能解决问题。

我的项目的一部分makefile如下所示:

SOURCE_DIR := src
COMPILER := g++
BINARY_DIR := bin/lrelease
CPPFLAGS := -Wall -g -O3 -std=c++11
LIBS := -lpthread -lboost_thread -lconfig++ -lboost_system
COMPILE.cpp = $(COMPILER) $(CFLAGS) $(CPPFLAGS) -c
LINK.cpp = $(COMPILER) $(LIBS)
%.o: %.cpp
#$(call make-depend,$<,$@,$(subst .o,.d,$@))
$(COMPILE.cpp) $< -o $@
# $(call source-dir-to-binary-dir, directory-list)
source-dir-to-binary-dir = $(addprefix $(BINARY_DIR)/,$1)
# $(call source-to-object, source-file-list)
source-to-object = $(call source-dir-to-binary-dir, $(subst .cpp,.o,$1))
# $(subdirectory)
subdirectory = $(patsubst %/module.mk,%,                
$(word                                 
$(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
define make-depend
g++ -MM    
-MF$3         
-MP            
-MT$2         
$(CFLAGS)      
$(CPPFLAGS)    
$(TARGET_ARCH) 
$1
endef
# $(call make-library, library-name, source-file-list)
define make-library
libraries += $(BINARY_DIR)/$1
sources   += $2
$(BINARY_DIR)/$1: $(call source-dir-to-binary-dir, $(subst .cpp,.o,$2))
$(AR) $(ARFLAGS) $$@ $$^
endef
# $(call make-program, program-name, library-list, source-file-list)
define make-program
programs  += $(BINARY_DIR)/$1
sources   += $3
$(BINARY_DIR)/$1: $(call source-dir-to-binary-dir, $(subst .cpp,.o,$3) $2 )
$(LINK.cpp) -o $$@ $$^ -lpthread
endef
# $(compile-rules)
define compile-rules
$(foreach f,$(local_src),$(call one-compile-rule,$(call source-to-object,$f),$f))
endef
# $(call one-compile-rule, binary-file, source-file)
define one-compile-rule
$1: $2
$(call make-depend,$2,$1,$(subst .o,.d,$1))
$(COMPILE.cpp) -o $1 $2
endef
# then the output dirs and make clean
#
#

我得到的错误如下:

g++ -lpthread -lboost_thread -lconfig++ -lboost_system -o bin/lrelease/ buildmyProjectFromN3 bin/lrelease/BuildmyProjectFromN3/BuildmyProject.o bin/lrelease/libmyProject.a -lpthread
bin/lrelease/libmyProject.a(myProjectBuilder.o):(.gcc_except_table+0x48c): undefined reference to `typeinfo for libconfig::FileIOException'
bin/lrelease/libmyProject.a(myProjectBuilder.o): In function `boost:: thread_exception::thread_exception(int, char const*)':
/usr/include/boost/thread/exceptions.hpp:51: undefined reference to `boost::system::system_category()'
bin/lrelease/libmyProject.a(myProjectBuilder.o): In function `boost:: condition_error::condition_error(int, char const*)':
/usr/include/boost/thread/exceptions.hpp:84: undefined reference to `boost: : system::system_category()'
bin/lrelease/libmyProject.a(myProjectBuilder.o): In function `interruption_checker':
/usr/include/boost/thread/pthread/thread_data.hpp:187: undefined reference to `boost::detail::get_current_thread_data()'
bin/lrelease/libmyProject.a(myProjectBuilder.o): In function `boost:: condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/include/boost/thread/pthread/condition_variable.hpp:84: undefined reference to `boost: : this_thread::interruption_point()'
bin/lrelease/libmyProject.a(myProjectBuilder.o): In function `boost:: shared_mutex::lock_shared()':
/usr/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
...
...
...

添加或不添加L/usr/lib#LIBS := -lpthread -lboost_system -lboost_thread -lconfig++ -L/usr/lib似乎没有区别。两者都无法正常工作。

我可以确定代码是正确的,并且可以在其他服务器上正确运行。所以,我真的不知道问题出在哪里。 如果我能获得有关该错误的任何有用信息,我将不胜感激,非常感谢:)

错误消息表明您的libmyProject.a库依赖于 Boost.Thread 和 Boost.System。但在链接器命令行中,它是在 Boost 库之后指定的,因此其 Boost 依赖项未解析。请注意,链接器命令行中库和对象文件的顺序非常重要。在您的情况下,您需要在命令行中将库和目标文件-lboost_thread-lboost_system