Cmake Getting Started Guide

Source: Internet
Author: User
Cmake is a more advanced compilation and Configuration tool than make. It can generate a makefile or vcproj project based on different platforms and compilers.
By writing cmakelists.txt, you can control the generated makefile to control the compilation process. The makefile automatically generated by cmake can not only build the project to generate the target file through the make command, but also support installation (make install) and test whether the installed program can be correctly executed (make test, or ctest) generate the installation package of the current platform, and generate the source package_source1_1_dashboardto display data and upload the advanced functions. You only need to simply configure it in cmakelists.txt to complete many complex functions, including writing test cases. If you have an embedded directory, you can download the local cmakelists.txt file. In short, cmake is a very powerful automatic configuration tool for compilation and supports various platforms. KDE is also compiled with it. If you are interested, try it out. Preparation Activities:(1) install cmake. : Bytes. (2) run the cmake method. (GUI, command line) http://www.cmake.org/cmake/help/runningcmake.html
Cmake procedure:
Run the cmake interface of Gui:
Cmake-2.8.1-win32-x86 \ bin \ cmake-gui.exe
Execute configure:
After running, the following file is generated:

Generate makefile:
Generate the following file after executing generate:
Run make to compile:
After compilation, you can generate tutorial.exein the builddirectory and run tutorial.exe 25 to see the running result:
Run the make install Installer:
Run make test to test:  Use cmake tutorial to learn how to configure cmake
Bytes. 1. Step 1.(If you do not know how to use cmakeand how to use the compiled turorial.exe, You can first look at the instructions in the previous "cmake usage steps". It takes step 4 as an example to describe the usage process. The configuration of step 1 may not be complete enough, for example,, you cannot run make test, but you can refer to it .)
Simple Program compilation. (1) Run cmake of the GUI and specify the source code path and binary file path to be compiled (automatically created ). (2) Click Configure. After the configuration is successful, click Generate. You need to select the appropriate compiler for the configuration. Although vc2008 is installed, the configuration is not successful. If you select UNIX makefiles, the configuration is successful, and the compiler such as gcc.exe under devcworkflow is automatically located.
(When the 3rd directory is makeed in the build3directory, You can compile and generate turorial.exe.
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step1 \ build3> make
Linking cxx executable tutorial.exe
[100%] built target tutorialyou can run ubunturorial.exe: D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step1 \ build3> tutorial.exe
Tutorial.exe Version 1.0
Usage: tutorial.exe number
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step1 \ build3> tutorial.exe 4
The square root of 4 is 2
2. Step 2
Compile the subdirectory as a library and link it to the final executable file. Include_directories ("$ {project_source_dir}/mathfunctions ")
Add_subdirectory (mathfunctions) # Make sure that the subdirectory mathfunctions can also be compiled.
# Add the executable
Add_executable (Tutorial tutorial. cxx)
Target_link_libraries (Tutorial mathfunctions)
Generate makefile:Click Configure on the GUI, and then generate is gray. Click Configure again to generate and click. Compile:Swap is not compatible with win7. If the following make file is found, it is probably caused by it. Therefore, rename it and make under unxutils is not used. D: \ tools \ cmd \ unxutils \ USR \ Local \ wbin \ make.exe compilation process:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step2 \ build> make
[50%] building cxx object mathfunctions/cmakefiles/mathfunctions. DIR/mysqrt. cxx
. OBJ
Linking cxx static library libmathfunctions.
[2, 50%] built target mathfunctions
Linking cxx executable tutorial.exe
[2, 100%] built target tutorial

3. Step 3Make install is supported to install the program to the specified directory of the system and run some tests to check whether the program works properly. A. The basic directory used for installation, which is specified by cmake_install_prefix. B. Check whether the program runs through a simple use case without exceptions. (Turotialruns is just a Use Case name) add_test (tutorialruns tutorial 25) C. It is very simple and convenient to test multiple groups of data in macro mode. # Define a macro to simplify adding tests, then use it
Macro (do_test Arg result)
Add_test (tutorialcomp $ {Arg} tutorial $ {Arg })
Set_tests_properties (tutorialcomp $ {Arg}
Properties pass_regular_expression $ {result })
Endmacro (do_test) # do a bunch of result based tests
Do_test (25 "25 is 5 ")
Do_test (-25 "-25 is 0 ")
Run make install:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step3 \ build> make install
[1, 50%] "built target mathfunctions"
[1, 100%] "built target tutorial"
Install the project...
-- Install configuration :""
-- Installing: C:/program files/tutorial/bin/tutorial.exe
-- Installing: C:/program files/tutorial/include/tutorialconfig. h
-- Installing: C:/program files/tutorial/bin/libmathfunctions.
-- Installing: C:/program files/tutorial/include/mathfunctions. h
Installation result:
C: \ Program Files \ tutorial> tree/F
C :.
Zhu── Bin
│ Libmathfunctions.
│ Tutorial.exe

└ ── Include
Mathfunctions. h
Tutorialconfig. h
Run make test:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step3 \ build> make test
Running tests...
TEST project D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/Step3/build
Start 1: tutorialruns
1/5 test #1: tutorialruns ...... passed 0.01 Sec
Start 2: tutorialcomp25
2/5 test #2: tutorialcomp25 ...... passed 0.01 Sec
Start 3: tutorialnegative
3/5 test #3: tutorialnegative ...... passed 0.01 Sec
Start 4: tutorialsmall
4/5 test #4: tutorialsmall ...... passed 0.00 Sec
Start 5: tutorialusage
5/5 test #5: tutorialusage .................... passed 0.00 sec 100% Tests passed, 0 tests failed out of 5 total test time (real) = 0.13 Sec
Modify a test case so that it does not:
Modify the top-layer cmakelists.txt, re-configure and generate, and then make test to see the result.
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ Step3 \ build> make test
Running tests...
TEST project D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/Step3/build
Start 1: tutorialruns
1/5 test #1: tutorialruns ...... passed 0.01 Sec
Start 2: tutorialcomp25
2/5 test #2: tutorialcomp25 ...... failed required regular expre
Ssion not found. RegEx = [25 is 3
] 0.01 Sec
Start 3: tutorialnegative
3/5 test #3: tutorialnegative ...... passed 0.01 Sec
Start 4: tutorialsmall
4/5 test #4: tutorialsmall ...... passed 0.01 Sec
Start 5: tutorialusage
5/5 test #5: tutorialusage .................... passed 0.01 sec 80% Tests passed, 1 tests failed out of 5 total test time (real) = 0.13 sec The following tests failed:
2-tutorialcomp25 (failed)
Errors while running ctest
Make: *** [test] Error 8

4. Step 4Check whether the system supports log and exp functions. (Both log and exp are mathematical calculation functions)
Check Method: (1) Use checkfunctionexists. cmake in the top-level Configuration
# Does this system provide the log and exp functions?
Include (checkfunctionexists. cmake)
Check_function_exists (log have_log)
Check_function_exists (exp have_exp)
(2) modify the. In file and define the macro. (Modify tutorialconfig. H. In. During cmake execution, the macro is defined as an appropriate value and turorialconfig. H is generated for compilation)
// Does the platform provide exp and log functions?
# Cmakedefine have_log
# Cmakedefine have_exp (3) Use macro and log functions in the code. // If we have both log and exp then use them
# If defined (have_log) & defined (have_exp)
Result = exp (log (x) * 0.5 );
# Else // otherwise use an iterative approachstep4 complete configuration, generate makefile, compile, run, install, and test processes, see the previous "cmake use steps ".
5. Step 5The source file is generated dynamically and automatically compiled into the system. An error occurred while making:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step5 \ build> make
Scanning dependencies of target maketable
[25%] building cxx object mathfunctions/cmakefiles/maketable. DIR/maketable. cxx.
OBJ
Linking cxx executable maketable.exe
[1, 25%] "built target maketable"
[2, 50%] generating table. h
'.' Is neither an internal or external command nor a program that can be run.
Or batch files.
Make [2]: *** [mathfunctions/table. H] Error 1
Make [1]: *** [mathfunctions/cmakefiles/mathfunctions. DIR/All] Error 2
Make: *** [all] Error 2
Problem Analysis: First, check the build/MAKEFILE file. The maketable has the following rules: # build rule for target.
Maketable: cmake_check_build_system
$ (Make)-f cmakefiles/makefile2 maketable
. Phony: maketable
Check the makefile2 file again and find the target that is being compiled when an error occurs. # All build rule for target.
Mathfunctions/cmakefiles/maketable. DIR/All:
$ (Make)-f mathfunctions/cmakefiles/maketable. DIR/build. Make mathfunctions/cmakefiles/maketable. DIR/depend
$ (Make)-f mathfunctions/cmakefiles/maketable. DIR/build. Make mathfunctions/cmakefiles/maketable. DIR/build
$ (Cmake_command)-E cmake_progress_report D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step5/build/cmakefiles 1
@ Echo "built target maketable"
. Phony: mathfunctions/cmakefiles/maketable. DIR/all
Make rules are executed in the order of commands: If the makefile content is as follows: All:
Echo "first line ."
Echo "second line ."
The Make result is as follows:
D: \ Users \ Desktop> make
Echo "first line ."
First line.
Echo "second line ."
Second line. Therefore, built target maketable fails after output.
6. Step 6Generate installation packages on Windows/Ubuntu/etc. platforms, including binary installation packages and source code installation packages. You can also package the dependent system library. Include (installrequiredsystemlibraries) uses cPack. Because of the Step1-7, the configuration of the next step includes the configuration of the previous step, so from step 5, you will encounter the problem of make. After compilation, you can modify cmakelists.txt and mysqrt. cxx in the mathfunctionsdirectory to remove all table. h dependencies. Run make package to generate the installation package: the first time because NSIs is not installed, the following error is prompted:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step6 \ build> make package
[1, 50%] "built target mathfunctions"
[1, 100%] "built target tutorial"
Run cPack packaging tool...
CPack error: cannot find NSIs registry value. This is usually caused by NSIs not
Being installed. Please install NSIs from http://nsis.sourceforge.net
CPack error: Cannot initialize the generator NSIS
Make: *** [Package] Error 1 after NSIs is installed, it runs successfully:
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step6 \ build> make package
[1, 50%] "built target mathfunctions"
[1, 100%] "built target tutorial"
Run cPack packaging tool...
CPack: Create package using NSIS
CPack: Install Projects
CPack:-run preinstall target for: tutorial
CPack:-install project: tutorial
CPack: compress package
CPack: Finalize package
CPack: Package D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step6/build/T
The utorial-1.0.1-win32.exe generated. generated the following Windows installation package file:
After the installation is complete, you can easily uninstall it:
Run make package_source to generate the source code package. (I am prompted on my computer that I cannot find a suitable zip Program)
D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step6 \ build> make package_sou
RCE
Run cPack packaging tool for source...
CPack error: cannot find a suitable zip Program
CPack error: Cannot initialize the generator zip
Make: *** [package_source] Error 1
  7. Step 7Publish the result to the dashboard.
The following url is a public dashboard:
Http://www.cdash.org/CDash/index.php? Project = publicdashboard
The project name displayed on the dashboard is set as follows:
Add the cmake \ bin directory to the path and run ctest-D experimental. An error is encountered. D: \ projects \ lab \ testngpp \ cmake-2.8.1 \ tests \ tutorial \ step7 \ build> ctest-D experim
Ental
Site: JELLY-PC2
Build name: Win32-make
Create new tag: 20100521-1833-experimental
Configure Project
Each. represents 1024 bytes of output
. Size of output: 0 K
Build Project
Each symbol represents 1024 bytes of output.
'! 'Represents an error and '* 'a warning.
. Size of output: 0 K
0 Compiler Errors
0 compiler warnings
TEST project D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step7/build
Start 1: tutorialruns
1/9 test #1: tutorialruns ...... passed 0.01 Sec
Start 2: tutorialusage
2/9 test #2: tutorialusage ...... passed 0.01 Sec
Start 3: tutorialcomp4
3/9 test #3: tutorialcomp4 ...... passed 0.01 Sec
Start 4: tutorialcomp9
4/9 test #4: tutorialcomp9 ...... passed 0.01 Sec
Start 5: tutorialcomp5
5/9 test #5: tutorialcomp5 ...... passed 0.01 Sec
Start 6: tutorialcomp7
6/9 test #6: tutorialcomp7 ...... passed 0.01 Sec
Start 7: tutorialcomp25
7/9 test #7: tutorialcomp25 ...... passed 0.01 Sec
Start 8: TutorialComp-25
8/9 test #8: TutorialComp-25 ...... passed 0.01 Sec
Start 9: tutorialcomp0.0001
9/9 test #9: tutorialcomp0.0001 ...... passed 0.01 Sec
100% Tests passed, 0 tests failed out of 9
Total test time (real) = 0.19 Sec
Discovery Ming coverage
Cannot find any coverage files. Ignoring coverage request.
Submit files (using HTTP)
Using HTTP submit Method
Drop site: http ://
Error When uploading file: D:/projects/lab/testngpp/cmake-2.8.1/tests/tutoria
L/step7/build/testing/20100521-1833/build. xml
Error message was: couldn't connect to host
Problems When submitting via HTTP
Errors while running ctest generates the following files:
Link: http://www.cnblogs.com/sinojelly/archive/2010/05/22/1741337.html

Cmake Getting Started Guide

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.