GeekLee
GeekLee

Reputation: 161

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

Although there are a lot of questions about this, I cannot find the right way to solve my problem.I make use of the libboost_systemin my project, and I add the Libs:=libconfig++ libboost_thread libboost_system in the makefile,but it still never work.

The environment of my machine:

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

So far, I have tried the following

  1. I write a test file to see if the boost library was installed correctly,The test file looks like this: [test.cpp][1] https://i.sstatic.net/Tb4I0.png, and then I compile with this command:g++ test.cpp -g -o main -lboost_system -lboost_thread,it does work in a right way. Does this mean that the boost library is installed correctly? In my opinion I think it's installed.
  2. I search the stackoverflow and find that lots of answer mention that the order of the libraries is important,so I check the order of the libs and put the libbost_systemat the end of the Libs.It still get the same error.
  3. Then I find that few answer infer to #define BOOST_ERROR_CODE_HEADER_ONLY,and I have no idea how to add this definition so I fail to solve the problem in the end.

Part of makefile of my project is shown below:

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
#
#

I got the error as follow:

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()'
...
...
...

Add or without L/usr/lib#LIBS := -lpthread -lboost_system -lboost_thread -lconfig++ -L/usr/lib seems to make no difference.Neither works properly.

I can be sure that the code is correct and can run correctly on other servers.So,I really don't know where the problem is. I will be appreciate if I could get any useful information about the error,Thank you very much:)

Upvotes: 1

Views: 2273

Answers (1)

Andrey Semashev
Andrey Semashev

Reputation: 10606

The error message suggests that your libmyProject.a library depends on Boost.Thread and Boost.System. But in the linker command line it is specified after the Boost libraries and thus leaves its Boost dependencies unresolved. Note that the order of the libraries and object files in the linker command line is significant. In your case you need to move -lboost_thread and -lboost_system after your libraries and object files in the command line.

Upvotes: 2

Related Questions