简体   繁体   English

为arm64交叉编译googletest

[英]cross-compiling googletest for arm64

I have some C code for a library that I'd like to test using googletest. 我有一些要使用googletest测试的库的C代码。 I intend to build the library for multiple architectures including x86, x86-64, ARM and ARM64. 我打算为多种架构构建库,包括x86,x86-64,ARM和ARM64。

As the library would export some functionality that is architecture specific, I'd like to test the library for each architecture. 由于该库将导出某些特定于体系结构的功能,因此我想针对每种体系结构测试该库。

In order to test the ARM64 build of the library, I'm trying to compile googletest for ARM64. 为了测试库的ARM64构建,我试图为ARM64编译googletest。

googletest uses cmake. googletest使用cmake。 I tried using the following cmake script to indicate that I want to cross compile google test. 我尝试使用以下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

I get the following output :- 我得到以下输出:-

-- 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".

Why does this error occur? 为什么会发生此错误? How can I fix this? 我怎样才能解决这个问题?

I've figured it out. 我知道了。 Adding the following line fixes the problem. 添加以下行可解决此问题。

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

cat googlemock/toolchain-arm-linux-gnueabihf.cmake 猫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)

Command to run cmake 运行cmake的命令

  • /googletest/googletest$ cmake ./ -DCMAKE_TOOLCHAIN_FILE=./toolchain-arm-linux-gnueabihf.cmake / googletest / googletest $cmake。/-DCMAKE_TOOLCHAIN_FILE =。/ toolchain-arm-linux-gnueabihf.cmake
  • /googletest/googletest$ make / googletest / googletest $ make

command to test the executable type 命令以测试可执行文件类型

  • ~/googletest/googletest$ readelf -a -W libgtest_main.a | 〜/ googletest / googletest $ readelf -a -W libgtest_main.a | more 更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM