CMake 交叉编译辅助依赖

时间:2021-07-02 13:44:52

标签: cmake cross-compiling ld

我有一个程序可以在我的目标机器(NVIDIA Jetson)上编译。现在我试图在我的笔记本电脑上交叉编译相同的程序。我有一个交叉编译器并克隆了 Jetson 以用作 sysroot。我正在遵循 NVIDIA 的 this 指南。我的程序编译,但在链接步骤失败。我正在链接一个库 (protobuf) 并且链接器找不到库的依赖项。我制作了一个最小的例子来演示这一点。

main.cpp:

#include <google/protobuf/util/json_util.h>


int main(int argc, char* argv[])
{
    google::protobuf::util::JsonPrintOptions jsonOptions;
    jsonOptions.always_print_enums_as_ints = true;
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

project(Test)

find_package(Protobuf REQUIRED)

add_executable(Test main.cpp)
target_include_directories(Test PRIVATE ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(Test PRIVATE ${PROTOBUF_LIBRARIES})

在我的目标计算机上,CMake 生成并构建得很好,但是在我的笔记本电脑上交叉编译时,CMake 生成生成文件,并且构建步骤失败并出现错误:

[ 50%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
[100%] Linking CXX executable Test
/home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/bin/ld: warning: libz.so.1, needed by /home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so, not found (try using -rpath or -rpath-link)
/home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/bin/ld: warning: libm.so.6, needed by /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/lib/../lib64/libstdc++.so, not found (try using -rpath or -rpath-link)
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflateInit2_'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflate'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflateEnd'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflate'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflateInit2_'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Test.dir/build.make:98: Test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

所需的库 (ZLIB) 存在于我的 sysroot (/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/lib/aarch64-linux-gnu/libz.so) 中,但链接器找不到它.

我的工具链文件是

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

set(CMAKE_SYSROOT /home/lhahn/Documents/CrossCompilation/Images/jetson)
set(CMAKE_C_COMPILER /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)


set(CMAKE_FIND_ROOT_PATH /home/lhahn/Documents/CrossCompilation/Images/jetson)
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)

我需要告诉 CMake 什么才能让链接器找到这些次要依赖项?

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。当我克隆图像并将其安装在我的笔记本电脑上时,我破坏了克隆图像中的所有符号链接,例如 libz.so,这就是 cmake 找不到它们的原因。当我修复符号链接时,cmake 继续以相同的方式抱怨下一个损坏的符号链接。我认为有太多符号链接无法手动修复它们,但至少现在我知道出了什么问题。