简体   繁体   中英

Linking against Boost on Ubuntu x64

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM