You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2022+CMake在WSL Ubuntu下的头文件包含问题求助

WSL Ubuntu环境下CMake FetchContent拉取GLFW后找不到头文件的问题

使用Visual Studio 2022开发,在WSL Ubuntu环境中编译包含<GLFW/glfw3.h>的代码时,提示源文件不存在,但相同代码在Windows系统下可正常运行(所有通过FetchContent拉取的库都存在此问题,本文以GLFW为例)。

当前使用的CMake代码如下:

cmake_minimum_required (VERSION 3.8)

project ("test project")

include(FetchContent)

FetchContent_Declare(
    glfw
    GIT_REPOSITORY https://github.com/glfw/glfw.git
    GIT_TAG 3.3.8
)

FetchContent_GetProperties(glfw)
if(NOT glfw_POPULATED)
    FetchContent_Populate(glfw)

    set(GLFW_BUILD_EXAMPLES OFF)
    set(GLFW_BUILD_TESTS OFF)
    set(GLFW_BUILD_DOCS OFF)
    set(GLFW_INSTALL OFF)

    add_subdirectory(${glfw_SOURCE_DIR} ${glfw_BINARY_DIR})
endif()

add_executable(test_exe "main.cpp")

target_link_libraries(test_exe PRIVATE glfw)

尝试过以下方法,但均存在问题:

  • 修改FETCHCONTENT_BASE_DIR为项目源目录作为临时解决方法,但在Windows与WSL Ubuntu环境间切换时,必须删除Windows下已拉取的文件,否则会出现如下错误:
The current CMakeCache.txt directory /home/olli/.vs/Omerta/external/glfw/glfw-subbuild/CMakeCache.txt is different than the directory c:/Users/Olli/source/repos/GitOlli/Omerta/external/glfw/glfw-subbuild where CMakeCache.txt was created.
  • 尝试显式指定包含目录,但同样无效:
target_include_directories(test_exe PRIVATE "${CMAKE_BINARY_DIR}/_deps/glfw-src/include")

内容的提问来源于stack exchange,提问作者Olli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:37:33