I have the following CMake project which compiles no problem on visual studio and macos.
#specify minimum cmake version
cmake_minimum_required(VERSION 3.14)
# name the project (can retreive this using ${PROJECT_NAME})
project(GLbase)
# specify any specific flags we want (here we are just requiring at least C++11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# set up some variables, holds the location of source, include, dependencies, and test directories.
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(INC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dependencies")
# our library files.
set(SOURCE_FILES "${SRC_DIR}/Error.cpp"
"${SRC_DIR}/GLSLShader.cpp"
"${SRC_DIR}/Renderer.cpp"
"${SRC_DIR}/Utils.cpp"
"${SRC_DIR}/GLbase.cpp"
"${SRC_DIR}/Texture.cpp"
"${SRC_DIR}/imgui/imgui_demo.cpp"
"${SRC_DIR}/imgui/imgui_draw.cpp"
"${SRC_DIR}/imgui/imgui_impl_glfw.cpp"
"${SRC_DIR}/imgui/imgui_impl_opengl3.cpp"
"${SRC_DIR}/imgui/imgui_widgets.cpp"
"${SRC_DIR}/imgui/imgui.cpp")
set(HEADER_FILES "${INC_DIR}/Binding.h"
"${INC_DIR}/Drawable.h"
"${INC_DIR}/Error.h"
"${INC_DIR}/GLSLShader.h"
"${INC_DIR}/Renderer.h"
"${INC_DIR}/Utils.h"
"${INC_DIR}/GLbase.h"
"${INC_DIR}/Texture.h"
"${INC_DIR}/stb_image.h"
"${INC_DIR}/imconfig.h"
"${INC_DIR}/imgui_impl_glfw.h"
"${INC_DIR}/imgui_impl_opengl3.h"
"${INC_DIR}/imgui_internal.h"
"${INC_DIR}/imgui.h"
"${INC_DIR}/imstb_rectpack.h"
"${INC_DIR}/imstb_textedit.h"
"${INC_DIR}/imstb_truetype.h")
# create the library
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
# where are the includes for the library.
target_include_directories(${PROJECT_NAME} PUBLIC "${INC_DIR}")
# dont add lib prefix on macOS
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
# Dependencies
# GLFW
set(GLFW_DIR "${LIB_DIR}/glfw")
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
add_subdirectory("${GLFW_DIR}")
target_link_libraries(${PROJECT_NAME} "glfw" "${GLFW_LIBRARIES}")
target_include_directories(${PROJECT_NAME} PUBLIC "${GLFW_DIR}/include")
target_compile_definitions(${PROJECT_NAME} PRIVATE "GLFW_INCLUDE_NONE")
# glad
set(GLAD_DIR "${LIB_DIR}/glad")
add_library("glad" STATIC "${GLAD_DIR}/src/glad.c")
target_include_directories("glad" PRIVATE "${GLAD_DIR}/include")
target_include_directories(${PROJECT_NAME} PUBLIC "${GLAD_DIR}/include")
target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}")
# glm
set(GLM_DIR "${LIB_DIR}/glm")
add_subdirectory("${GLM_DIR}")
target_include_directories(${PROJECT_NAME} PUBLIC "${GLM_DIR}")
target_link_libraries(${PROJECT_NAME} "glm" "${CMAKE_DL_LIBS}")
# assimp
set(ASSIMP_DIR "${LIB_DIR}/assimp")
add_subdirectory("${ASSIMP_DIR}")
target_include_directories(${PROJECT_NAME} PUBLIC "${ASSIMP_DIR}")
target_link_libraries(${PROJECT_NAME} "assimp" "${CMAKE_DL_LIBS}")
and then i have the following project which uses the above library as a dependency.
#specify minimum cmake version
cmake_minimum_required(VERSION 3.14)
# name the project (can retreive this using ${PROJECT_NAME})
project(GLBaseExample)
# specify any specific flags we want (here we are just requiring at least C++11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(GL_BASE_DIR "${ROOT_DIR}/gl-base")
add_executable(${PROJECT_NAME} ${ROOT_DIR}/example/Test.cpp)
add_subdirectory(${GL_BASE_DIR})
target_link_libraries(${PROJECT_NAME} "GLbase" "${CMAKE_DL_LIBS}")
target_include_directories(${PROJECT_NAME} PUBLIC "${GL_BASE_DIR}/include")
Project structure is
ROOT
- GLbase
- ....
- CMakeLists.txt
- Example
- Test.cpp
- CMakeLists.txt # CMakeLists for Example/Test.cpp
When I try to compile the Example project, I get unresolved symbols for every call to the GLbase library, what am I missing here?
question from:
https://stackoverflow.com/questions/65623063/cmake-project-depending-on-another-cmake-project-unresolved-symbols 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…