Windows + Visual Studio + VSCode + CMake 的疑難雜症

Environment

  1. Windows 10
  2. Visual Studio 2019
  3. CMake 3.27.7
  4. VSCode
  5. VSCode CMake Tools

1. CMAKE_BUILD_TYPE 是空的

參考一下這篇的處理。
大致上因為 Visual Studio + CMake 並不會主動 set CMAKE_BUILD_TYPE,除非你直接呼叫 CMake 時帶上 -DCMAKE_BUILD_TYPE=Debug

cmake -DCMAKE_BUILD_TYPE=Debug path/to/source

不過都用 VSCode 了就是懶得 command line 了,只好再請出 settings.json:

{

    "cmake.configureSettings":  {
        "CMAKE_BUILD_TYPE":  "${buildType}"
    }
}

Written with StackEdit.

CMake + vcpkg + vscode

CMake + vcpkg + vscode

  1. CMake 3.27.7
  2. CMake Tools
  3. VSCode

安裝的部份就跳過了,使用 Windows 和 boost::filesystem 當例子。

Key takeaways

vcpkg

使用 manifest mode,不開 vcpkg global integration。在 workspace root 下產生 vcpkg.json

{
    "$schema":  "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
    "name":  "test",
    "version":  "0.1.0",
    "dependencies":  [
        "boost-filesystem"
    ]
}

CMake

修改 .vscode/settings.json 讓 CMake 可以使用 vcpkg。

{
    "cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "you-path-to-vcpk/scripts/buildsystems/vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows"
    }
}

CMake

find_package(Boost REQUIRED COMPONENTS filesystem)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(hello PRIVATE ${Boost_LIBRARIES})
else()
    message(FATAL_ERROR "Boost not found. Please install Boost.")
endif()

Troubleshooting

Boost Not Found

  1. 先清掉 CMake cache 試試看:如果過程中,已經反覆 CMake -G 過,可能受到 CMake cache 影響,導致 find_package(Boost, ...) 不會再重頭開始找。

  2. 修改 .vscode/settings.json已開啟 CMake find boost debug log,方法如下:

    {
        "cmake.configureSettings": {
            ...
            "Boost_DEBUG": "on"
        }
    }
    
  3. 放棄 vscode ,繞過 CMake tool extension 的部份。

Reference

  1. vcpkg in CMake projects

Written with StackEdit.

CMake + vcpkg + vscode

CMake + vcpkg + vscode

  1. CMake 3.27.7
  2. CMake Tools
  3. VSCode

安裝的部份就跳過了,使用 Windows 和 boost::filesystem 當例子。

Key takeaways

vcpkg

使用 manifest mode,不開 vcpkg global integration。在 workspace root 下產生 vcpkg.json

{
    "$schema":  "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
    "name":  "test",
    "version":  "0.1.0",
    "dependencies":  [
        "boost-filesystem"
    ]
}

CMake

修改 .vscode/settings.json 讓 CMake 可以使用 vcpkg。

{
    "cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "you-path-to-vcpk/scripts/buildsystems/vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows"
    }
}

CMake

find_package(Boost REQUIRED COMPONENTS filesystem)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(hello PRIVATE ${Boost_LIBRARIES})
else()
    message(FATAL_ERROR "Boost not found. Please install Boost.")
endif()

Troubleshooting

Boost Not Found

  1. 先清掉 CMake cache 試試看:如果過程中,已經反覆 CMake -G 過,可能受到 CMake cache 影響,導致 find_package(Boost, ...) 不會再重頭開始找。

  2. 修改 .vscode/settings.json已開啟 CMake find boost debug log,方法如下:

    {
        "cmake.configureSettings": {
            ...
            "Boost_DEBUG": "on"
        }
    }
    
  3. 放棄 vscode ,繞過 CMake tool extension 的部份。

Reference

  1. vcpkg in CMake projects

Written with StackEdit.

Windows + Visual Studio + VSCode + CMake 的疑難雜症

Environment Windows 10 Visual Studio 2019 CMake 3.27.7 VSCode VSCode CMake Tools 1. CMAKE_BUILD_TYPE 是空的 參考一下 這篇 的處理。 大致上因為 Visual...