cmake iS不能够将标题文件包含在我的源文件中

发布于 2025-02-08 16:56:20 字数 1371 浏览 2 评论 0原文

我正在学习cmake,然后到达学习如何包含标头文件的部分。问题是我遇到了一个错误,说尚未找到标头文件。

顺便说一句,我在Windows 10上。

我目前所拥有的只是srcinclud目录以及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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-02-15 16:56:20

它是$ {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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文