Reputation: 1458
I have a Project with the following folder structure
/
|- build
|- MyLib1
| |- source
| | |- lib1_s1.cpp
| | |- lib2_s2.cpp
| |- include
| | |- lib1_s1.hpp
| | |- lib2_s2.hpp
| |- CMakeLists.txt
|- app
| |- source
| | |- app_s1.cpp
| | |- app_s2.cpp
| |- include
| | |- app_s1.hpp
| | |- app_s2.hpp
| |- CMakeLists.txt
|
|- CMakeLists.txt
My app depends on MyLib1 (compiled as a static library). lib1/CMakeLists.txt is as follows:
add_library(MyLib1 STATIC ${LIB_SRC})
install(TARGETS MyLib1 DESTINATION ${CMAKE_SOURCE_DIR}/bin)
And my app/CMakeLists.txt is as follows:
add_executable(app ${APP_SRC}/main.cpp)
target_link_libraries(app ${CMAKE_SOURCE_DIR}/bin/libMyLib1.a )
install(TARGETS app DESTINATION ${CMAKE_SOURCE_DIR}/bin)
In my build folder, I call cmake which builds MyLib1 successfully, but doesn't install it. The error generated is:
make[2]: *** No rule to make target `../bin/libMyLib1.a', needed by`/app'. Stop.
My Cmake version is:
cmake version 3.11.0-rc3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
I've already seen a few answers on stack overflow with the most relevant being this. But, the proposed solution doesn't seem to solve my problem.
What could I do differently to resolve the issue?
Upvotes: 4
Views: 2602
Reputation: 1488
Instead of linking your program directly with a .a
file, you should use the CMake target instead.
So this
target_link_libraries(app ${CMAKE_SOURCE_DIR}/bin/libMyLib1.a)
Becomes this
target_link_libraries(app MyLib1)
MyLib1
being the name you used on your add_library
command. This way CMake will generate the build files (e.g. makefiles) that orchestrate your build process in the correct order of dependencies on parallel builds (MyLib1
always before app
). I think the install command is no longer necessary.
Notice that using CMake target names is the most correct way, since CMake strength is to generate cross-platform build files. If you use a hardcoded reference to a .a
file, you're restricting the generated build scripts to compilers that generate static libraries in this format.
Upvotes: 3