【问题标题】:undefined reference to `boost::system::system_category()'对`boost::system::system_category()'的未定义引用
【发布时间】:2018-03-20 12:58: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库是否安装正确,测试文件长这样: [test.cpp][1] https://i.stack.imgur.com/Tb4I0.png,然后我用这个命令编译:g++ test.cpp -g -o main -lboost_system -lboost_thread,它确实以正确的方式工作。这是否意味着 boost 库安装正确?在我看来,我认为它已安装。
  2. 我搜索了stackoverflow,发现很多答案都提到库的顺序很重要,所以我检查了库的顺序并将libbost_system放在了库的末尾。它仍然得到同样的错误.
  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 似乎没有任何区别。两者都无法正常工作。

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

【问题讨论】:

  • 它是真正的错误日志,每个 not char 周围都有空格吗?
  • @S.M.你是指每个单词之间的空格吗?好像是编辑格式的问题,编译器的输出是没有这些空格的
  • 请复制粘贴真实的内容。
  • @S.M.我重新编辑了错误消息,并直接从控制台复制了它。
  • bin/lrelease/libmyProject.a 应该是-Lbin/lrelease -lmyProject

标签: c++ linux boost


【解决方案1】:

错误消息表明您的 libmyProject.a 库依赖于 Boost.Thread 和 Boost.System。但是在链接器命令行中,它是在 Boost 库之后指定的,因此它的 Boost 依赖关系未解决。请注意,链接器命令行中库和目标文件的顺序是significant。在您的情况下,您需要在命令行中的库和目标文件之后移动 -lboost_thread-lboost_system

【讨论】:

  • 我看了你给的链接,照你说的做了,但还是报错。同样的代码可以在centos_6.5中正确运行而无需更改顺序,这让我很困惑。
猜你喜欢
  • 2015-04-09
  • 2017-02-25
  • 2013-10-19
  • 2012-06-30
  • 2021-03-30
  • 1970-01-01
  • 2019-07-05
  • 2016-05-26
  • 2012-11-08
相关资源
最近更新 更多