[CMake notes] acquaintance CMake

Meet with CMake

       Do C / C ++ people should be experienced in the past to get an open source code and want to compile it, because there is always a different VS versions and various kinds of problems, the compilation process difficult, more time will be renounce the use of it. This process, in turn, will be difficult to promote the use of open source projects.

       In addition, cross-platform development based on my experience, it is a disaster. I would just graduated, I had a project is developed under Windows, but the server is Ubuntu's, when I was in VS2015 development under windows, debugging was no problem, but also uploaded to the server, and then write their environment according to various makefile

       Two years ago it exposed to OpenCV, which turned out to see CMakeLists.txt use this script, you can generate compile the project through the appropriate platform CMake program, it is simply too convenient.

CMake的HelloWorld

File Structure

helloworld
  |--CMakeLists.txt
  |--helloworld.cpp

 CMakeLists.txt

1  # CMake the minimum version required
 2 cmake_minimum_required (VERSION 3.0 )
 3  
4  # Specify the project name, in fact, is assigned to the variable PROJECT_NAME
 5  Project (the HelloWorld)
 6  
7  # find all the source files in the specified directory and place the variable name specified in SRC
 . 8 aux_source_directory (. the SRC)
 . 9  
10  # specify generate a target
 . 11 add_executable ($ {$ {} the PROJECT_NAME the SRC}

 helloworld.cpp

Because now the work is essentially VS developed under the windows, so I will write to the source file and header files generated by CMake, and then write in VS inside.

 Generate VS solution

CMake in

In the Select [Specify the generator for this project], the compiler should pay attention to the selected platform is 32-bit or 64-bit, if you need two platforms will have to create two solutions.

result

to sum up

Shortcoming

  1. You need to learn how to write CMakeLists.txt, need a little bit of learning costs;
  2. Each time you add a source or header files, etc., needs to be created in the source folder, you may also need to modify CMakeLists.txt, then CMake to generate it, can not be added directly in the VS in Oh! 1
  3. Example, some files still very early in this chapter, as aux_source_directory function can only find the source files, header files would not be able to add to the solution in which to modify the latter has brought no small trouble, the latter will then say in detail;
  4. temporarily unavailable.

advantage

  1.  To achieve a cross-platform compilation of management, no longer need to write a lot of repeat compiled script. Personal use is still very convenient
  2. Achieved when using Git or SVN, the purity of the code file management, regardless of the code will not be submitted to the project file (this is my personal OCD, was healed)

 

This is a personal study notes, you can correct me wrong!

Guess you like

Origin www.cnblogs.com/dilex/p/11074589.html