CMake 交叉编译二级依赖
CMake Cross Compilation secondary dependencies
我有一个程序可以在我的目标机器 (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 生成 makefile,并且构建步骤失败并出现错误:
[ 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 什么以便链接器可以找到这些次要依赖项?
我想通了我的问题。当我克隆图像并将其安装到我的笔记本电脑上时,我破坏了克隆图像中的所有符号链接,例如 libz.so
这就是 cmake 找不到它们的原因。当我修复符号链接时,cmake 继续以同样的方式抱怨下一个损坏的符号链接。我认为有太多符号链接无法手动修复它们,但至少现在我知道出了什么问题。
我有一个程序可以在我的目标机器 (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 生成 makefile,并且构建步骤失败并出现错误:
[ 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 什么以便链接器可以找到这些次要依赖项?
我想通了我的问题。当我克隆图像并将其安装到我的笔记本电脑上时,我破坏了克隆图像中的所有符号链接,例如 libz.so
这就是 cmake 找不到它们的原因。当我修复符号链接时,cmake 继续以同样的方式抱怨下一个损坏的符号链接。我认为有太多符号链接无法手动修复它们,但至少现在我知道出了什么问题。