简体   繁体   English

CMake 无法定位包含文件

[英]CMake is unable to locate include files

在此处输入图像描述

The CMakeLists.txt file in base folder: base文件夹中的CMakeLists.txt文件:

# leaf files
add_library(Account "")

target_sources(Account
  PRIVATE
        ${CMAKE_CURRENT_LIST_DIR}/Account.cpp
  PUBLIC
        ${CMAKE_CURRENT_LIST_DIR}/Account.h
        )

target_include_directories( Account
        PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}
        ${CMAKE_SOURCE_DIR}/initial/initial
  )

The above is generating the following build error:以上生成以下构建错误:

====================[ Build | all | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2020.2.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\pc\git\cmake_test\cmake-build-debug --target all -- -j 3
Scanning dependencies of target Account
[ 37%] Built target Initial
[ 25%] Building CXX object src/models/CMakeFiles/SavingsAccount.dir/SavingsAccount.cpp.obj
[ 50%] Building CXX object src/models/base/CMakeFiles/Account.dir/Account.cpp.obj
C:\Users\pc\git\cmake_test\src\models\SavingsAccount.cpp:6:10: fatal error: initial/initial/initial.h: No such file or directory
    6 | #include <initial/initial/initial.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
C:\Users\pc\git\cmake_test\src\models\base\Account.cpp:6:10: fatal error: initial/initial/initial.h: No such file or directory
    6 | #include <initial/initial/initial.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
mingw32-make[2]: *** [src\models\CMakeFiles\SavingsAccount.dir\build.make:83: src/models/CMakeFiles/SavingsAccount.dir/SavingsAccount.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:231: src/models/CMakeFiles/SavingsAccount.dir/all] Error 2
mingw32-make[1]: *** Waiting for unfinished jobs....
mingw32-make[2]: *** [src\models\base\CMakeFiles\Account.dir\build.make:83: src/models/base/CMakeFiles/Account.dir/Account.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:204: src/models/base/CMakeFiles/Account.dir/all] Error 2
mingw32-make: *** [makefile:103: all] Error 2

I discovered that this happens when a header file is included from a different parent folder or a child of a different parent folder.我发现当 header 文件包含在不同的父文件夹或不同父文件夹的子文件夹中时,会发生这种情况。

How can I resolve this?我该如何解决这个问题?

Since you are using the #include directive with the <> brackets.因为您使用的是带有<>括号的#include指令。 The obvious issue is within these lines of code:明显的问题是在这些代码行中:

target_include_directories( Account
        PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}
        ${CMAKE_SOURCE_DIR}/initial/initial #<-- here to be precise
  )

Bare in mind that whatever directory you point to it will get treated like a system path.请记住,您指向的任何目录都将被视为系统路径。

You are obviously (as you yourself know) not pointing to the correct folder.您显然(正如您自己所知)没有指向正确的文件夹。 Treat CMake like you would any other programming language , ie experiment with it and debug the variables you use!像对待任何其他编程语言一样对待 CMake ,即试验它并调试您使用的变量! The easiest way to debug this is to add a最简单的调试方法是添加一个

message(STATUS  ${CMAKE_SOURCE_DIR}) 

That way you will know for sure if you are pointing to the correct folder.这样您就可以确定您是否指向正确的文件夹。 Currently I assume that CMAKE_SOURCE_DIR is pointing to the root folder (where your main CMakeLists.txt is), because I would expect CLion to start building from the root CMakeLists.txt file (otherwise the existence of that file would be pointless) ie that is the top-level of your source tree .目前我假设CMAKE_SOURCE_DIR指向根文件夹(你的主要CMakeLists.txt所在的位置),因为我希望 CLion 从根CMakeLists.txt文件开始构建(否则该文件的存在将毫无意义)即源代码树的顶层 Thus the path should most likely be src/ as Tsyvarev already pointed out .因此路径应该很可能是src/正如Tsyvarev 已经指出的那样 But please confirm this by debugging it.但是请通过调试来确认这一点。

This would have been more obvious if you'd operate from the CLI, but because you are running this task from CLion you don't know for sure what is the CMAKE_SOURCE_DIR .如果您从 CLI 进行操作,这会更加明显,但是因为您是从 CLion 运行此任务,所以您不确定CMAKE_SOURCE_DIR是什么

This is because this is dependent on either the CWD from which you call cmake -B[build-folder] or what you specify to the -S flag in cmake -S[source_folder] -B[build_folder] .这是因为这取决于您从中调用cmake -B[build-folder]CWD或您在cmake -S[source_folder] -B[build_folder]中为-S标志指定的内容。

For more info on specifying the source-tree check this link .有关指定源代码树的更多信息,请查看此链接 Additionally check out CMAKE_SOURCE_DIR , target_include_directories and add_subdirectory另外检查CMAKE_SOURCE_DIRtarget_include_directoriesadd_subdirectory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM