1

I've to cross compile a 3rd party library which uses CMake.

In order to build something, I need to pass "-B path" flag to compiler.

foo.c :

void main(){}

Build command :

cross-gcc -B /path/to/somewhere -o foo.o foo.c

Without "-B path" option, the compiler simply does not work.

Hence I use the following lines in my cmake toolchain file :

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

But during the validation of compiler, CMake does not seem use the flags I set :

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- broken
    CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "cross-path/cross-gcc" is not able
  to compile a simple test program.

....
....

/cross-path/cross-gcc -o CMakeFiles/cmTryCompileExec.dir/testCCompiler.c.o -c CMakeFiles/CMakeTmp/testCCompiler.c

When I invoke CMake second time, cross-gcc passes the simple compilation test but this time cross-g++ fails :

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /cross-path/cross-g++
-- Check for working CXX compiler: /cross-path/cross-g++ -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake:45 (MESSAGE):
  The C++ compiler "/cross-path/cross-g++" is not
  able to compile a simple test program.

....
....

  /cross-path/cross-g++ -o CMakeFiles/cmTryCompileExec.dir/testCXXCompiler.cxx.o -c CMakeFiles/CMakeTmp/testCXXCompiler.cxx

And if I invoke CMake 3rd time, everything works fine with only a warning :

....
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_TOOLCHAIN_FILE

I'm using CMake 2.8.7 and aware that it is quite outdated but upgrade is not an option if this is the case.

Edit :

Toolchain file :

# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

# specify the cross compiler
SET(CMAKE_C_COMPILER   /cross-path/cross-gcc)
SET(CMAKE_CXX_COMPILER /cross-path/cross-g++)
Murat Şeker
  • 1,651
  • 1
  • 16
  • 29
  • Can you show all the toolchain file that you used? – fedepad Jan 07 '17 at 22:44
  • Sure, I edited the question. – Murat Şeker Jan 07 '17 at 22:47
  • I wonder if this is a case in which you should use the `CMAKE_FORCE_XXX_COMPILER()` macros, as you need to add those flags...have a look here [cmake_cross_compiling](http://www.vtk.org/Wiki/CMake_Cross_Compiling) and look for `INCLUDE(CMakeForceCompiler)`. – fedepad Jan 07 '17 at 22:55
  • Exactly :) I used the method mentioned in the following link : http://stackoverflow.com/questions/6476203/how-can-i-make-cmake-use-specific-compiler-and-flags-when-final-compilation-stag Thanks very much for the tip. I'm not sure if you should add it as an answer or the question should be closed as duplicate. – Murat Şeker Jan 07 '17 at 23:15
  • AH ok, I didn't see that SO, I just read the documentation I linked.... I will include these links (docs and this SO you mentioned) in the answer. – fedepad Jan 07 '17 at 23:18

1 Answers1

3

It looks that the problem of picking up the flags is very similar to one already discussed in SO in the past:
CMake cross-compiling: C flags from toolchain file ignored
Try the proposed solution, i.e. replace:

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

by

SET(CMAKE_C_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)

Try also to check if replacing your toolchain file with a new one that uses INCLUDE(CMakeForceCompiler) as mentioned in
http://www.vtk.org/Wiki/CMake_Cross_Compiling and
How can I make Cmake use specific compiler and flags when final compilation stage instead of detection? and
Cmake cross compile flags

INCLUDE(CMakeForceCompiler)
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

# specify the cross compiler
CMAKE_FORCE_C_COMPILER(/cross-path/cross-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(/cross-path/cross-g++ GNU)  
...

has the same effect as the first hack. The second option seems a cleaner solution.

Community
  • 1
  • 1
fedepad
  • 4,509
  • 1
  • 13
  • 27