CPP/CMake/CrossCompilation
Aller à la navigation
Aller à la recherche
Sources
On va cette fois se servir de deux fichiers sources, d’un fichier en-tête et d’un fichier cmake pour construire notre binaire cppdata:
- Le fichier main.cpp
- Le fichier data.cpp, qui est l’implémentation de la class Data
- Le fichier en-tête data.hpp qui déclare notre class Data
#include <iostream>
#include "data.hpp"
using namespace std;
int main()
{
Data data;
data.set( 99 );
cout << "data: " << data.get() << endl;
return 0;
}
#ifndef _DATA_HPP_
#define _DATA_HPP_
#include
class Data
{
public:
Data();
~Data();
void set( int number );
int get() const;
protected:
int _number;
};
int get();
#endif //_DATA_HPP_
#include "data.hpp"
Data::Data() :
_number( 0 )
{
}
Data::~Data()
{
}
void Data::set( int number )
{
_number = number;
}
int Data::get() const
{
return _number;
}
cmake_minimum_required(VERSION 2.6)
project(testdata)
add_executable(cppdata main.cpp data.cpp)
La chaine de construction
Afin de se simplifier la tâche, on va écrire un fichier buildchain pour chaque architecture. Ces fichiers, utilisables uniquement par cmake, nous permettront de nous affranchir de longues ligne de définition cmake.
- Le buildchain windows 32 bits: win32.cmake
- Le buildchain windows 64 bits: win64.cmake
set(CMAKE_SYSTEM_NAME Windows)
set( WIN_BUILD_ARCH "i686" )
set( WIN_ARCH_BITS "32" )
set( WIN_ARCH "x86" )
set(COMPILER_PREFIX "${WIN_BUILD_ARCH}-w64-mingw32")
# Which compilers to use for C and C++
set(CMAKE_C_COMPILER ${COMPILER_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${COMPILER_PREFIX}-windres)
# Here is the target environment located
set(CMAKE_FIND_ROOT_PATH /usr/${COMPILER_PREFIX} /usr/${COMPILER_PREFIX}/sys-root/mingw)
# Adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in 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)
set(CMAKE_SYSTEM_NAME Windows)
set( WIN_BUILD_ARCH "x86_64" )
set( WIN_ARCH_BITS "64" )
set( WIN_ARCH "x64" )
set(COMPILER_PREFIX "${WIN_BUILD_ARCH}-w64-mingw32")
# Which compilers to use for C and C++
set(CMAKE_C_COMPILER ${COMPILER_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${COMPILER_PREFIX}-windres)
# Here is the target environment located
set(CMAKE_FIND_ROOT_PATH /usr/${COMPILER_PREFIX} /usr/${COMPILER_PREFIX}/sys-root/mingw)
# Adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in 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)
Construction
Comme habituellement avec cmake, on créé un répertoire dédié à la construction.
Linux
mkdir linux
cd linux
cmake ..
make
Wind@uze
Version 32 bits
mkdir win32
cmake -D CMAKE_TOOLCHAIN_FILE=../win32.cmake ..
make
Version 64 bits
mkdir win64
cmake -D CMAKE_TOOLCHAIN_FILE=../win64.cmake ..
make