Visual Studio 中的 Cmake 项目再也找不到标准库了

问题描述

更新 1:我启动了一个新项目,该项目导致错误返回,并创建了一个额外的错误

C1034: iostream: no include path set

更新 2:我设法通过在顶级 CMakeList 中包含以下内容解决问题

if(WIN32)
    include_directories(
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30037\\include"
        "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt"
    )
    link_directories(
        "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\um\\x64"
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30037\\lib\\onecore\\x64"
        "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\ucrt\\x64"
    )
endif()

不过,我还不想将此标记解决方案。因为我发现完全不能接受所有这些额外的冲刷对于让一个项目在一个程序中工作是必要的我的项目或实际问题。


我昨天安装了 VS2019 并编译了一个小测试程序,它只是向控制台写入一个数字。

这是我的 main.cpp 文件内容

#include <iostream>
#include <iomanip> // Included to test whether including other libs did work.

#include "Graphics/window.hpp"

int main()
{
    std::cout << "Width: "  << std::endl;
    std::cout << "Height: " << std::endl;

    return 0;
}

昨天它运行得很好,但是当我在包含 GLFW 后打开项目时,它无法找到 iostream 库并显示以下错误

Error (active)  E0135   namespace "std" has no member "endl"    Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   9   
Error (active)  E0135   namespace "std" has no member "endl"    Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   10  
Error (active)  E0135   namespace "std" has no member "cout"    Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   9   
Error (active)  E0135   namespace "std" has no member "cout"    Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   10  
Error (active)  E1696   cannot open source file "iostream"  Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   1   
Error (active)  E1696   cannot open source file "iomanip"   Slash.exe (source\Slash.exe) - x64-Debug    C:\Users\Daisy\source\repos\CMakeProject1\source\main.cpp   2   
Error   C1083   Cannot open include file: 'iostream': No such file or directory C:\Users\Daisy\source\repos\CMakeProject1\out\build\x64-Debug\CMakeProject1 C:\Users\Daisy\source\repos\CMakeProject1\out\source\main.cpp   1   

这是相关的 CMake 文件

项目根

cmake_minimum_required (VERSION 3.19)

project(Slash)

set(CMAKE_RUNTIME_OUTPUT_DIR ${PROJECT_SOURCE_DIR}/bin)

add_subdirectory(source)

项目根目录/源

add_subdirectory(Graphics)

add_executable (${CMAKE_PROJECT_NAME} main.cpp)

target_include_directories(${CMAKE_PROJECT_NAME}
PUBLIC
    Graphics/include
)

target_link_libraries(${CMAKE_PROJECT_NAME}
    Graphics
)

项目根目录/源代码/图形

add_library(Graphics
    ${CMAKE_CURRENT_LIST_DIR}/src/window.cpp
)

target_include_directories(Graphics
PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/include/Graphics
)

我也试过不再包含 GLFW,但这也无济于事。 (以防万一:我通过使用随附的 CMAKE 文件并使用 add_subdirectory() 命令来包含 GLFW。)

我已经手动查看了标准库,但在我的机器上找不到它们。我只是觉得很奇怪 CMake 无法再定位这些文件

我还尝试在 samae 结果中包含其他标准库,以确保不仅仅是 iostream 给我带来了麻烦。

我使用的是库存 C++ 桌面工作负载,并选择了所有认组件,以防出现问题。

我之前在尝试使用不同的编译器和构建系统在 Windows 上设置我的 C++ 工具时遇到了类似的问题,所以我认为这只是与 CMake 相关。

预先感谢您的任何建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)