qq_41687281 2020-08-03 05:18 采纳率: 0%
浏览 355
已结题

交叉编译中cmake的路径搜索问题?

我在交叉编译ORBSLAM3, 准备用在树莓派4上面。
我想通过rsync 把树莓派的/usr, /lib 存在电脑ubuntu /xx/home/raspberrypi/rootfs/, 然后编译时候全部在这里面找所需要的库文件

小弟cmake不熟, 求大佬指点。

编译过程大致是这样:

1. 用 Toolchain-rpi.cmake 作用在 ORBSLAM3 文件顶层的CMakeLists.txt .

2. 在 CMakeLists.txt, 要用find_package() 找几个第三方库, 比如opencv,Pangolin。 在找pangolin, 这其中涉及到PangolinTargets.cmake,PangolinTargets-release.cmake

下面是顶层的CMakeLists.txt 和PangolinTargets.cmake 里面几个路径的输出

CMAKE_SYSROOT = /home/ethan/raspberrypi/rootfs 
CMAKE_PREFIX_PATH = ;/home/ethan/raspberrypi/rootfs/usr/local/lib;/home/ethan/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf 
_IMPORT_PREFIX = /home/ethan/raspberrypi/rootfs/usr/local

感觉情况是这样,find_package()找到的都是/home/ethan/raspberrypi/rootfs/下面的.cmake 文件,但最后具体到库文件时, 还是回到了/usr 下面。
举个例子。
PangolinTargets.cmake 有段是这样的

foreach(target ${_IMPORT_CHECK_TARGETS} )
  ## IMPORT_CHECK_TARGETS =  ##
  foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
    ## target = pangolin ##
    ## _IMPORT_CHECK_FILES_FOR_pangolin = /usr/local/lib/libpangolin.so ##
    ## file = /usr/local/lib/libpangolin.so ##

    if(NOT EXISTS "${file}" )
      message(FATAL_ERROR "The imported target \"${target}\" references the file
   \"${file}\"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
   \"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
    endif()
  endforeach()
  unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)

_IMPORT_CHECK_TARGETS, 以及 _IMPORT_CHECK_FILES_FOR_${target}

这些是在PangolinTargets-release.cmake 里面定义的。
如果没有改动的话, file = /usr/local/lib/libpangolin.so, 然后 NOT EXISTS "${file}" 就是true, 于是就会因为/usr下面没有该文件而报错

我不知道除了手动修改PangolinTargets-release.cmake 里面所有的路径,还有什么办法。 opencv 应该也存在相同的问题。 find之后显示版本为树莓派上的opencv3.4, 也就是
/home/ethan/raspberrypi/rootfs 下面的。 但具体到库文件, 比如core.hpp, 还是找的/usr 下面的

然后现在但凡涉及到stdio.h,

/usr/include/opencv2/core/core.hpp:48,

/opt/cross-pi-gcc/arm-linux-gnueabihf/include/c++/8.3.0/cstdio:42,

全部也是用的/usr/include/stdio.h。

我ubuntu 上面的没有bits/libio.h, 这里面编译每次用到stdio.h用到都会报fatal error: bits/libio.h: No such file or directory #include
但说来奇怪, 如果我自己写个待 的小程序, 并不会有问题。

我希望现在编译的时候全用的是/home/ethan/raspberrypi/rootfs/ 下面的文件
编译完了放到板子上, 找共享库之类的能在树莓派的 / 下面。

这里面感觉就是root 不一样, 但怎么写起来有点艰辛啊。。。

下面是PangolinTargets.cmake 的内容

# Generated by CMake

# message ("##CMAKE_SYSROOT = ${CMAKE_SYSROOT} ##")
# message ("##CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH} ##")

if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
   message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------

# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)

# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget pangolin)
  list(APPEND _expectedTargets ${_expectedTarget})
  if(NOT TARGET ${_expectedTarget})
    list(APPEND _targetsNotDefined ${_expectedTarget})
  endif()
  if(TARGET ${_expectedTarget})
    list(APPEND _targetsDefined ${_expectedTarget})
  endif()
endforeach()
if("${_targetsDefined}" STREQUAL "${_expectedTargets}")
  unset(_targetsDefined)
  unset(_targetsNotDefined)
  unset(_expectedTargets)
  set(CMAKE_IMPORT_FILE_VERSION)
  cmake_policy(POP)
  return()
endif()
if(NOT "${_targetsDefined}" STREQUAL "")
  message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n")
endif()
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)


# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
  set(_IMPORT_PREFIX "")
endif()

# message ("##_IMPORT_PREFIX = ${_IMPORT_PREFIX} ##}")
##_IMPORT_PREFIX = /home/ethan/raspberrypi/rootfs/usr/local ##

# Create imported target pangolin
add_library(pangolin SHARED IMPORTED)

set_target_properties(pangolin PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3;${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "rt;pthread;/usr/lib/arm-linux-gnueabihf/libGL.so;/usr/lib/arm-linux-gnueabihf/libGLU.so;/usr/lib/arm-linux-gnueabihf/libGLEW.so;/usr/lib/arm-linux-gnueabihf/libEGL.so;/usr/lib/arm-linux-gnueabihf/libSM.so;/usr/lib/arm-linux-gnueabihf/libICE.so;/usr/lib/arm-linux-gnueabihf/libX11.so;/usr/lib/arm-linux-gnueabihf/libXext.so;rt;pthread;/usr/lib/arm-linux-gnueabihf/libdc1394.so;/usr/lib/arm-linux-gnueabihf/libpng.so;/usr/lib/arm-linux-gnueabihf/libz.so;/usr/lib/arm-linux-gnueabihf/libjpeg.so;/usr/lib/arm-linux-gnueabihf/libtiff.so;/usr/lib/arm-linux-gnueabihf/libIlmImf.so"
  INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3"
)

if(CMAKE_VERSION VERSION_LESS 2.8.12)
  message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
endif()

# Load information for each installed configuration.

# message("## CMAKE_CURRENT_LIST_FILE =  ${CMAKE_CURRENT_LIST_FILE}## ")
## CMAKE_CURRENT_LIST_FILE =  /home/ethan/raspberrypi/rootfs/usr/local/lib/cmake/Pangolin/PangolinTargets.cmake ##

get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)#Sets <_DIR> to a component of <${CMAKE_CURRENT_LIST_FILE}>, where <mode> is PATH:

# message("## _DIR = ${_DIR}  ##")
## _DIR = /home/ethan/raspberrypi/rootfs/usr/local/lib/cmake/Pangolin  ##

file(GLOB CONFIG_FILES "${_DIR}/PangolinTargets-*.cmake")
# Generate a list of files that match the <"${_DIR}/PangolinTargets-*.cmake"> and store it into the <CONFIG_FILES>

# message("## CONFIG_FILES = ${CONFIG_FILES}##")
## CONFIG_FILES = /home/ethan/raspberrypi/rootfs/usr/local/lib/cmake/Pangolin/PangolinTargets-release.cmake##

foreach(f ${CONFIG_FILES})
  include(${f})
endforeach()

# Cleanup temporary variables.
set(_IMPORT_PREFIX)

# message("## IMPORT_CHECK_TARGETS = ${IMPORT_CHECK_TARGETS} ##")
## IMPORT_CHECK_TARGETS =  ##

# Loop over all imported files and verify that they actually exist
foreach(target ${_IMPORT_CHECK_TARGETS} )
  # message("## IMPORT_CHECK_TARGETS = ${IMPORT_CHECK_TARGETS} ##")
  ## IMPORT_CHECK_TARGETS =  ##
  foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
    message("## target = ${target} ##")
    ## target = pangolin ##
    message("## _IMPORT_CHECK_FILES_FOR_${target} = ${_IMPORT_CHECK_FILES_FOR_${target}} ##")
    ## _IMPORT_CHECK_FILES_FOR_pangolin = /usr/local/lib/libpangolin.so ##
    message("## file = ${file} ##")
    ## file = /usr/local/lib/libpangolin.so ##

    if(NOT EXISTS "${file}" )
      message(FATAL_ERROR "The imported target \"${target}\" references the file
   \"${file}\"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
   \"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
    endif()
  endforeach()
  unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)

# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.

# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)


  • 写回答

2条回答 默认 最新

  • bobhuang 2020-08-03 11:27
    关注

    find package的通常写法, 以Caffe为例:

    find_package(Caffe REQUIRED)
    
    if (NOT Caffe_FOUND)
        message(FATAL_ERROR "Caffe Not Found!")
    endif (NOT Caffe_FOUND)
    
    include_directories(${Caffe_INCLUDE_DIRS})
    
    add_executable(useSSD ssd_detect.cpp)
    target_link_libraries(useSSD ${Caffe_LIBRARIES})
    

    所以find_package之后,主要是生成了几个cmake变量(xxx_FOUND, xxx_INCLUDE_DIRS, xxx_LIBRARIES).你需要判断是否找到,如果找到,需要加上include_directories和target_link_libraries。

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试