在主机的链接阶段CMake交叉编译失败(目标是Raspberry Pi)

时间:2014-06-10 12:52:04

标签: c cmake raspberry-pi cross-compiling embedded-linux

我的源代码树是

cpp
├── bin
├── CMakeLists.txt
├── src
├── headers
├── Makefile
├── TestConfig.h
├── TestConfig.h.in
├── README.txt
├── third_party
├── x_build
└── xtoolchain.cmake

我正在使用crosstool-ng

编辑:其他信息(可能对其他人有帮助):我使用http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/的引用通过QEMU引导RPI(重要的是将img文件的大小调整为8GB)。

qemu-img resize 2014-01-07-wheezy-raspbian.img +6G
sudo fdisk /dev/mmcblk0
- use "parted" or 'd' options to delete the 2nd partition and then recreate it but with a larger size. (don't worry, the data will remain). 1st partition is vfat.
- reboot to activate the partition changes.
- use "sudo resize2fs /dev/mmclk0p2" (or /dev/sda2 if symlink set in udev rules) to enlarge the root file system.
- use e2fsck -f /dev/mmcblk0p2 to perform a file system check.

使用lib<package>-dev安装了所有apt-get和其他库。然后关闭QEMU

mkdir -p /tmp/rasp
sudo mount /home/test/raspberrypi/2014-01-07-wheezy-raspbian.img /tmp/rasp/ -o offset=$((122880*512))
cd $HOME/raspberrypi
sudo cp -ar /tmp/rasp ./rootfs
sudo chown -R $USER:$USER rootfs/home/pi
cd $HOME/raspberrypi/rootfs 

# also small hack around: error "Never use <bits/predefs.h> directly; include <stdc-predef.h> instead."
sudo cp /home/test/raspberrypi/rootfs/usr/include/features.h /home/test/raspberrypi/rootfs/usr/include/features.h-
sudo cp /home/test/local/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot/usr/include/features.h /home/test/raspberrypi/rootfs/usr/include
    sudo umount /tmp/rasp

我还设置了目标文件系统/home/test/raspberrypi/rootfs(这包含已经安装的交叉编译所需的所有库)

xtoolchain.cmake如下所示

# ========================= CROSS COMPILATION INITIALIZATION BEGIN =======================
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER
$ENV{HOME}/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER
$ENV{HOME}/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)

# where is the target environment
#SET(CMAKE_FIND_ROOT_PATH
#/home/test/local/x-tools/arm-unknown-linux-gnueabi /home/test/raspberrypi/rootfs)

SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)


# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

#include_directories ("/home/test/local/raspidev/include")
# ========================= CROSS COMPILATION INITIALIZATION END =========================

CMakeLists.txt(这是在父目录中的CMakeLists.txt,你可以从文件结构中看到)src目录中的部分看起来像

cmake_minimum_required(VERSION 2.8)
...
...
...
#Change 1 to 0 to disable logging
add_definitions(-DENABLE_LOG=1)

set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(CMAKE_LIBRARY_PATH $ENV{HOME}/raspberrypi/rootfs/lib:$ENV{HOME}/raspberrypi/rootfs/usr/lib:$ENV{HOME}/raspberrypi/rootfs/usr/local/lib)
set(CMAKE_MODULE_PATH $ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules)

#rdynamic is for stack unwinding on crash
set(CMAKE_CXX_FLAGS "-W -O3 -g -Wall -std=c++0x -rdynamic -D_AIBTRACE ${CMAKE_CXX_FLAGS}")

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS})

include_directories ("${PROJECT_SOURCE_DIR}/headers")
#add_subdirectory (headers)
...
...
...
# ======================== SQLITE3 =================================

pkg_check_modules(SQLITE3 REQUIRED sqlite3)
include_directories(${SQLITE3_INCLUDE_DIRS})
...
...  (...And other libraries)

我在x_build目录中进行了源代码构建。如下 cmake -DCMAKE_TOOLCHAIN_FILE=/home/test/xcompile/cpp/xtoolchain.cmake ../

奇怪的是,它似乎是与主机库相关联而不是目标库。 make VERBOSE=1显示了这个

它会引发所有库的链接器错误(这些库肯定存在于目标系统rootfs上)

/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lgmodule-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lgthread-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lxml2
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lglib-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lsqlite3
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -ldbus-1
collect2: error: ld returned 1 exit status

我有点失落。

编辑:

根据CMakeLists.txt

提出的要求
set (SOURCES
file1.cc
file2.cc
main.cc
)

# Similarly, why not concise files grouping that will be reused ;)?
set (COMMONS_LIBS
${OpenCV_LIBS} 
pthread
${GLIBMM_LIBRARIES} 
${GTKMM_LIBRARIES} 
#${GSTMM_LIBRARIES}
${GSTREAMER_LIBRARIES}
${SQLITE3_LIBRARIES}
#${DBUS_GLIB_LIBRARY}
${DBUS_LIBRARIES}
${DBUS_GLIB_LIBRARIES}
${OPENSSL_LIBRARIES}
)

add_executable (
${EXE_DAEMON} 
${SOURCES} 
)

target_link_libraries( 
${EXE_DAEMON} 
$COMMONS_LIBS}
)

install (TARGETS 
${EXE_DAEMON} 
DESTINATION bin)

1 个答案:

答案 0 :(得分:2)

我在CMakeLists.txt中添加了以下内容,然后在下面进行了一些更改并使其正常工作!

include($ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules/FindPkgConfig.cmake)
set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(SYSROOT $ENV{HOME}/raspberrypi/rootfs/)
set (PKG_CONFIG_SYSROOT_DIR ${SYSROOT})
set(PKG_CONFIG_PATH ${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/local/lib/pkgconfig:${SYSROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${SYSROOT}/usr/share/pkgconfig)
#Change 1 to 0 to disable logging
add_definitions(-DENABLE_LOG=1)
find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config PATHS /usr/bin DOC "pkg-config executable" NO_CMAKE_FIND_ROOT_PATH)
set (CMAKE_CXX_FLAGS "-W -O3 -g -Wall -std=c++0x -rdynamic  -D_TRACE ${CMAKE_CXX_FLAGS}"  CACHE STRING "" FORCE)
#rdynamic is for stack unwinding on crash
set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(CMAKE_LIBRARY_PATH $ENV{HOME}/raspberrypi/rootfs/lib:$ENV{HOME}/raspberrypi/rootfs/usr/lib:$ENV{HOME}/raspberrypi/rootfs/usr/local/lib)
set(CMAKE_MODULE_PATH $ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules)

include_directories(SYSTEM
$ENV{HOME}/raspberrypi/rootfs/usr/include/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/usr/include
$ENV{HOME}/raspberrypi/rootfs/usr/local/include
)

add_definitions(
-march=armv6zk 
-mfpu=vfp 
-mfloat-abi=hard
)

link_directories(

$ENV{HOME}/raspberrypi/rootfs/lib/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/lib
$ENV{HOME}/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/usr/lib
$ENV{HOME}/raspberrypi/rootfs/usr/local/lib
)   

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS})

然后做了以下

sudo vim /home/test/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libpthread.so

编辑

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/arm-linux-gnueabihf/libpthread.so.0 /usr/lib/arm-linux-gnueabihf/libpthread_nonshared.a )

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libpthread.so.0 libpthread_nonshared.a )

类似地,

sudo vim /home/test/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libc.so

编辑

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/arm-linux-gnueabihf/libc.so.6 /usr/lib/arm-linux-gnueabihf/libc_nonshared.a  AS_NEEDED ( /lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 ) ) 

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libc.so.6 libc_nonshared.a  AS_NEEDED ( ld-linux-armhf.so.3 ) )

最后,我将-lpcre库添加到了COMMON_LIBS

编译成功。然后我通过QEMU启动了raspberrypi并使用scp scp daemon pi@<ip address>:~将生成的二进制文件传输到PI模拟器并运行它。 Tadaa!它工作正常!