在使用vcpkg和cmake进行交叉编译时,如何选择特定的库呢?

huangapple go评论110阅读模式
英文:

how to choose specific lib while cross-compiling (using vcpkg and cmake)

问题

我使用vcpkg和cmake进行跨平台开发。在WSL上完成一个示例代码后,我尝试将工具链更改为arm-linux-gnueabihf,然后出现以下错误信息:

[cmake]   Could not find a configuration file for package "plog" that is compatible
[cmake]   with requested version "".
[cmake] 
[cmake]   The following configuration files were considered but not accepted:
[cmake] 
[cmake]     /home/fuxiaoer/vcpkg/installed/x64-linux/share/plog/plogConfig.cmake, version: 1.1.9 (64bit)
[cmake] 
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:11 (find_package)
[cmake] 

其中plog是我尝试在这个项目中使用的库。

然后我移除了plog:x64-linux并重新构建项目,结果显示:

Could not find a package configuration file provided by "plog" with any of
[cmake]   the following names:
[cmake] 
[cmake]     plogConfig.cmake
[cmake]     plog-config.cmake

我尝试在[vcpkgroot]/triplets/arm-linux-gnueabihf.cmake中编写一个arm-linux-gnueabihf.cmake文件,内容如下:

set(VCPKG_TARGET_ARCHITECTURE arm)
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_CMAKE_SYSTEM_PROCESSOR arm)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)

然后使用vcpkg install plog:arm-linux-gnueabihf安装一个使用arm-linux-gnueabihf构建的新库,它成功工作了。其中/vcpkg/installed显示了3个文件夹:

arm-linux-gnueabihf  vcpkg  x64-linux

当我回到我的cmake项目时,它显示了和之前提到的相同的错误信息。似乎cmake没有更改其搜索路径,仍然使用默认路径x64-linux来查找。

我在我的CMakeLists.txt中添加了一行来告诉cmake plog的位置:

set(plog_DIR "/home/fuxiaoer/vcpkg/installed/arm-linux-gnueabihf/share/plog")

然后这个项目终于可以成功构建了,这意味着我使用的方法有效。

我的CMakeLists.txt如下所示:

cmake_minimum_required(VERSION 3.0.0)

project(cmake_with_ninja_and_vcpkg VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

# set(plog_DIR "/home/fuxiaoer/vcpkg/installed/arm-linux-gnueabihf/share/plog")

find_package(Eigen3 CONFIG REQUIRED)
find_package(plog CONFIG REQUIRED)

add_executable(cmake_with_ninja_and_vcpkg main.cpp)

target_link_libraries(cmake_with_ninja_and_vcpkg PRIVATE Eigen3::Eigen)
target_link_libraries(cmake_with_ninja_and_vcpkg PRIVATE plog::plog)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
英文:

I use vcpkg and cmake to do some cross-platform devlpment.
After finishing an sample code at WSL,I tried to change my toolchain to arm-linux-gnueabihf,then it shows below.

[cmake]   Could not find a configuration file for package "plog" that is compatible
[cmake]   with requested version "".
[cmake] 
[cmake]   The following configuration files were considered but not accepted:
[cmake] 
[cmake]     /home/fuxiaoer/vcpkg/installed/x64-linux/share/plog/plogConfig.cmake, version: 1.1.9 (64bit)
[cmake] 
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:11 (find_package)
[cmake] 

where plog is a lib I try to use in this project.

then I remove plog:x64-linux.rebuild my project.it shows:

Could not find a package configuration file provided by "plog" with any of
[cmake]   the following names:
[cmake] 
[cmake]     plogConfig.cmake
[cmake]     plog-config.cmake

I tred to wriet an arm-linux-gnueabihf.cmake file for vcpkg at [vcpkgroot]/triplets/arm-linux-gnueabihf.cmake.

set(VCPKG_TARGET_ARCHITECTURE arm)
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_CMAKE_SYSTEM_PROCESSOR arm)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)

then use vcpkg install plog:arm-linux-gnueabihf to install a new lib which use arm-linux-gnueabihf to build and it worked.
where /vcpkg/installed shows 3 folders

arm-linux-gnueabihf  vcpkg  x64-linux

When turn around to my cmake project,it shows same message like I mentioned before.it seems like cmake do not change its search path,and still use default path x64-linux to find

I added a new line at my cmakelists.txt to tell cmake where plog is.

set(plog_DIR "/home/fuxiaoer/vcpkg/installed/arm-linux-gnueabihf/share/plog")

then this project finally can build successfuly.that means the method I use worked.

my CMakeLists.txt is shown below.

cmake_minimum_required(VERSION 3.0.0)

project(cmake_with_ninja_and_vcpkg VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

# set(plog_DIR "/home/fuxiaoer/vcpkg/installed/arm-linux-gnueabihf/share/plog")

find_package(Eigen3 CONFIG REQUIRED)
find_package(plog CONFIG REQUIRED)

add_executable(cmake_with_ninja_and_vcpkg main.cpp)

target_link_libraries(cmake_with_ninja_and_vcpkg PRIVATE Eigen3::Eigen)
target_link_libraries(cmake_with_ninja_and_vcpkg PRIVATE plog::plog)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

答案1

得分: 0

更改工具链只需要进行干净的重新配置(即使用一个新的文件夹或删除CMakeCache.txt)。如果没有这样做,你无法更改vcpkg的三元组。

如果你尝试在已经配置好的项目中切换,或者忘记将VCPKG_TARGET_TRIPLET设置为你实际想要使用的三元组,那么就会出现你描述的情况。你可能还想设置VCPKG_HOST_TRIPLET。不要忘记在project()之前定义这些变量。

英文:

> I tried to change my toolchain to arm-linux-gnueabihf

changing toolchains simply requires a clean reconfigure (meaning using a fresh folder or deleting the CMakeCache.txt). You cannot change vcpkg triplets without that.

> it seems like cmake do not change its search path,and still use default path x64-linux to find

that is exactly what happens if you try to switch in an already configured project or forget to set VCPKG_TARGET_TRIPLET to the triplet you actually want to use. You might want to also set VCPKG_HOST_TRIPLET. Don't forget this needs to be defined before project()

huangapple
  • 本文由 发表于 2023年7月27日 15:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定