C++ CMake 找不到源文件 (add_executable)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43400453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:33:30  来源:igfitidea点击:

CMake cannot find source file (add_executable)

c++openglcmake

提问by assemblyDruid

I have been trying to follow the vendor's tutorial here: CMake-Tutorial, look over the documentation here: Cmake-Documentation, and educate myself as best as possible with sources on YouTube, but I am really struggling with getting the environment set up for working with OpenGL. Having tinkered with the Glitter boilerplate and the tutorials on open.gl and learnopengl.com, I've decided that understanding the build process is too important not to investigate.

我一直在尝试遵循供应商的教程:CMake-Tutorial,查看此处的文档:Cmake-Documentation,并尽可能使用 YouTube 上的资源来教育自己,但我真的很难为工作设置环境使用 OpenGL。在修改了 Glitter 样板以及 open.gl 和 learnopengl.com 上的教程后,我决定了解构建过程太重要了,不能不去研究。

In my investigation, I have come across the CMake error "cannot find source file" which is shown below in more detail. The problem seems to be arising from "add_executable".

在我的调查中,我遇到了 CMake 错误“找不到源文件”,如下所示。问题似乎是由“add_executable”引起的。

A similar question was asked here: CMake - Cannot find file. The solution involved ensuring that the ${PROJECT_SOURCE_DIR} was set properly for each variable, which I believe I have done.

这里问了一个类似的问题:CMake - 找不到文件。解决方案包括确保为每个变量正确设置了 ${PROJECT_SOURCE_DIR},我相信我已经做到了。

My file structure:

我的文件结构:

+ infuriating_project
    + bin // post compile results
    + src // my humble code
    + deps // external code
        +glew
            + include
            + src
        +glfw
            + include
            + src
        +glm
        +soil
            + lib
            + src

My CMakeLists.txt:

我的 CMakeLists.txt:

cmake_minimum_required (VERSION 3.0)


# Version Information ---------------------------------------------------------
project (openGL-practice)
SET (VERSION_MAJOR 1)
SET (VERSION_MINOR 0)
SET (VERSION_FEATURE 0)
SET (VERSION_PATCH 0)
SET (VERSION "${VERSION_MAJOR}.${VERSION_MINOR}")
SET (VERSION "${VERSION}.${VERSION_FEATURE}.${VERSION_PATCH}")
MESSAGE ("Version: ${VERSION}")

# Configure Source & Binary Directories ---------------------------------------
SET (PROJECT_ROOT "${PROJECT_SOURCE_DIR}")
SET (PROJECT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src")
SET (PROJECT_BINARY_DIR "${PROJECT_BINARY_DIR}/bin")
MESSAGE ("Source path: ${PROJECT_SOURCE_DIR}")
MESSAGE ("Binary path: ${PROJECT_BINARY_DIR}")

# Configure Depenency Directories ---------------------------------------------
SET (PROJECT_DEPS "${PROJECT_ROOT}/deps")
SET (glew_inc "${PROJECT_DEPS}/glew/include/GL/")
SET (glew_src "${PROJECT_DEPS}/glew/src/")
SET (glfw_inc "${PROJECT_DEPS}/glfw/include/GLFW/")
SET (glfw_src "${PROJECT_DEPS}/glfw/src/")
SET (glm "${PROJECT_DEPS}/glm/glm/")
SET (soil_lib "${PROJECT_DEPS}/lib/")
SET (soil_src "${PROJECT_DEPS}/src/")

# Include directories ---------------------------------------------------------
include_directories("
    ${PROJECT_SOURCE_DIR}
    ${PROJECT_BINARY_DIR}
    ${glew_inc}
    ${glew_src}
    ${glfw_inc}
    ${glfw_src}
    ${glm}
    ${soil_lib}
    ${soil_src}
    ${PROJECT_ROOT}
    ")

# Add executable --------------------------------------------------------------
add_executable(main main.cpp)

The following error is provided:

提供了以下错误:

CMake Error at CMakeLists.txt:46 (add_executable):
  Cannot find source file:

    main.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx


CMake Error: CMake can not determine linker language for target: main
CMake Error: Cannot determine link language for target "main".

Any and all help would be very much appreciated.

任何和所有的帮助将不胜感激。

回答by guenni_90

There are some points I'd like to mention.

有几点我想提一下。

  1. include_directorieshelps for finding header files. Source files must always have a complete relative path.

    Assuming that your main.cpp is within src, the correct syntax is

    add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)
    
  2. Including ${PROJECT_BINARY_DIR}does not make sense.

  3. Overwriting CMake default variables like ${PROJECT_SOURCE_DIR}is no good practice. In future you will always remember that you have done such a thing and for another programmer it is quite unexpected.

  1. include_directories有助于查找头文件。源文件必须始终具有完整的相对路径。

    假设您的 main.cpp 在 src 内,正确的语法是

    add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)
    
  2. 包括${PROJECT_BINARY_DIR}没有意义。

  3. 覆盖 CMake 默认变量之类${PROJECT_SOURCE_DIR}的不是一个好习惯。以后你会永远记得你做过这样的事情,对于另一个程序员来说,这是非常出乎意料的。

Hope it helps.

希望能帮助到你。

回答by Tsyvarev

In add_executable()call relative paths are alwaysinterpreted relative to variables CMAKE_CURRENT_SOURCE_DIRor CMAKE_CURRENT_BINARY_DIR.

add_executable()调用中,相对路径总是相对于变量CMAKE_CURRENT_SOURCE_DIRCMAKE_CURRENT_BINARY_DIR.

If you have source files in subdirectories of these dirs, you should explicitely specify these subdirectories:

如果您在这些目录的子目录中有源文件,您应该明确指定这些子目录:

add_executable(main src/main.cpp)


Also, variables like PROJECT_SOURCE_DIRshould be treated as readonly: while changing them isn't detected by CMake as an error, it breaks some things.

此外,像这样的变量PROJECT_SOURCE_DIR应该被视为只读:虽然改变它们不被 CMake 检测为错误,但它会破坏一些东西。