CMake Cross-compilation configuration

Source: Internet
Author: User
CMake Cross-compilation configuration

Most of the time, we are developing in the face of the embedded platform, so due to resource constraints need to use the relevant cross-compilation. The program that you want to generate target targets on your host. Involved in the relevant header file switching and compiler selection and environment variables, and so on, I just briefly introduce the relevant cmake in the face of cross-compilation, need to do some preparatory work.

CMAKE a good variable Cmake_toolchain_file for cross-compilation, which defines the path to a file, which is toolchain, which sets a set of variables and attributes that you need to change, including c_compiler,cxx_ COMPILER, if you use QT, you need to change the qt_qmake_executable and boost_root if you need to change it if you use boost (see the path specified in the relevant findxxx.cmake specifically). CMake in order to not allow users to re-enter these commands every time they cross-compile, it brings the toolchain mechanism, in short, a cmake script that embeds the settings you need to change and all the cross-environments that need set. Several important variables set in the Toolchain script

1.cmake_system_name:

That is, the name of the operating system you target, such as arm or Linux, you need to write "Linux", if the Windows platform you write "Windows", if your embedded platform does not have the relevant OS you need to write "Generic", only when Cmake_ System_name This variable is set, CMake thinks it is cross-compiled at this time, it will set an additional variable cmake_crosscompiling to true.

2. Cmake_c_compiler:

As the name implies, the C language compiler, where you can set the variable to the full path or file name, set as the full path there is a benefit is that CMake will go down this path to find other compiler related tools such as Linker,binutils, If you write a filename with a prefix such as arm-elf and so on, CMake will recognize and look for the relevant cross compiler.

3. Cmake_cxx_compiler:

As above, this represents the C + + compiler.

4. Cmake_find_root_path:

one or more search paths that take precedence over other search paths are specified. For example, you set up the/opt/arm/, all the find_xxx.cmake will be prioritized according to the path under the/usr/lib,/lib and so on to find, and then go to your own/usr/lib and/lib to find, If you have some libraries that are not included in the/opt/arm, you can also display multiple values for Cmake_find_root_path, such as

Set (Cmake_find_root_path/opt/arm/opt/inst)

This variable is able to effectively reposition the root path of the search at a given location. The variable defaults to NULL.    This variable is useful when using cross-compilation: Use this variable to point to the root of the target environment, and then CMake will find it there.

5. Cmake_find_root_path_mode_program:

to Find_program () function, there are three kinds of values, Never,only,both, the first means not to look under your cmake_find_root_path, the second means to find only under this path, the third means to find the path, and then find the global path , for this variable, is generally called the host program, so it is generally set to never

6. Cmake_find_root_path_mode_library:

It works for Find_library (), which indicates the relevant options for the library at the time of the link, so it needs to be set to only to ensure that our library is found in a cross-environment.

7. Cmake_find_root_path_mode_include:

It works for Find_path () and Find_file (), generally only, and if you want to change it, it is generally also added option in the relevant FIND command to change the local settings, there are no_cmake_find_root_path,only_ Cmake_find_root_path,both_cmake_find_root_path

8. Boost_root:

For users who need the boost library, the associated boost library path configuration also needs to be set up, so the path here is the boost path under arm with include and Lib.

9. Qt_qmake_executable:

For QT users, you need to change the relevant Qmake command to switch to the embedded version, so you need to specify the corresponding Qmake path (assigned to qmake itself).

Toolchain Demo

# Required
Set (Cmake_system_name Linux)

# Specify the Cross compiler
set (Cmake_c_compiler   /opt /ARM/USR/BIN/PPC_74XX-GCC)
SET (cmake_cxx_compiler/opt/arm/usr/bin/ppc_74xx-g++)

# where is the target Environment 
SET (Cmake_find_root_path  /opt/arm/ppc_74xx/home/rickk/arm_inst)

# Search for programs in The Build host directories (not necessary)
SET (Cmake_find_root_path_mode_program never)
# for libraries and Headers in the target directories
set (cmake_find_root_path_mode_library only)
set (cmake_find_root_path_ Mode_include only)

# Configure boost and Qt
set (Qt_qmake_executable/opt/qt-embedded/qmake)
set (boost _root/opt/boost_arm)

This completes the related toolchain writing, after, you can flexibly choose whether to adopt the host version or the development machine version, the difference is only a-dcmake_toolchain_file=./ Toolchain.cmake, what's more, if you have a lot of programs that need to be transferred, but the target platform is the same, you just need to write a copy of the toolchain in one place and you can use it for all projects.

CMake cross-compiling configuration is introduced to this, transfer between me to Blog Park also 2 months, feel a lot, hope you will like this article:) and give me a little encouragement.

Http://www.cnblogs.com/rickyk/p/3875334.html?utm_source=tuicool&utm_medium=referral


CMake Cross Compiling

Cross compiling are supported by CMake starting with version 2.6.0.

Cross compiling means, the software is built for a different system than the one which does the build. This means CMake cannot autodetect the target system usually the executables don ' t run on the build host the Buil D process has-use-a different set of include files and libraries for building, i.e. not the native ones


Contents [Hide] 1 Setting up the system and Toolchain 2 searching and finding external software 3 the Toolchain file 4 system Intr Ospection 5 Using Executables in the build created during the build 6 cross compilation for Windows CE 7 information how T o Set up various cross compiling toolchains 8 What to cross compile specific projects 9 faq/potential problems Setting up the system and Toolchain

When cross compiling, CMake cannot guess the target system,so you had to preset some CMake variables, e.g. using a toolchain file. The following variables has to be preset: cmake_system_name  this one are mandatory, it is the NAME of the the target SYSTEM, i.e. the same as Cmake_system_name wo Uld has the if CMake would run on the target system. Typical examples is "Linux" and "Windows". This variable was used for constructing the file names of the platform files like Linux.cmake or Windows-gcc.cmake. If your target is a embedded system without OS set Cmake_system_name to "Generic". If Cmake_system_name is preset, the CMAKE variable cmake_crosscompiling are automatically set to TRUE, so the can used For testing in the CMake files. cmake_system_version  Optional, VERSION of your target SYSTEM, not used very much. cmake_system_processor  Optional, PROCESSOR (or hardware) of the target SYSTEM. This variable isn't used very much except for one purpose, it's used to load a cmake_system_name-compiler-cmake_system_p Rocessor.cmake file, which can be used to modify settings like compiler flags etc. for the target. You probably only has the to set this onE If you is using a cross compiler where every target hardware needs special build settings.

Since CMake cannot guess the target system, it also cannot guess which compiler it should use and so you had to preset t He too:cmake_c_compiler  the C COMPILER executable, May is the full path or just the filename. If It is a specified with full path, then this path would be prefered when searching the C + + compiler and the other tools (bi NUTILS, linker, etc.). If This compiler was a gcc-cross compiler with a prefixed name (e.g. "ARM-ELF-GCC") CMake would detect this and automaticall Y find the corresponding C + + compiler (i.e. "arm-elf-c++"). The compiler can also be preset via the CC environment variables. cmake_cxx_compiler  the C + + COMPILER executable, May is the full path or just the filename. It is handled the same as Cmake_c_compiler. If The toolchain is a GNU toolchain, you are need to set one of the both.

Once the system and the compiler is determined by CMake, it loads the corresponding files in the following order: Platform/${cmake_system_name}.cmake (optional, but issues a stern warning) platform/${cmake_system_name}-< Compiler>.cmake (optional) Platform/${cmake_system_name}-<compiler>-${cmake_system_processor}.cmake ( Optional

<compiler> is either the basename of the compiler executable, e.g. ' gcc ' (this is also used if GCC have a different n AME) or "CL", or by a compiler ID, which was detected by compiling a test source file.

for testing the host system, there are a corresponding set of variables, which is set automatically by CMake: Cmake_host_system_name cmake_host_system_version cmake_host_system_processor cmake_ Host_system

Without cross compiling the variables for the host system and the target system is identical. In most cases you'll want to testfor the target system and then the same-the-without cross compiling use the cmake_s Ystem_xxx variables,this would work both for cross compiling and for native building.

With these variables correctly set, CMake'll now use the cross compiling toolchain for building and in the CMAKELISTS.TX T can still use thecmake_system_xxx variables for testing for which SYSTEM is building. This is already enough to use CMake for cross compiling Simple (buildsystem-wise) projects. searching and finding external software

Most non-trivial projects would depend on external libraries or tools. CMake offers the Find_program (), Find_library (), Find_file (), Find_path () and find_package () commands for this purpose. They search the file system in common places for files and return the results. Find_package () is a bit different in the it actually doesn ' t search itself, but "only" executes Findxxx.cmake modules, WH Ich usually call the Find_program (), Find_library (), Find_file () and Find_path () commands then.

When cross compiling e.g. to a target with an ARM processor getting/usr/lib/libjpeg.so as the result of a find_package ( JPEG) wouldn ' t is much of a help, since this would is the JPEG library for the host system, e.g. an x86 Linux box. So-need to-tell CMake-to-search in the other locations. This can-be-done by setting the following variables:cmake_find_root_path  a list of directories, each of the Directories listed there'll be a prepended to each of the search directories of every find_xxx () command. So e.g. if your target environment are installed UNDER/OPT/ELDK/PPC_74XX, set Cmake_find_root_path to this directory. then e.g Find_library (bz2_lib bz2) would search In/opt/eldk/ppc_74xx/lib,/opt/eldk/ppc_74xx/usr/lib,/lib,/usr/lib and So give/opt/eldk/ppc_74xx/usr/lib/libbz2.so as result. By default Cmake_find_root_path is empty. If set, at first the directories prefixed with the directories given in cmake_find_root_path would be searched and after th At the unprefixedVersions of the search directories would be searched. This behaviour can is modified individually for every find_xxx () call with the No_cmake_find_root_path, Only_cmake_find_ro Ot_path and Cmake_find_root_path_both options or the default for all find_xxx () commands can is adjusted with the Cmake_fi Nd_root_path_mode_program, cmake_find_root_path_mode_library and cmake_find_root_path_mode_include variables. If you don ' t want-only libraries this come with the toolchain but also build and use additional libraries for your Target platform, you should create a install directory for these packages, e.g. $HOME/eldk-ppc_74xx-inst/and add Cmake_find_root_path, so the find_xxx () commands would search there too. If you then build packages for your target platform, they should is installed into this directory. Cmake_find_root_path_mode_program This sets the default behaviour for the Find_program () command. It can be set to never, only or BOTH (default). If set to Never, Cmake_find_rooT_path'll isn't being used for Find_program () calls (except where it's enabled explicitely). If set to only, the search directories with the prefixes coming from Cmake_find_root_path would be used in Find_progra M (). The default is BOTH, which means in first the prefixed directories and after that the unprefixed directories would be Searched. In the most cases find_program () are used to search for a executable which would then be executed e.g. using execute_process () or Add_custom_command (). The cases an executable from the build host was required, so usually set Cmake_find_root_path_mode_program to never. Cmake_find_root_path_mode_library this is the same as above and the For the Find_library () command. In most cases the used to find a library which'll then be used for linking, so a library for the target is required. In the common case, set it to only. Cmake_find_root_path_mode_include the same as above and used for both Find_path () and Find_file (). In many casesThis was used for finding include directories, so the target environment should was searched. In the common case, set it to only. The behaviour for some of the Find_path () or find_file () calls using the No_cmake_find_root_path, Only_cmake_find_root_path and Cmake_find_root_path_both options.The toolchain file

Defining all the variables mentioned above Using-dcmake_system_name etc. would be quite tedious and error prone. To make things easier, there are another cmake variable you can set:cmake_toolchain_file

Absolute or relative path to a cmake script which sets up all the toolchain related variables mentioned above Toolcha In Demo File

For instance-crosscompiling from Linux to Embedded Linux on PowerPC This file could look like this:

# This one was important
set (Cmake_system_name Linux) #this one not so
much
set (cmake_system_version 1)

# Specify the Cross compiler
set (Cmake_c_compiler   /OPT/ELDK-2007-01-19/USR/BIN/PPC_74XX-GCC)
Set (Cmake_ cxx_compiler/opt/eldk-2007-01-19/usr/bin/ppc_74xx-g++)

# where is the target environment 
SET (cmake_find_ Root_path  /opt/eldk-2007-01-19/ppc_74xx/home/alex/eldk-ppc74xx-inst)

# Search for programs in the build host Directories
SET (Cmake_find_root_path_mode_program never)
# for libraries and headers in the target Directories
Set (cmake_find_root_path_mode_library only)
set (Cmake_find_root_path_mode_include only)

If This file was named Toolchain-eldk-ppc74xx.cmake and is located in your home directory and you are building in the Subdi Rectory build then what can do:

~/src$ CD build
~/src/build$ cmake-dcmake_toolchain_file=~/toolchain-eldk-ppc74xx.cmake.
...

You don ' t has to write a toolchain file for every piece of software you want to build, the toolchain files is per ta Rget platform, i.e. if you is building several software packages all for the same target platform. One toolchain file and you can use the this for all packages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.