Now I am building a C++ project with CMake. I find CMake will introduce unnecessary library dependency in the project. Take an example, my project is composed of four parts: 1)lib1 2)lib2 3)lib3 and 4)app:
------lib1---
|----
lib2---
|----
lib3---
|----
app---
The source code in app will build a program, which relies on the dynamic library created in lib3. lib3 however, relies on the dynamic library created in lib2 and so on. I build the following CMake scripts to build a VC10 project:
1) Root CMakeLists:
cmake_minimum_required( VERSION 2.6 )
project (test)
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(lib3)
add_subdirectory(app)
2) lib1 CMakeLists.txt
add_definitions (-DEXP_STL )
add_library(lib1 SHARED lib1.cxx)
3) lib3 CMakeLists.txt
add_definitions (-DEXP_STL )
add_library(lib3 SHARED lib3.cxx)
target_link_libraries(lib3 lib2)
4) app CMakeLists.txt
add_executable(app main.cpp)
target_link_libraries(app lib3)
With these CMake scripts I have no problem in building a VC10 project. However, I notice that CMake will introduce unnecessary library dependency between libraries for VC10. For example, for the app application program, it only relies on one library, and that is, lib3. However, in VC10 project, I notice that it added the following additional dependencies:
..\lib3\Debug\lib3.lib
..\lib2\Debug\lib2.lib
..\lib1\Debug\lib1.lib
In the CMake script, however, only lib3 dependency is supposed to be introduced. For our example project, it may not be a problem, but in other cases the introduced redundant libraries can lead to compiling errors as they demand the proper searching paths. I am therefore wondering whether there is a way to eliminate these unnecessary libraries. Thanks!
CMake adds in dependent libraries transitively, which can be turned off by setting the property LINK_INTERFACE_LIBRARIES to an empty string. If you do
SET_TARGET_PROPERTIES(lib3 PROPERTIES LINK_INTERFACE_LIBRARIES “”)
then CMake will not generate a dependency from app to lib1 and lib2, when linking app.