0

I want to create a shared library with 'sqlite3(SHARED)' and 'glog(STATIC)' installed.

add_library(sqlite3 SHARED IMPORTED)
add_library(glog STATIC IMPORTED)
set_target_properties(sqlite3 PROPERTIES IMPORTED_LOCATION /usr/local/lib/libsqlite3.so)
set_target_properties(glog PROPERTIES IMPORTED_LOCATION /usr/local/lib/libglog.a)

add_library(${MY_LIBRARY} SHARED ${MY_SOURCE})
target_link_libraries(${MY_LIBRARY} sqlite3 glog)

This causes following error

/usr/local/lib/libglog.a: can not be used when making a shared object; recompile with -fPIC

What is wrong in CMakeLists.txt?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153

1 Answers1

1

Your problem isn't with CMake (though you should never have hardcoded paths... use find_package or find_library), it's that you're trying to do something impossible. As your compiler tells you,

/usr/local/lib/libglog.a: can not be used when making a shared object; recompile with -fPIC

libglog.a was not compiled with position-independent code, so it cannot be used in a shared library, period. There's more information here on SO: How to recompile with -fPIC

You'll need to recompile libglog.a with -fPIC.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • 1
    @makepossible99 If this works, please accept the answer (that earns +15 for Alex and +2 for you) – Wolf Mar 19 '21 at 11:10