Engineer
Engineer

Reputation: 8847

CMake cross compile - separate invocation per platform?

I'm new to CMake, which I'm using to cross-compile.

I'm using clang and wclang together, under Debian, to compile for Linux and Windows respectively.

My question - since I'm new to CMake's cross compile process - is do I:

(a) switch between compilers somewhere during CMakeLists.txt,

(b) run cmake only once, but make install for each platform, or

(c) run cmake and make install once for each platform, e.g.

$ export CC=/usr/bin/clang $ cmake .. $ make install

$ export CC=/usr/bin/wclang $ cmake .. $ make install

(etc.)

?

Upvotes: 0

Views: 666

Answers (2)

DevSolar
DevSolar

Reputation: 70293

C), separate invocation per platform. I would also recommend clearing the binary directory between builds, or using a separate directory for each build if you want to preserve the build(s).

Settings are usually done in a toolchain file, not the command line (for reproducability):

$ cmake --help-variable CMAKE_TOOLCHAIN_FILE
CMAKE_TOOLCHAIN_FILE
--------------------

Path to toolchain file supplied to ``cmake(1)``.

This variable is specified on the command line when cross-compiling with CMake.
It is the path to a file which is read early in the CMake run and which specifies
locations for compilers and toolchain utilities, and other target platform and
compiler related information.

A simple toolchain file can look like this:

# Name of the target operating system
set( CMAKE_SYSTEM_NAME Windows )

# Which compilers to use
find_program( CMAKE_C_COMPILER   NAMES /opt/mxe/usr/bin/x86_64-w64-mingw32.static-gcc )
find_program( CMAKE_CXX_COMPILER NAMES x86_64-w64-mingw32.static-g++ )
find_program( CMAKE_RC_COMPILER  NAMES x86_64-w64-mingw32.static-windres )

# Where to look for resources
set( CMAKE_FIND_ROOT_PATH /opt/mxe/usr/x86_64-w64-mingw32.static/ )

# Adjust find_*() behavior:
# Headers and libs from the target environment,
# programs from the host environment.
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )

Some cross-compiling environments, like MXE, come with pre-made toolchain files and wrappers calling them. (MXE in particular, you run i686-w64-mingw32.static-cmake instead of standard cmake to configure your build.)

Upvotes: 2

Tsyvarev
Tsyvarev

Reputation: 65938

All steps (cmake - make - install) should be repeated for every target. That is, "c" in your list.


Some development environments, e.g. Visual Studio, support multi configuration, that is project can be configured once, and every target configuration requires just building. Such a way project can be compiled in Debug and Release mode, or for x86 and x86-64.

But Windows and Linux targets require distinct configuration step (cmake run) in any case.

Upvotes: 1

Related Questions