Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use vcpkg with cross-compilation #123

sshcrack started this conversation in General
Discussion options

Hi!
This is a quick little guide to get vpckg up and running with cross compilation.

Requirements

Setup Triplet

<VCPKG_ROOT>/triplets/community/arm64-rpi.cmake

set(VCPKG_TARGET_ARCHITECTURE arm64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE <Put your toolchain file here as it was instructed in the wiki>) #Set this toolchain file

Modify toolchain file

You'll need to modify the original toolchain file that you created with the wiki. Remove the line starting with set(CMAKE_FIND_ROOT_PATH. You'll also need to append set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) at the end of the file.
It should now look something like this:

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(tools ${CMAKE_CURRENT_LIST_DIR}/tools/cross-pi-gcc-12.2.0-64) # Adjust cross compile gcc version 
set(rootfs_dir ${CMAKE_CURRENT_LIST_DIR}/rootfs)
# set(CMAKE_FIND_ROOT_PATH ${rootfs_dir}) UNCOMMENT THIS LINE!!!
set(CMAKE_SYSROOT ${rootfs_dir})
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
## Compiler Binary 
SET(BIN_PREFIX ${tools}/bin/aarch64-linux-gnu)
SET (CMAKE_C_COMPILER ${BIN_PREFIX}-gcc)
SET (CMAKE_CXX_COMPILER ${BIN_PREFIX}-g++ )
SET (CMAKE_LINKER ${BIN_PREFIX}-ld 
 CACHE STRING "Set the cross-compiler tool LD" FORCE)
SET (CMAKE_AR ${BIN_PREFIX}-ar 
 CACHE STRING "Set the cross-compiler tool AR" FORCE)
SET (CMAKE_NM {BIN_PREFIX}-nm 
 CACHE STRING "Set the cross-compiler tool NM" FORCE)
SET (CMAKE_OBJCOPY ${BIN_PREFIX}-objcopy 
 CACHE STRING "Set the cross-compiler tool OBJCOPY" FORCE)
SET (CMAKE_OBJDUMP ${BIN_PREFIX}-objdump 
 CACHE STRING "Set the cross-compiler tool OBJDUMP" FORCE)
SET (CMAKE_RANLIB ${BIN_PREFIX}-ranlib 
 CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
SET (CMAKE_STRIP {BIN_PREFIX}-strip 
 CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) #Add this line

Configuring

Now use the following command to configure your project:

# in your build directory
cmake -DCMAKE_TOOLCHAIN_FILE=<something>/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=arm64-rpi -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=<something>/rpi-cross-compile/PI.cmake ..

I'm very new to writing guides. I would be happy to receive feedback!

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hey, maybe someone can help:
When I comment the line set(CMAKE_FIND_ROOT_PATH ${rootfs_dir}) vcpkg does its job and the project can be configured.
But when I try to build the project using make, I'm getting these errors:

 /usr/bin/ld../vcpkg_installed/arm64-rpi/debug/lib/libspdlogd.a(spdlog.cpp.o): Relocations in generic ELF (EM: 183)

Now this seems like the wrong linker is being used. I guess this is achieved by CMAKE_FIND_ROOT_PATH right?
Anyway, it isn't working :/

You must be logged in to vote
1 reply
Comment options

Alright got a fix and updated the guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /