A small example of cmake

Source: Internet
Author: User

CMake, better choice than handwriting makefile.

Install CMake, this section skips

First, create a new project

Here I am using eclipse in Windows to create a new C project (PS: I generally create a new makefile type of project, so that relatively clean)

Ii. creating the necessary folders

My Project Catalog:


d:\code\cpp\cmakestudy\test>tree/f Volume Software Folder The PATH list volume sequence number is 0006-17b7d:.│  . cproject│  . project│  cmakelists.txt│├─bin├─include│      sum.h│├─lib└─src    │  CMakeLists.txt    │    ├─main    │      CMakeLists.txt    │      main.c    │    └─sum            CMakeLists.txt            sum.cd:\code\cpp\cmakestudy\test >
Bin: The executable file used to store the build


Include: Storing header files

Lib: Storing the generated library files

SRC: Source code

Third, write the source codeInclude/sum.h


#ifndef include_sum_h_#define include_sum_h_int SUM (int a, int b), #endif/* include_sum_h_ */



Src/sum/sum.c


#include ". /.. /include/sum.h "int sum (int a, int b) {return a + B;}



Src/main/main.c


#include <stdio.h> #include ". /.. /include/sum.h "int main (void) {int a = 0;int B = 0;puts (" Enter two integers: "), scanf ("%d%d ", &a,&b);p rintf ("%d +%d =%d\n ", b , sum (A, b)); return 0;}



Iv. Preparation of CMakeLists.txt
Top Floor CMakeLists.txt


# define Project name Project (HELLO) # define subdirectory src, used recursively to invoke Makelists.txtadd_subdirectory (SRC) in src



Src/cmakelists.txt


# define subdirectory add_subdirectory (main) #定义子目录add_subdirectory (sum)



Src/sum/cmakelists.txt


# set compiler set (Cmake_c_compiler gcc) # source file list set (Src_list sum.c) # header file directory include_directories (${hello_source_dir}/include) # Sets the path of the generated library file set (Library_output_path ${hello_source_dir}/lib) # Generated library file add_library (sum STATIC ${src_list})



Src/main/cmakelists.txt


# source file list set (Src_list main.c) # header file List include_directories (${hello_source_dir}/include) # Sets the path set for the generated executable (executable_ Output_path ${hello_source_dir}/bin) # generated executable add_executable (HELLO ${src_list}) # Directory of required library files link_directories (${ Hello_source_dir}/lib) # requires a linked library file target_link_libraries (hello sum)




V. using CMake to generate makefile

Create a new build directory in your project, and now the project directory:


d:\code\cpp\cmakestudy\test>tree/f Volume Software Folder The PATH list volume sequence number is 0006-17b7d:.│  . cproject│  . project│  cmakelists.txt│├─bin├─build├─include│      sum.h│├─lib└─src    │  CMakeLists.txt    │    ├─main    │      CMakeLists.txt    │      main.c    │    └─sum            CMakeLists.txt            sum.cd:\code\cpp\cmakestudy\ Test>
Execute the cmkae command:



D:\code\cpp\cmakestudy\test\build>cmake. -G "MinGW makefiles"--the C compiler identification are GNU 4.7.1--The CXX compiler identification is GNU 4.7.1--Check fo R working C compiler:d:/program/cpp/mingw/bin/gcc.exe--Check for working C Compiler:d:/program/cpp/mingw/bin/gcc.exe- -works--detecting C compiler ABI info--detecting C compiler ABI info-done--detecting C compile features--detecting C compile features-done--Check for working CXX compiler:d:/program/cpp/mingw/bin/g++.exe--check for working CXX Compi Ler:d:/program/cpp/mingw/bin/g++.exe--works--detecting CXX compiler ABI info--detecting CXX compiler Abi info-done- -Detecting CXX Compile features--detecting CXX compile Features-donecmake Warning (dev) in CMakeLists.txt:No cmake_m  inimum_required command is present.  A line of code such as cmake_minimum_required (VERSION 3.1) should is added at the top of the file. The version specified may is lower if you wish to support older CMake versions foR this project. For more information run "CMake--help-policy CMP0000".  This warning are for project developers. Use-wno-dev to suppress it.--configuring done--generating done--Build files has been written To:d:/code/cpp/cmakestu Dy/test/buildd:\code\cpp\cmakestudy\test\build>


Now the project directory:


d:\code\cpp\cmakestudy\test>tree/f Volume Software folder PATH list volume serial number is 0006-17B7D:.│.CPROJECT│.PROJECT│CMAKELISTS.TXT│├─BIN├─B Uild││cmakecache.txt││cmake_install.cmake││makefile│││├─cmakefiles│││cmake.check_cache│││cmake  Directoryinformation.cmake│││cmakeoutput.log│││makefile.cmake│││makefile2│││progress.marks│││ targetdirectories.txt│││││├─3.1.0││││cmakeccompiler.cmake││││cmakecxxcompiler.cmake││││cm Akedeterminecompilerabi_c.bin││││cmakedeterminecompilerabi_cxx.bin││││cmakerccompiler.cmake││││cma kesystem.cmake│││││││├─compileridc││││a.exe││││cmakeccompilerid.c│││││││└─ Compileridcxx│││a.exe│││cmakecxxcompilerid.cpp│││││└─cmaketmp│└─src││cmake_i Nstall.cmake││makefile│││├─cmakefiles││cmakedirectoryinformation.cmake││progre Ss.marks│││├─main│││cmake_install.cmake│││makefile│││││└─cmakefiles│││cmakedirectory      Information.cmake│││progress.marks│││││└─hello.dir││build.make│              │c.includecache││cmake_clean.cmake││depend.internal││ Depend.make││dependinfo.cmake││flags.make││includes_              C.rsp││link.txt││linklibs.rsp││objects.a││ Objects1.rsp││progress.make│││└─sum││cmake_install.cmake││makefile              │││└─cmakefiles││cmakedirectoryinformation.cmake││progress.marks│                      ││└─sum.dir│build.make│c.includecache│ Cmake_clean.cmake│                     Cmake_clean_target.cmake│depend.internal│depend.make│                      Dependinfo.cmake│flags.make│includes_c.rsp│    Link.txt│progress.make│├─include│sum.h│├─lib└─src│cmakelists.txt│├─main │cmakelists.txt│main.c│└─sum CMakeLists.txt Sum.cd:\code\cpp\cmakestudy\test >




Vi. execute make and run the programMake


d:\code\cpp\cmakestudy\test\build>makescanning dependencies of Target sum[50%] Building C Object Src/sum/cmakefiles /sum.dir/sum.objlinking C Static Library. \.. \.. \lib\libsum.a[50%] Built target sumscanning dependencies of target hello[100%] Building C Object Src/main/cmakefiles/hell O.dir/main.oblinking C executable. \.. \.. \bin\hello.exe[100%] Built Target hellod:\code\cpp\cmakestudy\test\build>
Now the project directory:



d:\code\cpp\cmakestudy\test>tree/f Volume Software folder PATH list volume serial number is 0006-17b7d:.│.cproject│.project│cmakelists.txt│├─bin│ Hello.exe│├─build││cmakecache.txt││cmake_install.cmake││makefile│││├─cmakefiles│││cmake.check_ca Che│││cmakedirectoryinformation.cmake│││cmakeoutput.log│││makefile.cmake│││makefile2│││progr ess.marks│││targetdirectories.txt│││││├─3.1.0││││cmakeccompiler.cmake││││cmakecxxcompiler.cm Ake││││cmakedeterminecompilerabi_c.bin││││cmakedeterminecompilerabi_cxx.bin││││cmakerccompiler.cma   Ke││││cmakesystem.cmake│││││││├─compileridc││││a.exe││││cmakeccompilerid.c││ │││││└─compileridcxx│││a.exe│││cmakecxxcompilerid.cpp│││││└─cmaketmp│└─src      ││cmake_install.cmake││makefile│││├─cmakefiles││cmakedirectoryinformation.cmake│ │progress.markS│││├─main│││cmake_install.cmake│││makefile│││││└─cmakefiles││              │cmakedirectoryinformation.cmake│││progress.marks│││││└─hello.dir││ Build.make││c.includecache││cmake_clean.cmake││depend.int              Ernal││depend.make││dependinfo.cmake││flags.make││      Includes_c.rsp││link.txt││linklibs.rsp││main.obj│          │objects.a││objects1.rsp││progress.make│││└─sum│ │cmake_install.cmake││makefile│││└─cmakefiles││cmakedirectoryinformat                      Ion.cmake││progress.marks│││└─sum.dir│build.make│ C.includeCache│cmake_clean.cmake│cmake_clean_target.cmake│depend.                      Internal│depend.make│dependinfo.cmake│flags.make│ Includes_c.rsp│link.txt│progress.make│sum      . obj│├─include│sum.h│├─lib│libsum.a│└─src│cmakelists.txt│├─main│cmakelists.txt│ Main.c│└─sum CMakeLists.txt sum.cd:\code\cpp\cmakestudy\test>




Execute the program:


D:\code\cpp\cmakestudy\test\bin>hello.exe input two integers: 2 + 3 = 5d:\code\cpp\cmakestudy\test\bin>


Vii. Summary of CMake

1. About the project directory structure

I don't really know what the directory structure should be, but when I look for CMake's information, I see that the directory structure of the project is almost include,src,bin,build. The different modules are then placed in different directories under SRC.

For me, is far from the kind of sub-module development, testing level, third-party library will not be a few, but will follow this directory structure to learn, but also recommend that you write a C + + program when you pay attention to the directory structure.

2, the top floor of the CMakeLists.txt

For this small example, this CMakeLists.txt actually has two effects. One is to define the name of the program, and the second is to define subdirectories.

The sentence of project (HELLO) also implicitly defines two CMake variables: <projectname>_binary_dir and <projectname>_binary_dir, this is Hello_ Binary_dir and Hello_source_dir, two variables refer to the path of the current project.

Add_subdirectory (SRC), which defines the subdirectories contained in the current directory to invoke CMakeLists.txt in its subdirectories. In fact, look at the directory results should be able to see, for the directory containing CMakeLists.txt subdirectories, its CMakeLists.txt should have a number of add_subdirectory (SRC).

3, CMakeLists.txt of each module in SRC

The essential parts are:

The source file list, the directory where the header file is located, the path and name of the generated library file, and if you need to link other library files, such as various third-party libraries, you need to give the directory and library file where the header files of the third-party library are located.

4. CMakeLists.txt of the folder where the main function resides

Similar to each module, just a few more directories and names for executable file generation.

PS: About the required library directory and library file name this part, you need to write to the last, otherwise there will be problems, for the moment I do not know why.

5, the next step to learn CMake goals

A.cmake commands, including commands that you learn frequently, about project settings such as generating installers, doc documents, etc.

B. Third-party libraries, to do this step, you need to first use third-party libraries, such as gtk+,libxml, etc.


Viii. References:

Http://www.cppblog.com/Roger/archive/2011/11/17/160368.html

http://blog.csdn.net/dbzhang800/article/details/6314073

A small example of cmake

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.