与Poky进行交叉编译

时间:2014-08-22 16:09:41

标签: c++ boost cmake arm cross-platform

我尝试使用CMake建立我的跨平台开发环境。

只有当我尝试使用poky toolchain进行编译时才会遇到以下错误:

make [2]: * 没有规则可以制作' /opt/poky/1.3/sysroots/armv5te-poky-linux-gnueabi/usr/lib/libboost_thread.so' ;需要通过' test'。停止

我尝试用两种不同的方法设置CMake工具链,结果是一样的。

Method1(简单):

###################################################################################
# Cross compile using Poky prebuilt toolchains, you need to source the
# environment first:
#
# $ rm -fr build; mkdir build; cd build
# $ source /opt/poky/1.3/environment-setup-armv5te-poky-linux-gnueabi
# $ cmake -DCMAKE_TOOLCHAIN_FILE=Toolchains/arm-poky-linux-gnueabi-simple.cmake
# $ make
###################################################################################

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_FIND_ROOT_PATH $ENV{OECORE_TARGET_SYSROOT})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

方法2:

# this one is important
SET( CMAKE_SYSTEM_NAME Linux )

# this one not so much
SET( CMAKE_SYSTEM_VERSION 1 )

# specify the cross compiler
SET( CROSS_COMPILER_PATH /opt/poky/1.3/sysroots/x86_64-pokysdk-linux/usr/bin/armv5te-poky-linux-gnueabi )
SET( C_CROSS_COMPILER arm-poky-linux-gnueabi- )

FIND_PROGRAM( CCACHE ccache )
IF( CCACHE )
    SET( CMAKE_C_COMPILER "${CCACHE}" "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}gcc" )
    SET( CMAKE_CXX_COMPILER "${CCACHE}" "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}g++" )
ELSE( CCACHE )
    SET( CMAKE_C_COMPILER "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}gcc" "" )
    SET( CMAKE_CXX_COMPILER "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}g++" "" )
ENDIF(CCACHE)

SET( CMAKE_RANLIB "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}ranlib" )
SET( CMAKE_AR "${CROSS_COMPILER_PATH}/${C_CROSS_COMPILER}ar" )

SET( CMAKE_COMPILER_IS_GNUCC 1 )
SET( CMAKE_COMPILER_IS_GNUCXX 1 )

INCLUDE_DIRECTORIES( BEFORE SYSTEM /opt/poky/1.3/sysroots/armv5te-poky-linux-gnueabi/usr/include )
LINK_DIRECTORIES( /opt/poky/1.3/sysroots/armv5te-poky-linux-gnueabi/usr/lib )

# where is the target environment 
SET( CMAKE_FIND_ROOT_PATH /opt/poky/1.3/sysroots/armv5te-poky-linux-gnueabi/ )

#search for libraries and header files only in the target environment
SET( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
SET( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )

# search for programs in the build host directories
SET( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )

我的CMake项目是:

cmake_minimum_required( VERSION 2.8.9 )

project ( TEST ) 

set( BOOST_COMPONENTS thread )

find_package( Boost COMPONENTS ${BOOST_COMPONENTS} )

add_executable( test test.cpp ) 
target_link_libraries( test ${Boost_THREAD_LIBRARY} )

我用于测试的cpp代码:

#include <boost/thread/thread.hpp>
#include <iostream>

void hello()
{
    std::cout << "Hello!" << std::endl;
}

int main()
{
    boost::thread thrd( &hello );
    thrd.join();
    return 0;
}

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我的问题的解决方案:

解决方法1:

###################################################################################
#
# Cross compile using Poky prebuilt toolchains, you need to source the
# environment first:
#
# $ rm -fr build; mkdir build; cd build
# $ source /opt/poky/1.3/environment-setup-armv5te-poky-linux-gnueabi
# $ cmake -DCMAKE_TOOLCHAIN_FILE=Toolchains/arm-poky-linux-gnueabi-simple.cmake
# $ make
#
###################################################################################

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_FIND_ROOT_PATH /home/sylvain/Share/dev/Dev2M_CMakeCrossCompile/temp/boost $ENV{OECORE_TARGET_SYSROOT})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

我的CMake项目:

cmake_minimum_required( VERSION 2.8.9 )

project ( TEST ) 

set( BOOST_COMPONENTS atomic system thread )

#Very useful to find the source of the problem
#set( Boost_DEBUG on )

#Set the version of gcc in my case that is the source of my problems otherwise the module defines the gcc version of my ubuntu PC
set( Boost_COMPILER "-gcc" )

set( BOOST_ROOT /home/sylvain/Share/dev/Dev2M_CMakeCrossCompile/temp/boost )

find_package( Boost COMPONENTS ${BOOST_COMPONENTS} true )

set( MY_BOOST_LIBRARIES
    ${Boost_ATOMIC_LIBRARY}
    ${Boost_SYSTEM_LIBRARY}
    ${Boost_THREAD_LIBRARY}
)

add_executable( test test.cpp ) 
target_link_libraries( test ${MY_BOOST_LIBRARIES} )

答案 1 :(得分:0)

Boost通常如下链接:

FIND_PACKAGE( Boost COMPONENTS ${TheLibs} REQUIRED )    
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

add_executable(test test.cpp)
target_link_libraries( test ${Boost_LIBRARIES} )

澄清: 消息“无规则”通常意味着目标文件不存在。大多数时候我会手动检查文件。如果文件存在但仍然出现错误,您可以尝试通过设置链接器标志手动链接 e.g:

SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -l${PATH_TO_LIBRARY}")

如果它仍然不起作用,则使用Verbose标志调用make(通常为VERBOSE = 1)并查看链接步骤是否正确执行。


链接外部库的一般方法是将其添加为自己的目标。优点是您可以将其重用于同一项目中的多个可执行文件或库。你不会将它用于提升,因为它们通常会为你处理。

add_library( libABCDEFG SHARED/STATIC IMPORTED )
set_target_properties( mylib PROPERTIES IMPORTED_LOCATION ${PathToLibrary} )
TARGET_LINK_LIBRARIES(test libABCDEFG)

使用工具链进行CMake使用的另一个提示:

工具链仅包含在项目的第一次创建中(除非您另外包含它)。如果更改工具链中的任何设置,则必须删除CMake Cache并重新执行第一个CMake调用。