CMake installation tutorial

Source: Internet
Author: User

CMake installation tutorial

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.

(2) run the cmake method. (GUI, command line)

  
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 CMakeHttp://www.cmake.org/cmake/help/cmake_tutorial.html

You can find the code corresponding to this manual in the Tests/Turorial directory of the source code.

  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> makeLinking CXX executable Tutorial.exe

[2, 100%] Built target Tutorial

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 2Compile 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 3

Make 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 4

Check whether the system supports log and exp functions. (Both log AND exp are mathematical calculation 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 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 approach

For the complete configuration, generate Makefile, compile, run, install, and test process in Step 4, see the previous "CMake usage steps ".

  5. Step 5

The source file is generated dynamically and automatically compiled into the system.

Make error: 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 rules are as follows:

# 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 6

Generate 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)

Use 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:

For the first time, because nsis is not installed, the following problems are 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

Utorial-1.0.1-win32.exe generated.

The following Windows installation package file is generated:

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 7

Publish 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

The following files are generated:

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.