CMake Use method & Cmakelist.txt__cmake

Source: Internet
Author: User
Tags mathematical functions
CMake Introduction


CMake is a Cross-platform installation (compilation) tool that describes the installation (compilation process) of all platforms with simple statements. He was able to output a wide variety of makefile or project files to test the C + + features supported by the compiler, similar to the Automake under UNIX. How to use CMake

All of CMake's 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 CMakeLists.txt resides. After the configuration is complete, apply the CMake command to generate the appropriate makefile (under Unix like system) or project files (specified when compiling with the appropriate programming tools under window).

Its basic operating procedures are:

$> ccmake directory
$> cmake directory
$> make

Where directory is the directory of CMakeList.txt; The first statement is used to configure compilation options, such as the Vtk_dir directory, generally this step does not need to configure, directly execute the second statement, but when there is an error, this need to think configuration, this step to really come in handy; The second command is used to generate makefile files based on CMakeLists.txt; the third command is used to execute makefile files, compile programs, and generate executable files;

CMake implementation is so simple, the difficulty lies in how to write the CMakeLists.txt file, the following combination of examples to introduce CMakeLists.txt's writing, look at the following CMakeLists.txt

#project name
project (test_math)
#head file path
include_directories (
INCLUDE
)
#source Directory
aux_source_directory (src dir_srcs)
#set environment variable
set (Test_math
${dir_srcs}
)
#set extern Libraries
set (libraries
libm.so
)
#add executable file
add_executable (.. /bin/bin ${test_math})
#add link library
target_link_libraries (... /bin/bin ${libraries})
 

or use the following CMakeLists.txt
#project name
project (test_math)
#head file path
include_directories (
INCLUDE
)
# SOURCE directory
aux_source_directory (src dir_srcs)
#set environment variable
set (Test_math
${ DIR_SRCS}
)
#add executable file
add_executable (.. /bin/bin ${test_math})
#add link library
target_link_libraries (... /bin/bin m)

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

Line 2nd specifies that the generated project name is 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 Setting the value of the environment variable Test_math is the value of the environment variable DIR_SRCS, which is used to show how to assign an environment variable to an environment variable

The 14th row of the Math function library is assigned to the environment variable libraries, of course, you can use this environment variable, and the library name directly behind

Line 18th is used to specify the build file, which compiles all the files in the Test_math directory of the environment variable. Executable bin in/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:

#include <stdio.h>
#include ". /INCLUDE/A.H "
int main ()
{
    double b=25.0;
    Double a=0.0;
    A=get_sqrt (b);
 
    printf ("A is%LF, B is%lf\n", a,b);
    return 0;
}


/src/a.c

#include ". /include/a.h
Double get_sqrt (double var1)
{return
    sqrt (var1);
}


/include/a.h

#ifndef  A_file_header_inc
#define  a_file_header_inc
#include <math.h>
 
double get_sqrt (double var1);
 
#endif 


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

$> CMake.
$> make


You can generate the executable file, the bin file under the directory/bin, and run to see if the effect is the same as what you want.

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.