The previous section said that CMake is easy to use, but the actual development of the project file is very large, which simple way to use can lead to the code is very confusing, so this article describes a cmake to manage the big project demo process.
The steps are as follows:
1. Create the relevant project directory
[CPP]View PlainCopy
- Cmd
- mkdir Hello
- CD Hello
- mkdir inlude lib src debug
where INLUCDE used to store the header files, Lib used to store the library files, SRC used to store the source program, debug for debugging, similar to the configuration of the IDE environment such as QT
[CPP]View PlainCopy
- CD src
- mkdir main util
Where main is used to store the main program (Main.cpp for example), Util is used to store the relevant library source program (here Hello.cpp for example)
2. Create the relevant source file
Add project-Related header files to the include (Hello.h for example):
[CPP]View PlainCopy
- Hello.h
- #ifndef _hello_h_
- #define _hello_h_
- extern int hello ();
- #endif
In main, add the main program main.cpp as follows:
[CPP]View PlainCopy
- #include <iostream>
- #include "hello.h"
- int main () {
- Hello ();
- return 0;
- }
Add the Hello.cpp as follows in Util:
[CPP]View PlainCopy
- #include <iostream>
- Using namespace std;
- int Hello () {
- cout << "Hello word cmake!!!" << Endl;
- return 0;
- }
3. Create the relevant configuration file CMakeLists.txt:
Add the CMakeLists.txt in the top-level directory Hello as follows:
[CPP]View PlainCopy
- <pre name="code" class="cpp" >project (HELLO) #设置工程名
- Add_subdirectory (SRC)
- Cmake_minimum_required (VERSION 3.7) #设置版本号
- MESSAGE (STATUS "This is BINARY dir" ${hello_binary_dir})
- MESSAGE (STATUS "This is SOURCE dir" ${hello_source_dir})
This defines the subdirectory SRC, which is used to recursively invoke the CMakeLists.txt in SRC
Add CMakeLists.txt to the SRC directory as follows:
[CPP]View PlainCopy
- Add_subdirectory (Util)
- Add_subdirectory (Main)
- This setting again recursively calls the CMakeLists.txt in main and util
Add CMakeLists.txt to the main directory as follows:
[CPP]View PlainCopy
- SET (Executable_output_path ${hello_source_dir}/bin)
- SET (Src_list main.cpp)
- Include_directories (${hello_source_dir}/include)
- Link_directories (${hello_source_dir}/lib)
- Add_executable (Hello ${src_list})
- Target_link_libraries (Hello util)
The main point here is to define some of the commands or environments needed to compile and link executable programs.
The Include_directories command is to define the project's include folder, which holds the header file of the library used, and Link_directories is
Defines the project library file, which holds the library file, add_executable is the executable file that defines the build, target_link_libraries
To define the library files that are needed when linking.
The Util directory adds CMakeLists.txt as follows:
[CPP]View PlainCopy
- SET (Library_output_path ${hello_source_dir}/lib)
- SET (Cmake_c_compiler g++)
- SET (Src_list hello.cpp)
- Include_directories (${hello_source_dir}/include)
- Add_library (Util STATIC ${src_list})
where set (Library_output_path ${hello_source_dir}/lib) defines the path generated by the library, Library_output_path is an internal variable,
Repository build path. SET (Src_list hello.c) is the source file that is used to define the library file. Add_library (Util STATIC ${src_list}) is
Used to define the name of the generated library, and the type of build library and the source files needed to build the library. SET (Cmake_c_compiler g++) is the compiler used to define C
For g++, prevent C and C + + code from using GCC by default without specifying the C compiler, resulting in system compilation confusion
After the configuration is complete, the directory structure is as follows:
4. Compile and run
CD Debug
Cmake-g "MinGW makefiles". \
Make
.. \bin\hello.exe
5. Installation
Add copyright, README, and Run.bat to the project catalog, create a Doc folder, and new Hellot.txt in doc
Add the following command to the CMakeLists.txt of the top-level engineering Catalog Hello:
[CPP]View PlainCopy
- INSTALL (FILES COPYRIGHT README DESTINATION Share/doc/cmake_demo)
- INSTALL (PROGRAMS run.bat DESTINATION bin)
- INSTALL (PROGRAMS bin/hello.exe DESTINATION bin)
- INSTALL (DIRECTORY doc/destination Share/doc/cmake_demo
These commands indicate that when the make install command is executed, the installer copies the corresponding file, directory, or program to the directory where the specified prefix begins.
Re-build CMake
Cmake-g "MinGW makefiles"-dcmake_install_prefix=c:\cmakedemo. #指定前缀c: \cmakedemo Build
Make install# Installation Project
tree/f c:\cmakedemo# Viewing the installation directory
http://blog.csdn.net/xiaopangzi313/article/details/53117923
http://blog.csdn.net/xiaopangzi313/article/details/53115702
Using CMAKE+MINGW configuration under Windows makefile