跨平台Poco库链接问题

时间:2019-05-06 11:50:18

标签: c++ cmake poco

我使用特殊的arm工具链文件并使用脚本在带有cmake的Ubuntu amd平台上编译了Poco:

#!/bin/sh

cd cmake_build
cmake \
    -G "Unix Makefiles" \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_TOOLCHAIN_FILE=~/RPi/Toolchain-RaspberryPi.cmake \
    -DCMAKE_INSTALL_PREFIX=~/RPi/rootfs/usr/local \
    ..

make -j4

工具链文件:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

# Specify the cross compiler
SET(CMAKE_C_COMPILER $ENV{HOME}/RPi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/RPi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)

# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/RPi/rootfs)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")

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

# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

我用make install将poco安装到了预配置的文件夹~/RPi/rootfs/usr/local/中。

我将所有Poco文件复制到Raspberry文件系统-liblib/cmakeinclude

我的cmake测试项目很少:

cmake_minimum_required (VERSION 3.7)

# Name of the project
project(raspi_ld_test)
message("BUILD ENVIRONMENT SET TO -${BUILD_ENV}-")

if (${BUILD_ENV} STREQUAL "UBUNTU")
  set(POCO_PATH "~/RPi/rootfs/usr/local")
elseif(${BUILD_ENV} STREQUAL "RASPI")
  set(POCO_PATH "/usr/local")
else()
  message(FATAL_ERROR "Undefined environment")
endif()

find_package(Poco REQUIRED Foundation Util HINTS ${POCO_PATH} NO_CMAKE_FIND_ROOT_PATH)
find_package(Threads REQUIRED)

# Alert the user if we do not find it
if(NOT WPI_LIB)
    message(FATAL_ERROR "wiringPi library not found")
endif()

# Add the local ‘include’ directory and the wiringPi directory to grab headers
include_directories("${POCO_PATH}/include")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)

# Add all the *.cpp files in our source directory to our executable output
file(
  GLOB_RECURSE 
  SOURCES 
  "src/*.cpp"
)

add_executable(raspi_ld_test ${SOURCES})

link_directories(${POCO_PATH}/lib)

# Link the pre-compiled wiringPi library to the executable we just declared 
target_link_libraries(
  raspi_ld_test
  ${CMAKE_THREAD_LIBS_INIT}
  Poco::Foundation
)

我只有一个src / main.cpp:

#include "Poco/BasicEvent.h"
#include "Poco/Delegate.h"
#include <iostream>

using Poco::BasicEvent;
using Poco::Delegate;

class Source
{
public:
    BasicEvent<int> theEvent;

    void fireEvent(int n)
    {
        theEvent(this, n);
    }
};

class Target
{
public:
    void onEvent(const void* pSender, int& arg)
    {
        std::cout << "onEvent: " << arg << std::endl;
    }
};

int main(int argc, char** argv)
{
    Source source;
    Target target;

    source.theEvent += Delegate<Target, int>(
        &target, &Target::onEvent);

    source.fireEvent(42);

    source.theEvent -= Delegate<Target, int>(
        &target, &Target::onEvent);

    return 0;
}

现在,我可以在Ubuntu上成功编译项目了。如果我将raspi_ld_test复制到Raspberry,则可以成功运行它。但!我无法在Raspberry上构建项目,但出现以下错误:

  

CMakeFiles / raspi_ld_test.dir / src / main.cpp.o:在函数中   Poco::MutexImpl::lockImpl()': /usr/local/include/Poco/Mutex_POSIX.h:60: undefined reference to Poco :: SystemException :: SystemException(std :: __ cxx11 :: basic_string,std :: allocator> const&,int)'   CMakeFiles / raspi_ld_test.dir / src / main.cpp.o:在函数中   Poco::MutexImpl::unlockImpl()': /usr/local/include/Poco/Mutex_POSIX.h:79: undefined reference to Poco :: SystemException :: SystemException(std :: __ cxx11 :: basic_string,std :: allocator> const&,int)'collect2:   错误:ld返回1退出状态   CMakeFiles / raspi_ld_test.dir / build.make:99:目标配方   'raspi_ld_test'失败make [2]: * [raspi_ld_test]错误1   CMakeFiles / Makefile2:67:目标配方   'CMakeFiles / raspi_ld_test.dir / all'失败[1]:*   [CMakeFiles / raspi_ld_test.dir / all]错误2 Makefile:83:配方   目标“全部”失败:*** [全部]错误2

我看到库成功链接:

pi@raspberrypi:/usr/local/lib $ ldd libPocoFoundationd.so
    linux-vdso.so.1 (0x7ef90000)
    /usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76bfe000)
    libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x76bd5000)
    libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0x76bc2000)
    librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x76bab000)
    libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x76a63000)
    libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x769e4000)
    libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x769b7000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x76878000)
    /lib/ld-linux-armhf.so.3 (0x76f03000)

更新

这是在amd上编译后的ldd输出,并复制到arm可执行文件中:

pi@raspberrypi:~/raspi_ld_test/build $ ldd raspi_ld_test 
    linux-vdso.so.1 (0x7efd0000)
    /usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76f0f000)
    libwiringPi.so => /usr/lib/libwiringPi.so (0x76ef1000)
    libPocoUtild.so.60 => /usr/local/lib/libPocoUtild.so.60 (0x76e35000)
    libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x76e0c000)
    libPocoXMLd.so.60 => /usr/local/lib/libPocoXMLd.so.60 (0x76d1f000)
    libPocoJSONd.so.60 => /usr/local/lib/libPocoJSONd.so.60 (0x76c8b000)
    libPocoFoundationd.so.60 => /usr/local/lib/libPocoFoundationd.so.60 (0x7699c000)
    libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0x76989000)
    librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x76972000)
    libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x7682a000)
    libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x767ab000)
    libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x7677e000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x7663f000)
    libcrypt.so.1 => /lib/arm-linux-gnueabihf/libcrypt.so.1 (0x76600000)
    /lib/ld-linux-armhf.so.3 (0x76f25000)

这是运行的输出:

pi@raspberrypi:~/raspi_ld_test/build $ ./raspi_ld_test 
onEvent: 42

问题是:如何可能在amd上为arm编译的Poco库对于在arm上运行时链接是好的,但对于在arm上链接的构建时间却是不好的,以及我为构建项目需要做什么胳膊上吗?

更新

所以我从评论中看到,不可能使用不同版本的g ++在arm工具链上使用amd编译的libs在arm上编译项目...
我有以下版本:

Ubuntu上的ARM工具链:

olga@olga-MS-7758:~/raspi_ld_test$ ~/RPi/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ --version
arm-linux-gnueabihf-g++ (crosstool-NG crosstool-ng-1.22.0-88-g8460611) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Raspbian上的ARM工具链:

pi@raspberrypi:~/Poco/cmake $ g++ --version
g++ (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

所以我需要搜索为Ubuntu编译的BCM(树莓)6.3 g ++?

0 个答案:

没有答案