CMake: cross-compile GoogleTest

This post is about how to cross-compile GoogleTest in Eclipse.

Environment
  • Ubuntu 16.04 LTS
  • Eclipse Neon.3 Release 4.6.3
Install CMake
$ sudo apt-get install cmake
Checkout GoogleTest
$ git clone https://github.com/google/googletest.git
Configure ARM toolchain

How to install ARM toolchain see Raspberry Pi: C++ cross-compiling.

  • Create in the root of GoogleTest folder  toolchain-arm-linux-gnueabihf.cmake file with the following content:
# Target system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

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

# Cross compiler
SET(CMAKE_C_COMPILER   /usr/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)

# 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)
  • Configure CMake:
$ cmake -DCMAKE_TOOLCHAIN_FILE=./toolchain-arm-linux-gnueabihf.cmake
Create Eclipse project
$ cmake -G "Eclipse CDT4 - Unix Makefiles"

Import the project in Eclipse:

  • File > Import
  • Select:
    • General
    • Existing Project into Workspace
    • Next
  • Select project
    • Select root directory > Browse (to the GoogleTest folder)
    • Finish
Compile GoogleTest

Eclipse:

Project > Build project

Check library type
$ readelf -a -W ./googlemock/gtest/libgtest.a | more

File: ./googlemock/gtest/libgtest.a(gtest-all.cc.o)
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 03 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          1328016 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         10368
  Section header string table index: 10365