New Cmake user diary (1) [initial experience under C ++ 11] And cmake11
When I used Cmake for the first time, I searched for many tutorials, including Cmake practices and Cmake manuals. However, programming with the latest C ++ 11 conditions still has some problems, you need to experiment many errors and search a large number of articles to solve the problem. Here we will use the newbie's diary to tell a Cmake Tom how to use Cmake in C ++ 11.
Generally, reading documents directly will not be suitable for new users. You have no sensory knowledge or experience. Here, we will record our learning process for your convenience, and also for other new users to learn.
1. Single file Compilation
(1) Operation Process
<1> create a directory t1.
<2> write the code:
Main. cpp
1 #include <iostream> 2 #include <vector> 3 int main() 4 { 5 6 using namespace std; 7 vector<int> V(5,3); 8 for(auto e:V) 9 cout << e << endl;10 cout << "OK" << endl;11 return 0;12 13 }
<3> compile cmakelists.txt
There is another "s" before "." In “cmakelists.txt ". If you are careless, it is estimated that you will be scratching your ears here.
PROJECT (HELLO)
SET(CMAKE_C_COMPILER g++)if(CMAKE_COMPILER_IS_GNUCXX) add_compile_options(-std=c++11)endif(CMAKE_COMPILER_IS_GNUCXX)
SET(SRC_LIST main.cpp)ADD_EXECUTABLE(hello ${SRC_LIST})
<4> compile in the t1 folder
Run
Cmake .
Run again
make
<5> run the program
In this case, the hello file will be generated in the t1 folder and run
./hello
The program runs correctly.
Output
@ubuntu:~/t1$ ./hello33333OK
(2) process explanation
In cmakelists.txt, the command can be written in uppercase, lowercase, or hybrid mode.
PROJECT (hellower is the first sentence of cmakelists.txt, telling the compiler that the PROJECT name is hello.
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 g ++ compiler is not set, if you do not judge the compiler, the following running 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 that compilation can be completed, but sometimes it cannot be completed here. Of course, it can also be done sometimes. This may be related to a specific system.
When the above problem occurs, you can use the compiler setting method to solve the problem, or you can use the if statement to make a judgment. Here, in order to make it clear all the situations, I will write this statement for the moment. CMAKE_COMPILER_IS_GNUCXX is used to determine the compiler type.
Statements other than explain can also be compiled normally. It's really strange.
Then SET (SRC_LIST main. cpp) and ADD_EXECUTABLE (hello $ {SRC_LIST}) statements. If this project is defined, an executable file named hello is generated. The source file is the list of source files defined in SRC_LIST. In this example, you can also directly write ADD_EXECUTABLE (hello main. c ).
$ {} Is used to reference variables. This is the way cmake variables are applied. However, there are some exceptions, such as when using the IF control statement, the variable is referenced directly using the variable name (for example, CMAKE_COMPILER_IS_GNUCXX) without $ {}. IF $ {} is used to apply the variable, in fact, IF will judge the variable with the value represented by $ {}, it certainly does not exist.
(3) Pay attention
In the cmakelists.txt script, you can set the compilation option throughadd_compile_options
Command, you can also use the set command to modifyCMAKE_CXX_FLAGS
OrCMAKE_C_FLAGS
.
The two methods have the same effect in some cases, but note that they are different:
add_compile_options
The compilation options added by the command are for all compilers (including c and c ++ compilers), while the set command is setCMAKE_C_FLAGS
OrCMAKE_CXX_FLAGS
The variables are for c and c ++ compilers respectively. Therefore, the following code has the same effect!
PROJECT (HELLO)SET(CMAKE_C_COMPILER g++)if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")endif(CMAKE_COMPILER_IS_GNUCXX)SET(SRC_LIST main.cpp)ADD_EXECUTABLE(hello ${SRC_LIST})