I have some reasonable options.
The add_subdirectory command add_subdirectory often used to include a directory (in fact, it will not be a subdirectory in the sense of the file system), which really contains a stand-alone module, for example. library or executable file; which can be built without the help of the parent CMakeLists file. In this case, the CMakeLists.txt submodule will contain its own project command, which makes it difficult to use the global variable ${PROJECT_NAME} .
In your case, it seems that you just want the CMakeLists files in the subdirectories to add the file lists to the variables defined in the parent scope. You can achieve this using the CMake variable CMAKE_CURRENT_LIST_DIR in subordinate CMakeLists:
set(${PROJECT_NAME}_HEADERS ${${PROJECT_NAME}_HEADERS} ${CMAKE_CURRENT_LIST_DIR}/IThread.hh ${CMAKE_CURRENT_LIST_DIR}/UnixThread.hh ${CMAKE_CURRENT_LIST_DIR}/WinThread.hh ${CMAKE_CURRENT_LIST_DIR}/IMutex.hh ${CMAKE_CURRENT_LIST_DIR}/UnixMutex.hh ${CMAKE_CURRENT_LIST_DIR}/WinMutex.hh PARENT_SCOPE ) set(${PROJECT_NAME}_SOURCES ${${PROJECT_NAME}_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/UnixThread.cpp ${CMAKE_CURRENT_LIST_DIR}/WinThread.cpp ${CMAKE_CURRENT_LIST_DIR}/UnixMutex.cpp ${CMAKE_CURRENT_LIST_DIR}/WinMutex.cpp PARENT_SCOPE )
Make sure you do not have the project command in the CMakeLists files of your subdirectories.
In this case, this can be made a little simpler by replacing the add_subdirectory command with the include command, which will avoid problems with defining the scope.
To do this, remove the PARENT_SCOPE arguments from the set commands and in the top-level CMakeLists.txt:
add_subdirectory(dll) add_subdirectory(sockets) add_subdirectory(threads) include(dll/CMakeLists.txt) include(sockets/CMakeLists.txt) include(threads/CMakeLists.txt)
Another problem with target_link_libraries(${PROJECT_NAME} pthread) is simply that after the target_link_libraries command, the add_library command is add_library to define the library ${PROJECT_NAME} . Your easiest option is probably to move the target_link_libraries command to the parent CMakeLists.txt
source share