I've search the internet all over, but there is no simple answer for the issue. I've read this: CMake: Attempted to add link library to target which is not built in this directory
But i don't want to put the target_link_libraries
into the same cmake list, instead i want to leave the linkage commands inside each respective list because a library cmake list has to know which one library dependency it must be linked with.
Mine project's structure is this:
<main_exe> : /main
| : |
<main_lib> : +- /_3dparty
/ \ : | |
<lib_A> <lib_B> : | +- /lib_A/CMakeLists.txt
\ / : | +- /lib_B/CMakeLists.txt
<lib_X> : | +- /lib_X/CMakeLists.txt
: |
: +- /CMakeLists.txt
All 3 libraries are standalone and static, and i don't want to make them as a subdirectory to each other.
But when i tried to add_subdirectory
with external binary directory from lib_A
and lib_B
respectively:
if (NOT TARGET lib_X)
add_subdirectory(${lib_X_ROOT} ${CMAKE_BUILD_ROOT}/_3dparty/lib_X)
endif()
add_dependencies(${PROJECT_NAME} lib_X)
target_link_libraries(${PROJECT_NAME}
PUBLIC
lib_X
)
, then the cmake throw an error:
Attempt to add link library "lib_X" to target "lib_B" which is not built in
this directory
Is there a way to put it to work w/o excessive ExternalProject_Add
usage?
EDIT:
I reproduced the issue on the minimal example.
The library libB actually had slightly different structure:
/main
|
+- /_3dparty
| |
| +- /lib_A/CMakeLists.txt
| +- /lib_B
| | |
| | +- /libsubdir/CMakeLists.txt
| | +- /CMakeLists.txt
| |
| +- /lib_X/CMakeLists.txt
|
+- /CMakeLists.txt
That was the cause of the issue.
/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(main_project)
set(CMAKE_BUILD_ROOT ${CMAKE_CURRENT_LIST_DIR}/_build)
set(CMAKE_LIBS_ROOT ${CMAKE_CURRENT_LIST_DIR}/_3dparty)
add_library(mainlib ${CMAKE_CURRENT_LIST_DIR}/main.cpp)
add_subdirectory(${CMAKE_LIBS_ROOT}/libA ${CMAKE_BUILD_ROOT}/_3party/libA)
add_subdirectory(${CMAKE_LIBS_ROOT}/libB ${CMAKE_BUILD_ROOT}/_3party/libB)
target_link_libraries(mainlib
PUBLIC
libA
libB
)
/_3dparty/libA/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(libA_project)
add_library(libA STATIC ${CMAKE_CURRENT_LIST_DIR}/main.cpp)
add_subdirectory(${CMAKE_LIBS_ROOT}/libX ${CMAKE_BUILD_ROOT}/_3party/libX)
target_link_libraries(libA
PUBLIC
libX
)
/_3dparty/libB/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(libB_project)
#add_library(libB STATIC ${CMAKE_CURRENT_LIST_DIR}/libsubdir/main.cpp)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libsubdir) # the root cause of the issue
#add_subdirectory(${CMAKE_LIBS_ROOT}/libX ${CMAKE_BUILD_ROOT}/_3party/libX) # would be an error if add this twice
target_link_libraries(libB # <- the issue error is here
PUBLIC
libX
)
/_3dparty/libB/libsubdir/CMakeLists.txt
add_library(libB STATIC main.cpp)
/_3dparty/libX/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(libX_project)
add_library(libX STATIC ${CMAKE_CURRENT_LIST_DIR}/main.cpp)
Output:
x:\_cmake_test\_out>cmake .. -G "Visual Studio 14 2015"
CMake Error at _3dparty/libB/CMakeLists.txt:11 (target_link_libraries):
Attempt to add link library "libX" to target "libB" which is not built in
this directory.
-- Configuring incomplete, errors occurred!
See also "X:/_cmake_test/_out/CMakeFiles/CMakeOutput.log".
If uncomment the add_library(libB ...
and comment add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libsubdir)
then the issue disappears.
Any suggestions how to fix that w/o edit a 3dparty library?