CMake:在不同的目录中找不到我的头文件
CMake: can't find my header file in a different directory
我有一个包含这些相关文件夹的 CMake 项目:
project_folder
-src
|--main.c
peripherals
|--something.h
|--something.c
我在 project_folder
中的 CMake 包括:
add_subdirectory(peripherals)
if (NOT $ENV{TARGET_SOURCES} STREQUAL "")
target_sources(app PRIVATE $ENV{TARGET_SOURCES})
else()
target_sources(app PRIVATE src/main.c)
endif()
我在 peripherals
下的 CMake 包括:
add_library (peripherals something.c)
target_include_directories (peripherals PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
CMake 在 src
:
add_executable(app main.c)
target_link_libraries(app PRIVATE peripherals)
我的项目正在完全构建,但是当我尝试在 main.c
中包含我的头文件时,我得到:
project_folder/peripherals/something.h:No such file or directory
在我的 main.c
文件中有 #include "peripherals/something.h"
。有谁知道如何解决这一问题?我不确定我的 #include 语句是否正确,我想我的 CMakeLists 文件中缺少内容
您需要定义可执行文件并将其与库一起“link”:
# project_folder/CMakeLists.txt
add_subdirectory(peripherals)
add_subdirectory(src)
# project_folder/src/CMakeLists.txt
add_executable(my_executable main.c)
target_link_libraries(my_executable PRIVATE peripherals)
然后,您需要将 header 正确地包含在 main.c
中 - 因为您已经 link 编辑了包含 peripherals
目录的库,所以您现在可以直接包含它:
#include "something.h"
您的包含语句必须相对于包含的目录。
编辑: 在您的示例中,${CMAKE_CURRENT_SOURCE_DIR}
是项目目录(您的 CMake 文件所在的位置)。
因此你应该能够写:
#include <peripherals/something.h>
您始终可以通过打印来检查 cmake 变量的内容。在您的 CMake 文件中,您可以这样写:
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
当 运行 cmake 时,您应该会在控制台输出中看到打印的路径。
您可以在 main.cpp
中执行 "#include "../peripherals/i2c_test.h"
或
在 project_folder
中的 CMake 中:
target_include_directories(app ${CMAKE_CURRENT_SOURCE_DIR})
然后在 main.c:
#include <peripherals/i2c_test.h>
....
在 project_folder 中的 CMake 中:
include_directories(src)
然后在 main.c:
#include <peripherals/i2c_test.h>
或
在 project_folder 中的 CMake 中:
include_directories(src/peripherals)
然后在 main.c:
#include <i2c_test.h>
我有一个包含这些相关文件夹的 CMake 项目:
project_folder
-src
|--main.c
peripherals
|--something.h
|--something.c
我在 project_folder
中的 CMake 包括:
add_subdirectory(peripherals)
if (NOT $ENV{TARGET_SOURCES} STREQUAL "")
target_sources(app PRIVATE $ENV{TARGET_SOURCES})
else()
target_sources(app PRIVATE src/main.c)
endif()
我在 peripherals
下的 CMake 包括:
add_library (peripherals something.c)
target_include_directories (peripherals PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
CMake 在 src
:
add_executable(app main.c)
target_link_libraries(app PRIVATE peripherals)
我的项目正在完全构建,但是当我尝试在 main.c
中包含我的头文件时,我得到:
project_folder/peripherals/something.h:No such file or directory
在我的 main.c
文件中有 #include "peripherals/something.h"
。有谁知道如何解决这一问题?我不确定我的 #include 语句是否正确,我想我的 CMakeLists 文件中缺少内容
您需要定义可执行文件并将其与库一起“link”:
# project_folder/CMakeLists.txt
add_subdirectory(peripherals)
add_subdirectory(src)
# project_folder/src/CMakeLists.txt
add_executable(my_executable main.c)
target_link_libraries(my_executable PRIVATE peripherals)
然后,您需要将 header 正确地包含在 main.c
中 - 因为您已经 link 编辑了包含 peripherals
目录的库,所以您现在可以直接包含它:
#include "something.h"
您的包含语句必须相对于包含的目录。
编辑: 在您的示例中,${CMAKE_CURRENT_SOURCE_DIR}
是项目目录(您的 CMake 文件所在的位置)。
因此你应该能够写:
#include <peripherals/something.h>
您始终可以通过打印来检查 cmake 变量的内容。在您的 CMake 文件中,您可以这样写:
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
当 运行 cmake 时,您应该会在控制台输出中看到打印的路径。
您可以在 main.cpp
中执行"#include "../peripherals/i2c_test.h"
或
在 project_folder
中的 CMake 中:
target_include_directories(app ${CMAKE_CURRENT_SOURCE_DIR})
然后在 main.c:
#include <peripherals/i2c_test.h>
....
在 project_folder 中的 CMake 中:
include_directories(src)
然后在 main.c:
#include <peripherals/i2c_test.h>
或
在 project_folder 中的 CMake 中:
include_directories(src/peripherals)
然后在 main.c:
#include <i2c_test.h>