英文:
When I add a new subdirectory in CMake, the bug occurs
问题
I have project that builds with CMake.
Here is project hierarchy:
Project
Dependencies
glfw
Source
CMakeList.txt
main.cpp
CMakeList.txt
Then I try to add GLFW to my project and my CMakeList file looks like:
cmake_minimum_required(VERSION 3.8)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project( Project )
# Include sub - projects.
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(MAIN "Source/main.cpp")
add_subdirectory("Dependencies/glfw")
add_executable( Project ${MAIN} )
target_link_libraries( Project glfw )
With the version of the file above, everything works fine.
When I add this line 'add_subdirectory("Source")`' to CMakeList, an error occurs in the main.cpp that the file cannot be found: 'GLFW/glfw3.h'. I want to understand why the error occurs.
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>
int main()
{
//glewExperimental = true; // Needed for core profile
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
}
EDIT:
CMakeList.txt in Source directory:
add_executable (CMakeTarget "Game Engine.cpp" )
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
英文:
I have project that builds with CMake.
Here is project hierarchy:
Project
Dependencies
glfw
Source
CMakeList.txt
main.cpp
CMakeList.txt
Then I try to add GLFW to my project and my CMakeList file looks like:
cmake_minimum_required(VERSION 3.8)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project( Project )
# Include sub - projects.
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(MAIN "Source/main.cpp")
add_subdirectory("Dependencies/glfw")
add_executable( Project ${MAIN} )
target_link_libraries( Project glfw )
With the version of the file above, everything works fine.
When I add this line 'add_subdirectory("Source")`' to CMakeList, an error occurs in the main.cpp that the file cannot be found: 'GLFW/glfw3.h'. I want to understand why the error occurs.
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>
int main()
{
//glewExperimental = true; // Needed for core profile
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
}
EDIT:
CMakeList.txt in Source directory:
add_executable (CMakeTarget "Game Engine.cpp" )
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
答案1
得分: 2
问题在于您将 Project
与 glfw 连接起来,但没有将 CMakeTarget
(顺便说一下,这是一个奇怪的名称)与 glfw 连接起来。
您需要添加以下行:
target_link_libraries(CMakeTarget glfw)
这是因为 CMake 使用链接库来确定如何设置编译器的包含路径以及链接路径。具体来说,它可以正常工作,因为 glfw 具有 PUBLIC target_include_directories
,在此处可以找到:https://github.com/glfw/glfw/blob/9a87635686c7fcb63ca63149c5b179b85a53a725/src/CMakeLists.txt#L140
英文:
The issue is that you link Project
to glfw but do not link CMakeTarget
(strange name, btw) to glfw.
You need to add the line:
target_link_libraries( CMakeTarget glfw )
This is because CMake uses link libraries to determine how to set up include paths for the compiler as well as for linking. Specifically, it works because glfw has a PUBLIC target_include_directories
here: https://github.com/glfw/glfw/blob/9a87635686c7fcb63ca63149c5b179b85a53a725/src/CMakeLists.txt#L140
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论