CMake在使用android-cmake工具链文件时找不到Boost库

dsf9zpds  于 11个月前  发布在  Android
关注(0)|答案(5)|浏览(239)

我正在尝试为使用Boost库的代码创建一个CMake项目。我想为Android构建它,我使用这个项目作为参考:https://code.google.com/p/android-cmake/
我想使用我以前为Android构建的Boost静态库,而不是将它们作为项目的一部分进行编译。
这是我的CMakeLists.txt,用于使用Boost的项目部分

#BOOST

set(BOOST_ROOT /home/neb/workspace/SDLActivityCMAKE/jni/boost)

set(Boost_INCLUDE_DIR /home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53)
set(BOOST_INCLUDEDIR /home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53)
set(Boost_LIBRARY_DIR /home/neb/workspace/SDLActivityCMAKE/jni/boost/lib)
set(BOOST_LIBRARYDIR /home/neb/workspace/SDLActivityCMAKE/jni/boost/lib)

set(Boost_USE_STATIC_LIBS ON)
set(USE_STATIC_BOOST ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(BOOST_COMPILER gcc)

find_package(Boost COMPONENTS "thread-gcc-mt-1_53" "date_time-gcc-mt-1_53" REQUIRED)

include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
#BOOSTEND

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

字符串
当我运行常规的cmake命令(cmake ..来自android项目中jni文件夹的子目录)时,没有问题,cmake会找到库。当我使用上面提到的项目提供的android.toolchain.cmake时,我得到了这个(我使用-DBoost_DEBUG选项)。

-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:566 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:568 ] Boost_USE_MULTITHREADED = TRUE
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:570 ] Boost_USE_STATIC_LIBS = ON
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:572 ] Boost_USE_STATIC_RUNTIME = ON
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:574 ] Boost_ADDITIONAL_VERSIONS = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:576 ] Boost_NO_SYSTEM_PATHS = 
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:644 ] Declared as CMake or Environmental Variables:
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:646 ]   BOOST_ROOT = /home/neb/workspace/SDLActivityCMAKE/jni/boost
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:648 ]   BOOST_INCLUDEDIR = /home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:650 ]   BOOST_LIBRARYDIR = /home/neb/workspace/SDLActivityCMAKE/jni/boost/lib
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:652 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:734 ] location of version.hpp: /home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53/boost/version.hpp
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:754 ] version.hpp reveals boost 1.53.0
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:786 ] guessed _boost_COMPILER = -gcc46
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:796 ] _boost_MULTITHREADED = -mt
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:839 ] _boost_RELEASE_ABI_TAG = -s
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:841 ] _boost_DEBUG_ABI_TAG = -sd
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:884 ] _boost_LIBRARY_SEARCH_DIRS = /home/neb/workspace/SDLActivityCMAKE/jni/boost/lib;/home/neb/workspace/SDLActivityCMAKE/jni/boost/lib;/home/neb/workspace/SDLActivityCMAKE/jni/boost/stage/lib;/home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53/lib;/home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53/../lib;/home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53/stage/lib;C:/boost/lib;C:/boost;/boost/boost_1_53_0/lib;/boost/boost_1_53/lib;/boost/lib;/boost;/sw/local/lib
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:962 ] Searching for THREAD-GCC-MT-1_53_LIBRARY_RELEASE: boost_thread-gcc-mt-1_53-gcc46-mt-s-1_53;boost_thread-gcc-mt-1_53-gcc46-mt-s;boost_thread-gcc-mt-1_53-mt-s-1_53;boost_thread-gcc-mt-1_53-mt-s;boost_thread-gcc-mt-1_53
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:994 ] Searching for THREAD-GCC-MT-1_53_LIBRARY_DEBUG: boost_thread-gcc-mt-1_53-gcc46-mt-sd-1_53;boost_thread-gcc-mt-1_53-gcc46-mt-sd;boost_thread-gcc-mt-1_53-mt-sd-1_53;boost_thread-gcc-mt-1_53-mt-sd;boost_thread-gcc-mt-1_53-mt;boost_thread-gcc-mt-1_53
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:962 ] Searching for DATE_TIME-GCC-MT-1_53_LIBRARY_RELEASE: boost_date_time-gcc-mt-1_53-gcc46-mt-s-1_53;boost_date_time-gcc-mt-1_53-gcc46-mt-s;boost_date_time-gcc-mt-1_53-mt-s-1_53;boost_date_time-gcc-mt-1_53-mt-s;boost_date_time-gcc-mt-1_53
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:994 ] Searching for DATE_TIME-GCC-MT-1_53_LIBRARY_DEBUG: boost_date_time-gcc-mt-1_53-gcc46-mt-sd-1_53;boost_date_time-gcc-mt-1_53-gcc46-mt-sd;boost_date_time-gcc-mt-1_53-mt-sd-1_53;boost_date_time-gcc-mt-1_53-mt-sd;boost_date_time-gcc-mt-1_53-mt;boost_date_time-gcc-mt-1_53
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:1108 ] Boost_FOUND = FALSE
CMake Error at /usr/share/cmake-2.8/Modules/FindBoost.cmake:1195 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.53.0

  Boost include path:
  /home/neb/workspace/SDLActivityCMAKE/jni/boost/include/boost-1_53

  The following Boost libraries could not be found:

          boost_thread-gcc-mt-1_53
          boost_date_time-gcc-mt-1_53

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
Call Stack (most recent call first):
  src/CMakeLists.txt:15 (find_package)

-- Configuring incomplete, errors occurred!


所以我看到CMake正在这个目录中搜索库:/home/neb/workspace/SDLActivityCMAKE/jni/boost/lib,其中有编译的静态库:

neb@neb-VirtualBox:~/workspace/SDLActivityCMAKE/jni/boost/lib$ ls -l
total 24440
-rwxrwxrwx 1 neb neb   557190 sie 27 12:36 libboost_date_time-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb  1348474 sie 27 12:36 libboost_filesystem-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb   827216 sie 27 12:36 libboost_iostreams-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb  6813972 sie 27 12:36 libboost_program_options-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb 12927086 sie 27 12:37 libboost_regex-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb  1197696 sie 27 12:37 libboost_signals-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb   159462 sie 27 12:36 libboost_system-gcc-mt-1_53.a
-rwxrwxrwx 1 neb neb  1177994 sie 27 12:37 libboost_thread-gcc-mt-1_53.a`


我设置了所有指向BOOST目录的变量,我设置了变量说Boost库应该是静态的,但它仍然找不到它们。我还能做什么?

8cdiaqws

8cdiaqws1#

使用CMake查找Boost

让我先说让CMake找到Boost可能很困难,因为FindBoost.cmake中的自动化会咬你,CMake的开发者KitWare有一些我遇到过的任何流行项目中最糟糕的文档。如果FindBoost.cmake失败了,你必须遍历它的源代码来调试它。

交叉编译Boost

安装交叉编译的boost的目录看起来像这样,假设是1.62.0

boost-arm-build/include
boost-arm-build/include/boost-1_62
boost-arm-build/lib

字符串
从Project-Build目录调用CMake

cmake \
   -DBoost_NO_SYSTEM_PATHS=TRUE \
   -DBoost_ADDITIONAL_VERSIONS="1.62" \
   -DCMAKE_TOOLCHAIN_FILE=../android.toolchain \
   -DBOOST_ROOT="../boost-arm-build" \
   ../Project/


-DBoost_ADDITIONAL_VERSIONS- CMake使用硬编码的字符串列表查找Boost,如果您的CMake比您正在构建的Boost版本旧,它将不知道在包含目录中的何处查找,因此您需要将其传递沿着。尽管版本实际上是1.62.0,boost把includes放在boost-1_62中,所以我用1.62代替。在FindBoost.cmake中,你会发现每个版本都被添加了两次,就像在“1.62.0”“1.62”中一样。
您还应该检查您构建的库的文件名,对于我的示例,让我们看看system

libboost_system-gcc-mt-1_62.a
libboost_system-gcc-mt-1_62.so    # Symbolic link to libboost_system-gcc-mt-1_62.so.1.62.0
libboost_system-gcc-mt-1_62.so.1.62.0
libboost_system-gcc-mt-d-1_62.a
libboost_system-gcc-mt-d-1_62.so  # Symbolic link to libboost_system-gcc-mt-d-1_62.so.1.62.0
libboost_system-gcc-mt-d-1_62.so.1.62.0

  • gcc-使用的编译器
  • mt-多线程
  • d-调试
  • 1_62-boost的版本

如果你看到的是libboost_system.a,这意味着你没有用--with-layout=versioned构建boost,你应该看看我关于使用NDK构建boost的SO答案。如果你看到的是clang而不是gcc,那说明你已经用clang构建了boost,但是你可能需要使用Boost_COMPILER标志通知CMake clang存在。

-DBoost_COMPILER="-clang"

android.toolchain文件

set(CMAKE_SYSTEM_NAME Android)

set(CROSS_COMPILER_DIR /opt/ndk-R13-standalone)
set(CMAKE_C_COMPILER ${CROSS_COMPILER_DIR}/bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER_DIR}/bin/arm-linux-androideabi-g++)

set(ANDROID_NATIVE_API_LEVEL android-19)
set(ANDROID_ABI armeabi-v7a)
set(ANDROID_STL gnustl_static)

set(CMAKE_SYSROOT ${CROSS_COMPILER_DIR}/sysroot)
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})

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

cwtwac6a

cwtwac6a2#

我遇到了同样的问题。使用find_host_package(Boost COMPONENTS ..)而不是find_package(Boost COMPONENTS ..)解决了我的问题。
(图:http://code.google.com/p/android-cmake/issues/detail?id=16

cclgggtu

cclgggtu3#

这里有几个问题。
看起来你的boost库在构建时没有使用runtime-link=static,也就是说它们没有静态链接到C++标准库。如果是,它们的名称后缀应该包含s(请参阅boost文档中的库命名)
另一个问题是,CMake的FindBoost module期望COMPONENTS参数的形式为“date_time”,“thread”,而不是您提供的全名。
另一个更小的问题是,你不需要设置Boost_INCLUDE_DIRBoost_LIBRARY_DIR;如果成功的话,这些都是由CMake模块设置的。设置它们应该不会引起任何问题,但是,这只是不必要的混乱。
此外,对于BOOST_ROOTBOOST_INCLUDEDIRBOOST_LIBRARYDIR,通常可以通过命令行使用-D选项进行设置,或者设置为环境变量,以避免将特定于计算机的路径硬编码到CMakeLists.txt中。
最后一点是,你应该删除link_directories调用。它自己的文档不鼓励使用它,而且它也不需要,因为你已经在target_link_libraries调用中传递了boost库的完整路径。
所以,最终版本应该更像是:

#BOOST
set(Boost_USE_STATIC_LIBS ON)
set(USE_STATIC_BOOST ON)  # Not sure if you really need this later.  If not, delete.
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_COMPILER -gcc)

find_package(Boost COMPONENTS thread date_time REQUIRED)

include_directories(${Boost_INCLUDE_DIRS})
#BOOSTEND

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

字符串
你可以像这样调用CMake:

cmake . -DBOOST_ROOT=<path to root> -DBOOST_INCLUDEDIR=... -DBOOST_LIBRARYDIR=...

wrrgggsh

wrrgggsh4#

我也遇到了同样的问题,我使用Boost for android仓库构建了boost,但我只得到了静态库。
我不认为你真的需要使用cmake的find_package。

add_library(boost_system-gcc-mt-1_55  STATIC IMPORTED  GLOBAL)
set_target_properties( # Specifies the target library.
                       boost_system-gcc-mt-1_55

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       ${CMAKE_SOURCE_DIR}/src/main/jni/boost/lib/libboost_system-gcc-mt-1_55.a )
SET(Boost_LIBRARY_DIR ${Boost_LIBRARY_DIR} boost_system-gcc-mt-1_55)
# You can make this include just once
include_directories("src/main/jni/boost/include/boost-1_55")
target_link_libraries(${MyNDKSampleLibs}
                   ${Boost_LIBRARY_DIR}
                   ${log-lib}

字符串
这里我唯一能提到的是不要忘记cmake var ${CMAKE_SOURCE_DIR} in set_target_properties函数。

c86crjj0

c86crjj05#

分享以帮助那些想要在Android上使用Boost的人(使用Android Studio),并提供了几个技巧来有效地调试cmakeFindBoost脚本。
1.克隆存储库.
git clone https://github.com/moritz-wundke/Boost-for-Android.git

  1. build boost with(check readme for windows)
    ./build-android.sh $(NDK_ROOT)
    1.添加指向路径的buuild.gradle变量,类似于:
externalNativeBuild {
   cmake {
       cppFlags '-std=c++17' // not needed but common
       DBOOST_PATH="+"/Users/bloom/sw_develop/Libraries/Boost-for-Android/build/out/"
   }
}

字符串
1.在Cmakelist上添加一个与此类似的部分:

set(CMAKE_FIND_DEBUG_MODE FALSE)
    set(Boost_DEBUG 0)
    set(CMAKE_SYSROOT_OLD ${CMAKE_SYSROOT})
    set(CMAKE_SYSROOT "")
    set(BOOST_ROOT ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI})
    message("LOOKING FOR BOOST ROOT IN " ${BOOST_ROOT})
    set(BOOST_INCLUDEDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/include/boost-1_82)
    set(BOOST_LIBRARYDIR ${BOOST_PATH}/${CMAKE_ANDROID_ARCH_ABI}/lib)
    set(Boost_LIB_PREFIX lib)

    set(Boost_ADDITIONAL_VERSIONS "1.82.0" "1.82")
    set(Boost_NO_SYSTEM_PATHS TRUE)
    set(Boost_COMPILER "-clang-darwin")
    set(Boost_USE_STATIC_LIBS        ON)
    set(Boost_USE_MULTITHREADED      ON)
    set(Boost_USE_STATIC_RUNTIME    OFF)
    find_package( Boost 1.82.0 COMPONENTS serialization )

    if(Boost_FOUND)
        message("Boost_FOUND is True")
        include_directories(${Boost_INCLUDE_DIRS})
    else()
        message(FATAL_ERROR "Boost_FOUND is False")
    endif()
    set(CMAKE_SYSROOT ${CMAKE_SYSROOT_OLD})

    #and after defining the target don't forget the line
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})


打开它对调试CMAKE_FIND_DEBUG_MODEBoost_DEBUG非常有用。第一个将显示搜索包含和库的路径。FindBoost查找公共头和库以确定是否找到boost。第二个打印FindBoost的调试信息。我不得不从脚本的这一行删除STATUS一词以使其工作:要打开findboost.cmake脚本,请使用set(Boost_ROOT "pippo")触发一个错误,然后单击错误消息上的链接。
我不得不使用一个技巧并设置set(CMAKE_SYSROOT ""),因为传递的路径被奇怪地附加到ndk路径上,这触发了notfound错误。这被报告给ndk google组,可能在未来被修复。
其余的设置都很有说明性。
别忘了给这个答案投票,然后点击订阅。啊,不,那是在youtube上:)

相关问题