little_planet
little_planet

Reputation: 1035

cmake: how to tell library where to find dependencies

I would like to build a library in a static way and integrate it (together will all its dependencies) in my project. The target platform (in the long run) is android. Currently I just want to test the whole process for my linux (debian 64 bit) vm on windows host. The resulting project structure should look like this

project/
|
|----- thirdparty/
|           |
|           |----- lib1/ (depends on 2 and 3)
|           |----- lib2/
|            ----- lib3/
 ----- application/

lib1 is the library I use in my application. All the libs are already build for my current target system. I used the configure scripts which came with the libraries. Because lib1 needs lib2 and lib3 I referenced them by using with-lib2-prefix=<pathof-lib2>. The cmake script which I use to build the application with QtCreator links lib1 like this:

ADD_LIBRARY(lib1 STATIC IMPORTED)
SET(lib1_path ${CMAKE_SOURCE_DIR}/thirdparty/lib1/lib1.a)
# and give path to external library with IMPORTED_LOCATION
SET_TARGET_PROPERTIES(gpgme PROPERTIES IMPORTED_LOCATION ${lib1_path}) 

//later...

target_link_libraries(  ${COMPONENT_NAME}                           
                        lib1
)

Lib1 is found as expected, but as you maybe already suspect the dependencies are not found. Indeed I never told cmake how to. However because I'm very new to cmake I don't know how this is done.

Either I would like to tell lib1 where to find lib2 and lib3 with cmake. Or if this is not possible without touching the "build-process" of lib1, maybe I could somehow manage to call the ./configure from cmake in the right way. Does anybody had a similar problem and could give me a advice how to make this work?

Upvotes: 1

Views: 1034

Answers (1)

little_planet
little_planet

Reputation: 1035

I solved the problem. I had to add every dependency library to my cmake file:

ADD_LIBRARY(lib3 STATIC IMPORTED)
SET(lib3_path ${CMAKE_SOURCE_DIR}/thirdparty/lib3/lib/lib3.a)
SET_TARGET_PROPERTIES(lib3 PROPERTIES IMPORTED_LOCATION ${lib3_path})

ADD_LIBRARY(lib2 STATIC IMPORTED)
SET(lib2_path ${CMAKE_SOURCE_DIR}/thirdparty/lib2/lib/lib2.a)
SET_TARGET_PROPERTIES(lib1 PROPERTIES IMPORTED_LOCATION ${lib2_path})

ADD_LIBRARY(lib1 STATIC IMPORTED)
SET(lib1_path ${CMAKE_SOURCE_DIR}/thirdparty/lib1/lib/lib1.a)
SET_TARGET_PROPERTIES(lib2 PROPERTIES IMPORTED_LOCATION ${lib1_path})

and then link them in the correct order:

target_link_libraries(  ${COMPONENT_NAME}                           
                        lib1
                        lib2
                        lib3
)

If you link link2 and lib3 before lib1 is linked the linker will "optimize lib2 and 3 away".

Upvotes: 0

Related Questions