我发现了 vcpkg erlier,它看起来很有趣,但是易于使用。
据我了解,经过一天的调查,vcpkg pybind11 与 vcpkg python 搭配使用。但是当我启动一个简单的程序时,它被中止并出现以下输出 无法找到平台独立库<前缀>
这是一个已知问题,但不适用于vcpkg python。我不知道为什么?不可能设置 vcpkg python 的路径。
我很高兴有任何想法或见解。
谢谢
我明白了。看来在尝试将
vcpkg
与
cmake
和
pybind11
一起使用以创建 Python 绑定时遇到了问题。具体来说,看到错误消息“无法找到平台独立库<前缀>”,这表明的 Python 解释器找不到的 pybind11 模块。
This is a common issue, and it often arises from how
vcpkg
manages libraries and how
cmake
needs to be configured to find them. Here's a breakdown of what's likely happening and how to resolve it:
Understanding the Problem
-
vcpkg's Installation Directory:
vcpkg
typically installs libraries in a specific directory structure within its own installation folder. This structure is designed for isolation but means your system's default library search paths might not include it. -
cmake's Need for Guidance:
cmake
needs explicit instructions on where to find libraries and include files. If you've installed pybind11 and Python throughvcpkg
, you need to tellcmake
to look for them withinvcpkg's
directories.
Solution: Configuring cmake with vcpkg
The most reliable way to fix this is by integrating
vcpkg
with your
cmake
project:
-
vcpkg Integration:
-
Use
vcpkg.cmake
: The recommended approach is to use thevcpkg.cmake
helper script. Add these lines to yourCMakeLists.txt
:```cmake # Specify the vcpkg toolchain file set(CMAKE_TOOLCHAIN_FILE "
/scripts/buildsystems/vcpkg.cmake") # Specify the triplet (if you are using a specific one) set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "") # Find the package find_package(pybind11 REQUIRED)
# ... rest of your CMakeLists.txt ...
`` * Replace
with the actual path to your
vcpkginstallation. * Adjust
x64-windows` if you are using a different triplet. -
Link Your Target:
cmake
# Assuming you have a library or executable target named 'my_project'
target_link_libraries(my_project PRIVATE pybind11::embed)
Additional Tips
-
Clean Build: After making changes to your
CMakeLists.txt
, it's always a good idea to do a clean build:bash rm -rf build # Or your build directory mkdir build cd build cmake .. cmake --build .
-
Environment Variables (Less Reliable): While you can temporarily set environment variables like
PATH
andPYTHONPATH
to includevcpkg
directories, this is less maintainable in the long run. Usingvcpkg.cmake
is strongly recommended.
By following these steps, you ensure that
cmake
knows where to find your
vcpkg
-managed
pybind11
and Python installations, resolving the "Could NOT find platform independent libraries