Cmake cross-compilation Configuration

Source: Internet
Author: User

Many times, we are faced with embedded platforms during development. Therefore, due to resource restrictions, cross-compilation is required. That is, the program of the target machine to be generated on your host machine. It involves the switch of related header files, the selection of compilers, and the change of environment variables. Today I will only briefly introduce the preparations required by cmake in the face of cross-compilation.

Cmake reserved a good variable for cross-compilation, namely cmake_toolchain_file, which defines the path of a file. This file is toolchain, which contains a series of variables and attributes you need to change, including c_compiler and cxx_compiler. If QT is used, you need to change qt_qmake_executable and boost_root to be changed if boost is used (For details, refer to findxxx. cmake ). Cmake creates a toolchain mechanism to prevent users from re-entering these commands for each cross-compilation. In short, it is a cmake script, embedded with all the cross-environment settings you need to change and set.

Some related variable settings are also involved here. Here we will briefly introduce several important

1. cmake_system_name: name of the operating system where the target of your target is located. For example, for arm or Linux, you need to write "Linux". For Windows, you need to write "Windows ", if your embedded platform does not have related OS, you need to write it as "generic". cmake considers that cross-compilation is in progress only when the cmake_system_name variable is set, it sets an additional variable cmake_crosscompiling to true.

2. cmake_c_compiler: C language compiler. You can set the variable to the complete path or file name, one advantage of setting the complete path is that cmake will go down to this path to find other compilation-related tools such as linker and binutils. If the file name you write has a prefix such as arm-Elf, cmake identifies and searches for related cross compilers.

3. cmake_cxx_compiler: Same as above, which indicates the C ++ compiler.

4. cmake_find_root_path: represents a series of changes to the root path of the relevant folder path. For example, if you set/opt/ARM/, all find_xxx.cmake will give priority to/usr/lib under this path, /lib and so on, and then go to your own/usr/lib and/lib for search. If you have some libraries that are not included in/opt/arm, you can also specify multiple values to cmake_find_root_path, for example

set(CMAKE_FIND_ROOT_PATH /opt/arm /opt/inst)

5. cmake_find_root_path_mode_program: used for find_program (). There are three values: never, only, both. The first one indicates that it is not in your cmake_find_root_path. The second one indicates that it is only in this path, the third indicates that the path is searched first, and then the Global Path is searched. For this variable, the host machine program is generally called, so it is generally set to never.

6. cmake_find_root_path_mode_library: used for find_library (), which indicates the library-related options when linking. Therefore, we need to set only to ensure that our library is found in the Cross environment.

7. cmake_find_root_path_mode_include, only_cmake_find_root_path, both_cmake_find_root_path

8. boost_root: for users who need a boost library, the path configuration of the boost Library also needs to be set. Therefore, the path here is the boost path under arm, including include and Lib.

9. qt_qmake_executable: For QT users, you need to change the relevant qmake command to the embedded version. Therefore, you need to specify the corresponding qmake path (to qmake itself)

 

The following is a demo to show you more clearly the corresponding configuration.

# this is requiredSET(CMAKE_SYSTEM_NAME Linux)# specify the cross compilerSET(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 directoriesSET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)# configure Boost and QtSET(QT_QMAKE_EXECUTABLE /opt/qt-embedded/qmake)SET(BOOST_ROOT /opt/boost_arm)

This completes the compilation of the relevant toolchain. After that, you can choose whether to use the host machine version or the development machine version flexibly. The difference is only one-dcmake_toolchain_file =. /toolchain. cmake, even better, if you have many programs that need to be transferred, but the target platform is the same, you only need to write a toolchain to put it in one place, it can be used for all projects.

The cross-compilation configuration of cmake is introduced here. I have been in the blog Park for more than two months. I feel a lot and hope you will like this article:) and give me some encouragement.

 

Reference: http://www.cmake.org/Wiki/CMake_Cross_Compiling

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.