I'm writing a library that uses CMake and trying to get Abseil linked into it following the instructions here:
https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md
My CMakeLists.txt
basically contains:
add_library(MyLib SHARED src/mylib.cc)
add_subdirectory(third_party/abseil-cpp)
target_link_libraries(MyLib PRIVATE absl::base absl::strings absl::str_format)
Now if I compile my library, I get the following error:
/usr/bin/ld: third_party/abseil-cpp/absl/strings/libabsl_absl_str_format_internal.a(arg.cc.o): relocation R_X86_64_PC32 against symbol `_ZNKSt17basic_string_viewIcSt11char_traitsIcEE4sizeEv' can not be used when making a shared object; recompile with -fPIC
In other words, CMake compiles Abseil without using position independent code. I then tried for fun to see what happens if I change my library to be a static one instead. Changing CMakeLists.txt
to
add_library(MyLib STATIC src/mylib.cc)
add_subdirectory(third_party/abseil-cpp)
target_link_libraries(MyLib PRIVATE absl::base absl::strings absl::str_format)
I get the following error when running cmake
:
CMake Error: install(EXPORT "MyLibTargets" ...) includes target "MyLib" which requires target "absl_base" that is not in the export set.
CMake Error: install(EXPORT "MyLibTargets" ...) includes target "MyLib" which requires target "absl_strings" that is not in the export set.
CMake Error: install(EXPORT "MyLibTargets" ...) includes target "MyLib" which requires target "absl_str_format" that is not in the export set.
I'm a CMake newbie and I haven't managed to figure out how to fix either error. How can you transitively in CMake specify that dependencies should compile to position independent code?