Using CMAKE+MINGW configuration under Windows makefile

Source: Internet
Author: User

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 
    1. Cmd
    2. mkdir Hello
    3. CD Hello
    4. 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 
    1. CD src
    2. 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  
    1. Hello.h
    2. #ifndef _hello_h_
    3. #define _hello_h_
    4. extern int hello ();
    5. #endif

In main, add the main program main.cpp as follows:

[CPP]View PlainCopy  
    1. #include <iostream>
    2. #include "hello.h"
    3. int main () {
    4. Hello ();
    5. return 0;
    6. }

Add the Hello.cpp as follows in Util:

[CPP]View PlainCopy  
    1. #include <iostream>
    2. Using namespace std;
    3. int Hello () {
    4. cout << "Hello word cmake!!!" << Endl;
    5. return 0;
    6. }

3. Create the relevant configuration file CMakeLists.txt:
Add the CMakeLists.txt in the top-level directory Hello as follows:

[CPP]View PlainCopy 
    1. <pre name="code" class="cpp" >project (HELLO) #设置工程名
    2. Add_subdirectory (SRC)
    3. Cmake_minimum_required (VERSION 3.7) #设置版本号
    4. MESSAGE (STATUS "This is BINARY dir" ${hello_binary_dir})
    5. 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  
    1. Add_subdirectory (Util)
    2. Add_subdirectory (Main)
    3. This setting again recursively calls the CMakeLists.txt in main and util

Add CMakeLists.txt to the main directory as follows:

[CPP]View PlainCopy 
    1. SET (Executable_output_path ${hello_source_dir}/bin)
    2. SET (Src_list main.cpp)
    3. Include_directories (${hello_source_dir}/include)
    4. Link_directories (${hello_source_dir}/lib)
    5. Add_executable (Hello ${src_list})
    6. 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  
    1. SET (Library_output_path ${hello_source_dir}/lib)
    2. SET (Cmake_c_compiler g++)
    3. SET (Src_list hello.cpp)
    4. Include_directories (${hello_source_dir}/include)
    5. 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 
    1. INSTALL (FILES COPYRIGHT README DESTINATION Share/doc/cmake_demo)
    2. INSTALL (PROGRAMS run.bat DESTINATION bin)
    3. INSTALL (PROGRAMS bin/hello.exe DESTINATION bin)
    4. 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

Related Article

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.