CMake How to use

Source: Internet
Author: User
Tags mathematical functions

CMake is a cross-platform installation (compilation) tool that can be used to describe the installation of all platforms (the compilation process) with simple statements. He is able to output a variety of Makefile or project files that can test the C + + features supported by the compiler, similar to the Automake under UNIX.

CMake How to use

All of the CMake statements are written in a file called: CMakeLists.txt. When the CMakeLists.txt file is determined, the associated variable value can be configured with the Ccmake command. This command must point to the directory where the CMakeLists.txt is located. After the configuration is complete, apply the CMake command to generate the appropriate makefile (under the UNIX like system) or the project file (specified with the appropriate programming tools under window).

Its basic operating procedures are:

    1. $> Ccmake Directory

    2. $> CMake Directory

    3. $> make

Where directory is the directory where CMakeList.txt resides;

    • The first statement is used to configure the compilation options, such as the Vtk_dir directory, the general step does not need to configure, directly execute the second statement, but when there is an error, it is necessary to consider the configuration, this step is really useful;
    • The second command is used to generate makefile files based on CMakeLists.txt;
    • The third command is used to execute the makefile file, compile the program, and generate the executable file;

CMake implementation is so simple, the difficulty lies in how to write CMakeLists.txt files, the following example of a simple introduction to CMakeLists.txt writing, see the following CMakeLists.txt

    1. #project Name

    2. PROJECT (Test_math)

    3. #head file path

    4. Include_directories (

    5. Include

    6. )

    7. #source Directory

    8. Aux_source_directory (src Dir_srcs)

    9. #set environment variable

    10. SET (Test_math

    11. ${DIR_SRCS}

    12. )

    13. #set extern Libraries

    14. SET (LIBRARIES

    15. Libm.so

    16. )

    17. #add executable file

    18. Add_executable (.. /bin/bin ${test_math})

    19. #add link Library

    20. Target_link_libraries (.. /bin/bin ${libraries})

or use the following CMakeLists.txt

    1. #project Name

    2. PROJECT (Test_math)

    3. #head file path

    4. Include_directories (

    5. Include

    6. )

    7. #source Directory

    8. Aux_source_directory (src Dir_srcs)

    9. #set environment variable

    10. SET (Test_math

    11. ${DIR_SRCS}

    12. )

    13. #add executable file

    14. Add_executable (.. /bin/bin ${test_math})

    15. #add link Library

    16. Target_link_libraries (.. /bin/bin m)

This is the CMakeLists.txt of a program that tests mathematical functions, followed by "#" as the contents of a comment, and CMake commands are all uppercase

Line 2nd specifies that the generated project is named Test_math

Line 4th specifies that the header file directory is include

Line 8th specifies that the source file directory is SRC and assigns it to the environment variable Dir_srcs

Line 10th sets the value of the environment variable Test_math to the value of the environment variable DIR_SRCS, which is used here to show how environment variables are assigned to an environment variable

The 14th line is to assign the math library to the environment variable libraries, which, of course, can be used without this environment variable, and directly using the library name later

Line 18th is used to specify the build file to compile all the files in the environment variable Test_math directory. Executable file bin in the/bin directory

Line 20th specifies: /bin/bin the link library at execution time is the value of the environment variable libraries-libm.so

The source file is given below
/SRC/MAIN.C:

    1. #include <stdio.h>

    2. #include ". /include/a.h "

    3. int main ()

    4. {

    5. Double b=25.0;

    6. Double a=0.0;

    7. A=get_sqrt (b);

    8. printf ("A is%LF, B is%lf\n", A, A, a, a);

    9. return 0;

    10. }

/src/a.c

    1. #include ". /include/a.h "

    2. Double get_sqrt (double var1)

    3. {

    4. return sqrt (VAR1);

    5. }

/include/a.h

#ifndef A_file_header_inc

    1. #define A_file_header_inc

    2. #include <math.h>

    3. Double get_sqrt (double var1);

    4. #endif

Place the CMakeLists.txt in the current directory and execute the CMakeLists.txt

    1. $> CMake.

    2. $> make

You can build the executable file in the bin file under directory/bin, so run to see if it works the way you want it to.

CMake use Method (RPM)

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.