CMake + vcpkg + vscode
- CMake 3.27.7
- CMake Tools
- 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
-
先清掉 CMake cache 試試看:如果過程中,已經反覆
CMake -G
過,可能受到 CMake cache 影響,導致find_package(Boost, ...)
不會再重頭開始找。 -
修改
.vscode/settings.json
已開啟 CMake find boost debug log,方法如下:{ "cmake.configureSettings": { ... "Boost_DEBUG": "on" } }
-
放棄 vscode ,繞過 CMake tool extension 的部份。
Reference
Written with StackEdit.
沒有留言:
張貼留言