Search code examples
c++sqlitecmakegit-submodules

cmake: library dependent on another library (sqlite3pp)


I am trying to link with cmake sqlite3pp library to my application, which in turn depends on sqlite. So the dependency graph is following:

my_app  --> sqlite3pp  --> sqlite

A trick that works fine, but is too clumsy for distribution (I am using git submodule), is to put sqlite inside the libs/sqlite3pp/src:

project
|___ src    # my project
|___ libs   # libraries
|      |___ sqlite3pp
|      |        |___ src
|      |              |___sqlite   # I copy it here; clumsy  :-(
...    ...

With following line in the sqlite3pp/src/CMakeLists.txt:

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/sqlite3")

However, with git submodule it creates a big mess, as I need to modify the submodule. So I try to pull the directory sqlite two levels up, into libs directory:

project
|___ src    # my project
|___ libs   # libraries
|      |___ sqlite3pp
|               |___ src
|
|___ sqlite   # Now it looks better
...    ...

Now, two approaches I tried did not work (I am very new to cmake):

1) when I replace the above statement with

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../sqlite3")

The cmake does not recognise the relative path.

2) Trying to manage linking from the project/CMakeLists.txt file by following lines:

set(sqlite_path "${CMAKE_CURRENT_SOURCE_DIR}/libs/sqlite")
include_directories(sqlite_path)
add_subdirectory( sqlite_path )
add_library(sqlite3 STATIC "${CMAKE_CURRENT_SOURCE_DIR}/libs/sqlite/sqlite3.c")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/sqlite3pp/src")
add_subdirectory("libs/sqlite3pp/src")


TARGET_LINK_LIBRARIES(sqlite3pp "${CMAKE_CURRENT_SOURCE_DIR}/libs/sqlite/sqlite3.c" )
target_include_directories( sqlite3pp PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libs/sqlite> )

leads to the situation when cmake looks successful, but in make after successful compilation of both libraries, when linking them to the project, I receive a bunch of 'undefined reference' errors:

linking CXX executable ../../bin/myapp-1.0.0/myapp
../libs/sqlite3pp/src/libsqlite3pp.so: undefined reference to `pthread_mutex_trylock'
...

Any ideas?


Solution

  • To solve the linker errors use Threads package and CMAKE_THREAD_LIBS_INIT variable from it:

    find_package(Threads REQUIRED)
    target_link_libraries(libsqlite3pp ${CMAKE_THREAD_LIBS_INIT})
    

    See FindThreads for details