简体   繁体   中英

CMake is unable to locate include files

在此处输入图像描述

The CMakeLists.txt file in base folder:

# 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.

How can I resolve this?

Since you are using the #include directive with the <> brackets. 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! 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 . Thus the path should most likely be src/ as Tsyvarev already pointed out . 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 .

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] .

For more info on specifying the source-tree check this link . Additionally check out CMAKE_SOURCE_DIR , target_include_directories and add_subdirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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