cmake iS不能够将标题文件包含在我的源文件中
我正在学习cmake,然后到达学习如何包含标头文件的部分。问题是我遇到了一个错误,说尚未找到标头文件。
顺便说一句,我在Windows 10上。
我目前所拥有的只是src
和includ
目录以及cmakelists.txt
file。在源中,我只有一个文件,也只有一个文件。 这是我的main.cpp
文件:
#include <iostream>
#include "include/main.h"
int main() {
std::cout << "Hello, World!\n";
sayHi("Joshua");
return 0;
}
这是我的cmakelists.txt
file。
cmake_minimum_required(VERSION 3.10)
set(CXX_STANDARD 17)
set(CXX_STANDARD_REQUIRED ON)
project(hello VERSION 1.0)
add_executable(hello ../src/main.cpp)
target_include_directories(hello PUBLIC ${CMAKE_CURRENT_SRC_DIR}/include)
编辑:我修复了源代码,以便我包括main.h
,而不是include/main.h
,但我仍然最终得到同样的错误:
"C:\Users\HP\desktop\test\4\build\hello.sln" (destino padrão) (1) ->
"C:\Users\HP\desktop\test\4\build\hello.vcxproj.metaproj" (destino padrão) (3) ->
"C:\Users\HP\desktop\test\4\build\hello.vcxproj" (destino padrão) (4) ->
(ClCompile destino) ->
C:\Users\HP\Desktop\test\4\src\main.cpp(3,10): fatal error C1083: Cannot open include file: 'main.h
': No such file or directory [C:\Users\HP\desktop\test\4\build\hello.vcxproj]
0 Aviso(s)
1 Erro(s)
```
What is going on?
I'm learning CMake and got to the part where I learn how to include header files. The problem is that I get an error, saying that the header file has not been found.
I'm on Windows 10, by the way.
All I have as of right now is a src
and include
directories along with the CMakeLists.txt
file. In source, I only have a single file and I also only have a single file in include.
This is my main.cpp
file:
#include <iostream>
#include "include/main.h"
int main() {
std::cout << "Hello, World!\n";
sayHi("Joshua");
return 0;
}
And this is my CMakeLists.txt
file.
cmake_minimum_required(VERSION 3.10)
set(CXX_STANDARD 17)
set(CXX_STANDARD_REQUIRED ON)
project(hello VERSION 1.0)
add_executable(hello ../src/main.cpp)
target_include_directories(hello PUBLIC ${CMAKE_CURRENT_SRC_DIR}/include)
EDIT: I fixed the source code so that I includes main.h
, rather than include/main.h
, but I still end up with the same error:
"C:\Users\HP\desktop\test\4\build\hello.sln" (destino padrão) (1) ->
"C:\Users\HP\desktop\test\4\build\hello.vcxproj.metaproj" (destino padrão) (3) ->
"C:\Users\HP\desktop\test\4\build\hello.vcxproj" (destino padrão) (4) ->
(ClCompile destino) ->
C:\Users\HP\Desktop\test\4\src\main.cpp(3,10): fatal error C1083: Cannot open include file: 'main.h
': No such file or directory [C:\Users\HP\desktop\test\4\build\hello.vcxproj]
0 Aviso(s)
1 Erro(s)
```
What is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是
$ {cmake_current_source_dir}
,而不是$ {cmake_current_src_dir}
。如果修复此操作,则编译器应查看正确的Include Directory。
目前,由于变量不存在,因此它看到的唯一目录是
/include
,这不是您想要的。It's
${CMAKE_CURRENT_SOURCE_DIR}
, not${CMAKE_CURRENT_SRC_DIR}
.If you fix this then the compiler should see the correct include directory.
At the moment because the variable doesn't exist, the only directory it sees is
/include
which is not what you want.