Using CMake for the first time, we searched for a lot of tutorials, including "CMake practice", "CMake Handbook", etc., but there are a few problems with programming under the latest C++11 conditions, you need to experiment many times and search a lot of articles to solve the problem. Here with the Novice diary of the way to tell a cmake how to use the c++11 under the CMake.
In general, reading documents directly will be less suitable for beginners, no sensory awareness, no experience. Here will be their own learning process recorded, convenient for their own inspection, but also for other beginners to learn.
First of all, declare the CMake script, in fact, is the basic grammatical rules of CMakeLists.txt:
- Variables use the ${} method, but the variable name is used directly in the IF control statement
- The directive (parameter 1, parameter 2 ...) parameter is enclosed in parentheses, and the arguments are separated by a space or semicolon.
- Directives are case-insensitive, and arguments and variables are case-sensitive. However, it is recommended that you use all uppercase instructions.
One, single file compilation
(1) Operation process
<1> Create a directory t1.
<2> Writing code:
Main.cpp
1#include <iostream>2#include <vector>3 intMain ()4 {5 6 using namespacestd;7vector<int> V (5,3);8 for(auto e:v)9cout << e <<Endl;Tencout <<"OK"<<Endl; One return 0; A -}
<3> Writing CMakeLists.txt
"Note" Do not forget "CMakeLists.txt" in "." There is also a "s", if you are very careless, estimated to be here scratching.
PROJECT (HELLO)
SET (Cmake_c_compiler g++)if (cmake_compiler_is_gnucxx) add_compile_options (-std=c++11)
Message (STATUS "optional:-std=c++11")endif (cmake_compiler_is_gnucxx)
SET (src_list main.cpp)add_executable (Hello ${src_list})
<4> compiling under the T1 folder
Run first, pay attention to the small dots behind the CMake "." Represents the current directory.
Cmake.
Run again
Make
<5> Running Programs
At this point, the T1 folder will produce a hello file, run
./hello
The program is running correctly.
Output
@ubuntu: ~/t1$./hello33333OK
(2) Process explanation
First, in CMakeLists.txt, commands can be written in uppercase, lowercase, and mixed.
Project (Hello) is the first sentence of CMakeLists.txt, which tells the compiler that this project is called HELLO.
The SET (Cmake_c_compiler g++) declaration uses the g++ compiler, because if it is a. C file, the default GCC compiler is usually used.
Add_compile_options (-std=c++11) tells the compiler to use c++11, but if you do not set the g++ compiler and do not judge the compiler, the following results will appear:
@ubuntu: ~/t1$ makescanning dependencies of target Hello[50%]Building CXX Object cmakefiles/hello.dir/main.oc++: error:unrecognized command line option '-STD=c++11' Cmakefiles/hello.dir/build.make:62: Recipe for target ' cmakefiles/hello.dir/main.o ' Failedmake[2]: ***[CMAKEFILES/HELLO.DIR/MAIN.O]Error 1Cmakefiles/makefile2:67: Recipe for target ' Cmakefiles/hello.dir/all ' Failedmake[1]: ***[Cmakefiles/hello.dir/all]Error 2Makefile:83: Recipe for target ' all ' failedmake: * * *[ All]Error 2
Some blogs say they can be compiled, but there is no time to finish it here. Of course, sometimes it can be done, this may be related to the specific system.
When the above problem occurs, you can use the set compiler to resolve, you can use the IF statement to judge, here in order to be able to clear all the situation, it is temporarily superfluous this write. Where Cmake_compiler_is_gnucxx is used to determine the compiler type.
However, the author also encountered such a problem, if the compiler is not set to g++, the first time to set the compiler for the Std=c++11 property times wrong, but once set to run successfully, even if the compiler set statements removed, The T1 below except for the main.cpp and CMakeLists.txt outside the statement removed, also can compile normally. It's kind of weird.
The message (STATUS "optional:-std=c++11") can be used to print some hints, where CMake is finished running. After that, the text in the "" is printed out. In fact, in addition to printing a generic identity with status, message has some other printing features, such as using Send_error, which generates an error message and the build process is skipped. If you use Fatal_error, all cmake procedures are terminated immediately.
Then set (Src_list main.cpp) and add_executable (Hello ${src_list}) statements. Define this project will generate a file named Hello executable file, the related source file is src_list defined in the source file list, in this case can also be written directly add_executable (Hello main.c).
${} to refer to variables, this is how the CMake variable is applied, but there are some exceptions, such as in the IF control statement, where the variable is directly referenced by the variable name (as in this case, cmake_compiler_is_gnucxx), without the need for ${}. If you use ${} to apply a variable, the IF will judge the variable named ${}, which of course does not exist.
(3) A little attention
In the CMakeLists.txt script, the set compilation options can be add_compile_options
modified by command or by the Set CMAKE_CXX_FLAGS
command CMAKE_C_FLAGS
.
In some cases, the effect is the same, but note that there are differences between the two ways:
add_compile_options
The compile options that the command adds are for all compilers (including the C and C + + compilers), and the SET command settings CMAKE_C_FLAGS
or CMAKE_CXX_FLAGS
variables are for the C and C + + compilers, respectively. Therefore, the following code is the same effect Oh!
PROJECT (HELLO) SET (Cmake_c_compiler g++) if (cmake_compiler_is_gnucxx) " -std=c++11 ${cmake_cxx_flags} ") endif (cmake_compiler_is_gnucxx) SET (src_list main.cpp) add_executable (Hello ${ Src_list})
CMake Beginner's Diary (1) "First experience under C++11"