我有一些要使用googletest测试的库的C代码。我打算为多种架构构建库,包括x86,x86-64,ARM和ARM64。

由于该库将导出某些特定于体系结构的功能,因此我想针对每种体系结构测试该库。

为了测试库的ARM64构建,我尝试为ARM64编译googletest。

googletest使用cmake。我尝试使用以下cmake脚本来表明我想交叉编译google测试。

$ cat toolchain-arm64.cmake
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_CROSSCOMPILING TRUE)

set(CMAKE_CXX_COMPILER /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)

set(CMAKE_C_COMPILER /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)

set(CMAKE_FIND_ROOT_PATH /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)


$ mkdir build && cd build
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../toolchain-arm64.cmake

我得到以下输出:-
-- The CXX compiler identification is GNU 6.1.1
-- The C compiler identification is GNU 6.1.1
-- Check for working CXX compiler: /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /home/asdf/repos/toolchains/gcc-linaro-6.1.1-2016.08-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.12")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
CMake Error: TRY_RUN() invoked in cross-compiling mode, please set the following cache variables appropriately:
   THREADS_PTHREAD_ARG (advanced)
For details see /home/asdf/repos/misc/googletest/googletest/build/TryRunResults.cmake
-- Check if compiler accepts -pthread - no
-- Found Threads: TRUE
-- Configuring incomplete, errors occurred!
See also "/home/asdf/repos/misc/googletest/googletest/build/CMakeFiles/CMakeOutput.log".
See also "/home/asdf/repos/misc/googletest/googletest/build/CMakeFiles/CMakeError.log".

为什么会发生此错误?我怎样才能解决这个问题?

最佳答案

猫googlemock / toolchain-arm-linux-gnueabihf.cmake

# Target system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_CROSSCOMPILING TRUE)

set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf)

# Cross compiler
SET(CMAKE_C_COMPILER   arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
set(CMAKE_LIBRARY_ARCHITECTURE arm-linux-gnueabihf)

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

# Libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(THREADS_PTHREAD_ARG "0" CACHE STRING "Result from TRY_RUN" FORCE)

运行cmake的命令
  • / googletest / googletest $ cmake ./ -DCMAKE_TOOLCHAIN_FILE =。/ toolchain-arm-linux-gnueabihf.cmake
  • / googletest / googletest $ make

  • 命令以测试可执行文件类型
  • 〜/ googletest / googletest $ readelf -a -W libgtest_main.a |更多
  • 关于c++ - 为arm64交叉编译googletest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46916611/

    10-10 18:16