1

After reading a lot of SO questions on the matter, I just couldn't get it to work. I downloaded boost_1_60_0, then I ran the commands to build it:

sudo ./bootstrp.sh --prefix=/home/ricardo/boostlib
sudo ./b2 install -j8

I even tried running b2 like this:

sudo ./b2 install -j8 architecture=x86 address-model=64 

Does not matter. The error is always the same:

main.cpp:(.text+0x7e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x8a): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x96): undefined reference to `boost::system::system_category()'

Yeah, I know. Linking error, should run with -lboost_system and all. Yeah, you should put -L/home/ricardo/boostlib/lib. I know.

This is what my CMake looks like:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system -Wl,--verbose")

include_directories("/home/ricardo/boostlib/include")

You might be asking: What's the output of -Wl,--verbose? Well, surprisingly enough, libboost_system.so has been found:

attempt to open /home/ricardo/boostlib/lib/libboost_system.so succeeded
-lboost_system (/home/ricardo/boostlib/lib/libboost_system.so)

Okay. The library was found. I'm still trying to find some alternatives, nothing seems to work so far. So, even though I'm linking it against boost, and boost_system has been found, the program still does not compile due to... why? Can someone explain me?

Edit: How to link C++ program with Boost using CMake seems to be working. Though I still would like to know why my method does not work, and what should I do when I want to link against boost using just the g++ compiler, without CMake and Make. I've been able to link against Openblas succesfully before, so I wonder why it isn't working with Boost.

Edit2: This is the g++ command I got after running make VERBOSE=1, now I can see that the sources are being put AFTER the dependencies.

 -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system   CMakeFiles/prophet-service.dir/main.cpp.o  -o prophet-service -rdynamic

And this is my current CMake file:

cmake_minimum_required(VERSION 3.2)
project(prophet-service)
set(SOURCE_FILES
    main.cpp)
include_directories("/home/ricardo/boostlib/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system")
add_executable(prophet-service ${SOURCE_FILES})

It also seems that I don't have the libbost_system.a file. At least now I know that I need the .a file instead of the .so file.

Community
  • 1
  • 1
Ricardo Pieper
  • 2,613
  • 3
  • 26
  • 40
  • Please post the full link line output from VERBOSE=1 – xaxxon Apr 19 '16 at 11:35
  • The linker requires `.a` files not `.so` files. If you have locate installed on your system, try `locate libboost_system.a` to find it. – kenba Apr 19 '16 at 12:26
  • cxxflags is normally for compiling, for linking it may be better to use something like ldflags or whatever this is called in cmake. – Marc Glisse Apr 19 '16 at 21:37

2 Answers2

1

Are you linking in the right order? The thing that HAS the dependency needs to go before the thing that SATISFIES the dependency.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Hmm, I think so. I'm not sure now, I'll try to see what Make is doing behind the scenes, but that would make a lot of sense. – Ricardo Pieper Apr 19 '16 at 11:36
  • Yes, I found a good solution, but I am curious about why my solution does not work. Seems that I don't have the .a file inside my boostlib/lib folder. I updated my qustion and now I'm trying to solve it. – Ricardo Pieper Apr 19 '16 at 23:20
0

You need to link version of the library with extension .a because linker requires this extension (used for static libraries) and not .so which is used for shared libraries (the same of dll for Windows).

For example, if I search for libboost_system.a on my Ubuntu operating system I find the following:

frar@Home-PC:~$ locate libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/bin.v2/libs/system/build/gcc-4.8/release/link-static/threading-multi/libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/stage/lib/libboost_system.a
/usr/lib/x86_64-linux-gnu/libboost_system.a
Francesco Argese
  • 626
  • 4
  • 11