我正在x86_64 / Ubuntu 13.04上交叉编译Raspberry Pi项目.调用cmake后:
cmake -DCMAKE_TOOLCHAIN_FILE=./Toolchain-raspberry.pi .
然后make,链接失败:
/opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lopencv_gpu ...
问题是cmake生成的Makefile以下列方式调用链接器:
/opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ CMakeFiles/Watson.dir/main.cpp.o -o Watson -rdynamic -lopencv_gpu -lopencv_contrib -lopencv_legacy -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_video -lopencv_highgui -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
它没有指定这些共享库的路径.但是,如果我手动将–sysroot / opt / rpi-rootfs / flag添加到上面的命令,则链接会成功.
在交叉编译时,建议让cmake指定共享库的正确路径的方法是什么?
这是我的Toolchain-raspberry.pi文件:
SET(CMAKE_SYSTEM_NAME Linux) SET(CMAKE_SYSTEM_VERSION 1) SET(CMAKE_C_COMPILER /opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc) SET(CMAKE_CXX_COMPILER /opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++) SET(CMAKE_FIND_ROOT_PATH "/opt/rpi-rootfs/") SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
这是我的CMakeList.txt文件:
cmake_minimum_required(VERSION 2.8) project(watson) add_executable(Watson main.cpp) find_package(OpenCV required) target_link_libraries(Watson ${OpenCV_LIBS})
目标中的/ usr和/ lib目录是rsync到/ opt / rpi-rootfs /并且所有必需的* .so文件都在那里.阅读完cmake文档后,我原本期望设置CMAKE_FIND_ROOT_PATH可以解决这个问题,但显然没有.我使用的是cmake版本2.8.10.1.
解决方法
通过我的实践,
INCLUDE_DIRECTORIES(/opt/rpi-rootfs/usr/include) LINK_DIRECTORIES( /opt/rpi-rootfs/usr/lib /opt/rpi-rootfs/lib )
将工作