boost issue with static linking

Hello,
I am building a code which makes use of boost libraries.
My CMakeLists.txt looks like the following:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(MTmgen LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Boost 1.72 REQUIRED COMPONENTS serialization iostreams system filesystem)
add_library(Boost_serialization STATIC IMPORTED)
add_library(Boost_iostreams STATIC IMPORTED)
add_library(Boost_system STATIC IMPORTED)
add_library(Boost_filesystem STATIC IMPORTED)

set_target_properties(Boost_serialization PROPERTIES
        IMPORTED_LOCATION "${Boost_LIBRARY_DIRS}/libboost_serialization.a")
set_target_properties(Boost_iostreams PROPERTIES
        IMPORTED_LOCATION "${Boost_LIBRARY_DIRS}/libboost_iostreams.a")
set_target_properties(Boost_system PROPERTIES
        IMPORTED_LOCATION "${Boost_LIBRARY_DIRS}/libboost_system.a")
set_target_properties(Boost_filesystem PROPERTIES
        IMPORTED_LOCATION "${Boost_LIBRARY_DIRS}/libboost_filesystem.a")
add_executable(MTmgen mt1danis_generate_model.cpp)
target_include_directories(MTmgen PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(MTmgen
        PUBLIC
        Boost_filesystem
        Boost_iostreams
        Boost_system
        Boost_filesystem
        Boost_serialization
        )


I did downloaded/compiled boost and installed under the folder /usr/local/boost_clang/

now, when I build the code, I get the following error:
1
2
3
4
5
6
7
8
9
10
11
12
13
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_iostreams.a(file_descriptor.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(basic_oarchive.o): relocation R_X86_64_32S against symbol `_ZTVN5boost7archive6detail14basic_oarchiveE' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(basic_oserializer.o): relocation R_X86_64_32S against symbol `_ZTVN5boost7archive6detail17basic_oserializerE' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(basic_text_oprimitive.o): relocation R_X86_64_32S against hidden symbol `_ZTVN5boost7archive12codecvt_nullIcEE' can not be used when making a PIE object
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(extended_type_info.o): relocation R_X86_64_32 against hidden symbol `_ZZN5boost13serialization9singletonISt8multisetIPKNS0_18extended_type_infoENS0_6detail11key_compareESaIS5_EEE12get_instanceEvE1t' can not be used when making a PIE object
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(extended_type_info_typeid.o): relocation R_X86_64_32S against symbol `_ZTVN5boost13serialization13typeid_system27extended_type_info_typeid_0E' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(text_oarchive.o): relocation R_X86_64_32 against hidden symbol `_ZZN5boost13serialization9singletonINS_7archive6detail12extra_detail3mapINS2_13text_oarchiveEEEE12get_instanceEvE1t' can not be used when making a PIE object
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(archive_exception.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /usr/local/boost_clang/lib/libboost_serialization.a(basic_archive.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/MTmgen.dir/build.make:89: MTmgen] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/MTmgen.dir/all] Error 2
make: *** [Makefile:84: all] Error 2 


I think the solution will be to recompile the boost static libraries with the -lPIE flag. Is this correct?
Also, what is this error meaning? what is not working here?

Thank you
Are you aware that you have 'Boost_filesystem' twice in your target_link_libraries?

It is -fPIE not -lPIE
hello coder, thank for the short answer.
I did not notice the double link to filesystem, and thanks to point me the typo.

Still some context would be useful here, what is failing without the -fPIE flag? Why can't I simply point my compiler to the archives (which is from my understanding what the .a library are) and expect it links the boost code into my executable?
It is probably a version conflict. Either the compiler or the target system has some mismatch. In both cases it most likely means that you have some dated libs and you may even don't need this flag when you compile the libs on your system.

By the way: The most recent version of boost is now 1.74
Topic archived. No new replies allowed.