Use cmake to write platform-related checks-use Google Translate

Source: Internet
Author: User

Cmake: how to compile the platform check
If you want to write software and run it on different operating systems, you must take care of the special nature of different platforms. There are minor differences in different operating systems. For example, you should not use malloc. h on FreeBSD, but it can be used on Linux. To handle these differences, we usually provide a header file that contains definitions. According to the platform performance report, it is usually named config. h in a heap:

1 # define have_malloc_h
/* # Democracy

Fund have_sys_mnttab_h 1 */
/* # Democratic Fund have_sys_mntent_h 1 */
1 # define have_sys_mount_h
Then, the header file is included in the source file and properly handled:

Foo. C:

# Include "config. h"

# Ifdef have_malloc_h
# Include <malloc. h>
# Else
# Include <stdlib. h>
# Endif

Invalid do_something ()
(
* Buffer = Invalid malloc (1024 );
...
)
The content in config. h depends on the source of the compilation platform. Therefore, you need a method to generate this header file at the beginning of the actual compilation process. If you are using an automatic tool based software, you may know. /Configure, which must be executed before being started. In. The/configure script is used to reflect on the system and generate the config. h header file from the collected information. Cmake can do the same. I will tell you how to do it.

In addition, the built-in commands are called modules to implement more commands through the provided cmake script file. These files are located in the cmake Module Directory. By default, these files are cmake/units of/usr in UNIX systems.

If you want to use these commands, they will be in cmakelists.txt. Cmake comes, they all follow the same style of the example check_include_files to check the system's several modules:

Including (checkincludefiles)
# Usage: check_include_files (

Check_include_files (malloc. h have_malloc_h)
Check_include_files ("System/Param. h; System/mount. h" have_sys_mount_h)
Configure_file ($ (cmake_current_source_dir)/$ (cmake_current_binary_dir config. H. In)/config. h)
The cmake module checkincludefiles provides the command check_include_files (). The first parameter of this command is the header, you need to check. The second parameter is that the variable will contain the result. If a given header is found, it is set to 1; otherwise, it is null. If the other is the header you want to use, you need to list the header files and separate them with semicolons. You can see it above. To check whether check_include_files () is running:/usr/local/shared/cmake/module/checkincludefiles. cmake. There you will see that it tries to compile a simple source file, including the specified header file. The result of the test is stored in the cmakecache.txt file. If you want to check whether the test result is successful or not, the result is stored in the cmakecache.txt file:

// Include have_malloc_h
Have_malloc_h: Internal = 1
As long as the results are cached for a long time, the test will not be executed again. If you want your project to be executed again and then delete the cmakecache.txt file, all the tests will be executed again or only the variables will be deleted. You want to have the project to be tested again. This saves some time. If the test fails, find out the cause and open cmakefiles/cmakeerror. log and the title name (or function) to search for your test. There you will see the compilation command and error message when the Code cannot be compiled or linked. Together with the checkincludefiles. cmake test, you should be able to find out what went wrong.

Now, we have tested whether there is a malloc. h and the variable have_malloc_h in cmake. Therefore, we still need to create a config. h header. To do this, we use the cmake command configure_file (), as you can see above. This will copy the source file to the target file and edit it. For more information, see the manual page. Therefore, we have written a source file named config. H. in, but you can give it any name you want (it is also usually named config. H. In autotools ):

1 # cmakedefine have_malloc_h
# Cmakedefine have_sys_mount_h
Now, when cmake runs, it replaces # cmakedefine. If have_malloc_h and have_sys_mount_h are true, it will generate config. h:

1 # define have_malloc_h
# Define have_sys_mount_h
If both are false, this will produce:

/* # Democratic Fund have_malloc_h 1 */
/* # Define have_sys_mount_h */
By including the header to your source file, you can check these attributes using # ifdef. This check can be inserted into cmakelists.txt of any project, not just at the top layer of cmakelists.txt. If you have multiple configuration headers, you should not name them all config. H, which may cause problems with the inclusion path. It is best to give them a favorite config. H Name, configure it as Foo. h/in the configuration subdirectory, and configure it in the bar/subdirectory, etc. Bar. h

The future of cmake is used to perform system checks on other commands in this style, so we can process them shorter.

Module
Including (checkincludefiles)
Usage
Check_include_files (header variable)
Example
Check_include_files (strings. h have_strings_h)
As mentioned in the lengthy discussion, this can be used to check the existence of a header.

Module
Including (checkfunctionexists)
Usage
Check_function_exists (function variable)
Example
Check_function_exists (madvise have_madvise)
Check whether a given function exists. This is done by moving a small program and it may not cause undefined references.

Module
Including (checksymbolexists)
Usage
Check_symbol_exists (Symbol Title variable)
Example
Check_symbol_exists (lc_messages "in locale. h" have_lc_messages)
Check whether a given symbol exists if the specified header is also included.

Module
Including (checklibraryexists)
Usage
Check_library_exists (location variable of the library function)
Example
Check_library_exists (volmgt volmgt_running "" have_volmgt)
Check whether a given library exists and contains the given function. This is done by linking a applet that uses functions with libraries. The parameter directory (-ldir) in an additional link location can be considered if needed.

Module
Including (checktypesize)
Usage
Set (cmake_extra_include_files header)
Check_type_size (type variable)
Set (cmake_extra_include_files)
Example
Set (cmake_extra_include_files sys/socket. h)
Check_type_size ("Structure ucred" struct_ucred)
Set (cmake_extra_include_files)
Check whether the specified type and return type exist. The variable type size will be returned, and another variable have_struct_ucred will be set to true if the existing type exists. Do not think that you must set cmake_extra_include_files to the header file required for this type. You should call check_type_size and reset it. If you are not really interested in the size of this type, but only whether it exists or not, you can also use struct_ucred directly. If the type does not exist, this will be empty, therefore, it is also calculated as false (for example, have_struct_ucred ).

Module
Including (checkprototypeexists)
Usage
Check_prototype_exists (function header variable)
Example
Check_prototype_exists (in mkstemps "stdlib. h; unistd. h" have_mkstemps_proto)
Header check for the declaration of a given function, that is, if you do not check whether the function is used, it will lead to undefined reference.

Module
Including (checkcxxsourcecompiles)
Including (checkcsourcecompiles)
Usage
Check_cxx_source_compiles (source variable)
Check_c_source_compiles (source variable)
Check whether the given source code will be compiled and linked. You can set cmake_required_libraries, cmake_required_flags, and cmake_required_includes. Therefore, if the additional library or compiler flag is required.

 

-----------------

 

Address: http://www.itk.org/Wiki/CMake:How_To_Write_Platform_Checks

 

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.