2014-06-10 152 views
1

我的源代码树是CMake的交叉编译失败(目标是树莓派)

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. 

安装使用apt-get所有lib<package>-dev和其他库。然后关闭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 ========================= 

src目录中的CMakeLists.txt(这是除父文件夹中CMakeLists.txt之外的文件结构中看到的)部分看起来像

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) 
+0

你能告诉我们完整的'ld'命令吗? – Peter

+0

@Peter对我的编辑有帮助吗? – enthusiasticgeek

+0

你能在这里粘贴完整的链接命令(创建上面显示的错误的命令),而不是CMakeLists.txt创建它吗? – Peter

回答

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 [email protected]<ip address>:~将生成的二进制文件传输到PI仿真器并运行。 Tadaa!它工作正常!