Transfer from RICHARDXG original cmake use tutorial
CMake is a more advanced compiler configuration tool than make, which can generate corresponding makefile or vcproj projects based on different platforms and different compilers.
By writing CMakeLists.txt, you can control the generated makefile to control the compilation process. CMake Auto-generated makefile not only build the target file from the Make command build project, but also support the installation (make install), test the installed program to execute correctly (makes test, or ctest), build the current platform's installation package Package), generate source packages (make Package_source), generate dashboard display data and upload advanced features, as long as the simple configuration in CMakeLists.txt, you can complete a lot of complex functions, including writing test cases. If you have nested directories, you can have your own CMakeLists.txt under subdirectories. In short, CMake is a very powerful compiled automatic configuration tool that supports a variety of platforms, KDE is also compiled with it, interested can be tried.
warming up:(1) Install the CMake. :http://www.cmake.org/cmake/resources/software.htmlDownload the appropriate package according to your needs, download the green version of Zip compression under Windows, and download the source code. (2) How to run the CMake. (GUI, command line)http://www.cmake.org/cmake/help/runningcmake.html
CMake Steps to use:
to run the GUI's CMake interface:
Cmake-2.8.1-win32-x86/bin/cmake-gui.exe
Execution Configure:
After running, the following files are generated:
Generate Makefile:
After executing generate, the following files are generated:
run make to compile:
After compiling, the build directory generates Tutorial.exe and runs Tutorial.exe 25 to see the results:
Run the Make install Installer:
run make test for testing:
Learn CMake configuration method by CMake tutorial
http://www.cmake.org/cmake/help/cmake_tutorial.htmlThe code for this manual can be found in the Tests/turorial directory of the source code.
1, Step1. (If you do not know how to use CMake, and how to use the Turorial.exe generated by the compilation, you can first look at the previous "CMake use steps" description, it takes STEP4 as an example in detail the use of the process, STEP1 configuration may not be complete, such as unable to run the make Install, you cannot run make test, but you can refer to it. )
Simple program compilation. (1) Run the GUI's cmake, specifying the source code path and the binary file path to be compiled (automatically created). (2) Click Configure, after the configuration is successful, then click Generate. Configuration needs to choose the appropriate compiler, although I installed the VC2008, but not configured successfully, select Unix makefiles, the configuration is successful, it automatically found devc++ under the Gcc.exe and other compilers.
(3) in the BUILD3 directory to execute make, you can compile the build Turorial.exe.
D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step1/build3>Make
Linking CXX executable Tutorial.exe
[100%] Built Target Tutorial
You can run Turorial.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, Step2
The handle directory is compiled into a library and is linked to the final executable file. Include_directories ("${project_source_dir}/mathfunctions")
Add_subdirectory (mathfunctions)# makes subdirectory mathfunctions can also be compiled
# Add the Executable
Add_executable (Tutorial tutorial.cxx)
Target_link_libraries (Tutorial mathfunctions)
Generate makefile: Click on the GUI configure, then generate or gray, click Configure,generate again to click.
Compile:Run make in the build directory to start compiling, But the beginning will report Sh.exe run exception, should be tools under the Unxutils Sh.exe and Win7 incompatible, found the following make file, estimated that it caused, so rename it, do not use the unxutils under Make, OK music. 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.A
[50%] Built Target Mathfunctions
Linking CXX executable Tutorial.exe
[100%] Built Target Tutorial
3, Step3Support make install to install the program to the system-specified directory, and run some tests to check if it works correctly. A. The base directory to be used at the time of installation, specified by Cmake_install_prefix. B, a very simple use case to check whether the program is running, no exception occurred. (Turotialruns is just a use case name) add_test (Tutorialruns Tutorial) C, macro method of testing multiple sets of data is very concise and convenient. #define a macro to simplify adding tests
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 ("5")
Do_test ( -25 " -25 is 0")
Execute make install:
D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step3/build>make Install
[50%] "Built Target Mathfunctions"
[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.A
--Installing:c:/program Files/tutorial/include/mathfunctions.h
Installation Result:
C:/Program files/tutorial>tree/f
C:.
├─bin
│libmathfunctions.a
│tutorial.exe
│
└─include
MathFunctions.h
TutorialConfig.h
Execute 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-level CMakeLists.txt, then configure and generate, then make test to see the results.
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, Step4Check if the system supports log and EXP functions. (both log and exp are mathematical operations functions)
Check method: (1) Use Checkfunctionexists.cmake in 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 to define the macro. (Modifying Tutorialconfig.h.in,cmake execution will define the macro as the appropriate value, generating the TurorialConfig.h for compile-time use)
Does the platform provide exp and log functions?
#cmakedefine Have_log
#cmakedefine Have_exp (3) Use the macro and log functions in your code. If we have both logs and exp then use them
#if defined (have_log) && defined (HAVE_EXP)
result = exp (log (x) *0.5);
The full configuration, build makefile, compile, run, install, and test procedures for the #else//otherwise use of an iterative approachStep4 are described in the first "cmake steps".
5, Step5Dynamically generate source files and automatically compile the source files into the system.Make made the error when:
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
[25%] "Built Target Maketable"
[50%] Generating Table.h
'. ' is not an internal or external command, nor is it a running program
or batch file.
MAKE[2]: * * * [mathfunctions/table.h] Error 1
MAKE[1]: * * * [Mathfunctions/cmakefiles/mathfunctions.dir/all] Error 2
Make: * * * [ALL] Error 2
problem Analysis: First look at the Build/makefile file, about maketable there are the following rules:# Build rule for Target.
Maketable:cmake_check_build_system
$ (make)-F cmakefiles/makefile2 maketable
. Phony:maketable
look at the Makefile2 file again to find the target that was being compiled when the error occurred. # 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
The order in which make rules are executed is in order of command: If the makefile content is as follows: all:
echo "First line."
echo "Second line."
So make result:
D:/users/desktop>make
echo "First line."
First line.
echo "Second line."
Second line. As a result, Built target maketable failed after the output.
6, Step6Generate a variety of platform windows/ubuntu/etc. The installation package, including the binary installation package and the source installation package. You can also package dependent system libraries. Include (Installrequiredsystemlibraries) uses Cpack. Because of step1-7, the configuration of the next step contains the configuration of the previous step, so starting with STEP5, you will encounter the issue of make. To compile the pass, you can modify the CMakeLists.txt and Mysqrt.cxx in the Mathfunctions directory, removing all dependencies on the Table.h. Run make packages to build the installation package: The first time, because NSIs is not installed, the following issues are indicated:
D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step6/build>make Package
[50%] "Built Target Mathfunctions"
[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 installing NSIs, run successfully:
D:/projects/lab/testngpp/cmake-2.8.1/tests/tutorial/step6/build>make Package
[50%] "Built Target Mathfunctions"
[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
Utorial-1.0.1-win32.exe generated. Generates the following Windows installation package files:
After the installation is complete, it is also convenient to uninstall it:
Run make Package_source to generate the source code package. (I'm not prompted to find a suitable zip program on my Computer)
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, Step7Publish the results to dashboard.
The following URL is a public dashboard:
Http://www.cdash.org/CDash/index.php?project=PublicDashboard
The name of the project displayed on dashboard is set up in the following way:
You need to add the Cmake/bin directory to path and then execute the ctest-d experimental. An error has been encountered here. 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 1024x768 bytes of output
. Size of output:0k
Build Project
Each symbol represents 1024x768 bytes of output.
'! ' represents an error and ' * ' a warning.
. Size of output:0k
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
Performing 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
Some of the following files have been generated:
CMake Using Tutorials