Learn how to use cmake to build a project

Source: Internet
Author: User

This document describes how to use cmake.

Install cmake refer to: http://blog.csdn.net/alex_my/article/details/17310001 this article uses win7x64, vs2012. Refer to this article: http://blog.csdn.net/fan_hai_ping/article/details/8208898 the code used in this article has been packaged: http://download.csdn.net/detail/alex_my/6904069 please be patient a line to see it! Directory: 1. Example of a single file; 2. Examples of multiple files; 3. Example of generating a library file; 4. Examples of multiple directories; 5. Example of specifying an output directory; 6. Compile a dynamic library example. 7. Use cmake to generate an SLN project; 0. Some information: the SET command is used to set the variable add_executable to tell the project to generate an executable file add_library to tell the project to generate a library file 1. Example of a single file: 1) Create the folder cmaketest/src 2) create two files main.cand cmakelists.txt in the folder src3) Main. C: # include <stdio. h>

Int main ()
{
Printf ("Hello world .");
Getchar ();
Return 0;
} 4) cmakelists.txt Project (Hello)
Set (src_list main. c)
Add_executable (Hello $ {src_list}) 5) Open: start -- Microsoft Visual Studio 2012 -- Visual Studio Tools -- vs2012 x86 native tools command prompt 6) and go to the cmaketest/build/src directory, run the following code mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake running results are shown in the following two figures: # insert image cmake_step11.png # insert image cmake_step12.png 7) generate hello.exe under src/build. 2. Multi-file Example 1) create a folder cmaketest/src2 2) create three files in the folder src2: Main. c, hello. h, hello. C 3) Hello. h # ifndef _ src2_hello_h __
# DEFINE _ src2_hello_h __

Void hello2 (const char * text );

# Endif/_ src2_hello_h _ 4) Hello. C # include "Hello. H"
# Include <stdio. h>

Void hello2 (const char * text)
{
If (text)
Printf ("Hello % s! \ N ", text );
} 5) Main. C # include "Hello. H"

Int main ()
{
Hello2 ("Alex ");

Getchar ();
Return 0;
}; 6) cmakelists.txt Project (Hello)
Set (src_list main. c hello. c)
Add_executable (Hello $ {src_list}) 7) Go to src3 through vs2012 x86 native tools command prompt and run the same code as 1: mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake 8) generate hello.exe under src3/build. 3. Create the cmaketest/src3 folder. 2) copy the hello. C, hello. H, Main. C, and cmakelists.txt files in the src2 directory to src3. 3) Modify cmakelists.txt Project (Hello)
Set (src_libhello hello. c)
Set (src_app main. c)
Add_library (libhello $ {src_libhello })
Add_executable (Hello $ {src_app })
Target_link_libraries (Hello libhello) 4) Go to src3 through vs2012 x86 native tools command prompt and run the same code as 1: mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake 5) generate hello.exe under src3/build. 6) If you want to specify the name of the generated library, you can add set_target_properties (libhello properties output_name "hello") 4. Multiple directory Example 1) create a folder cmaketest/src4, create the lib and SRC folders in src4. 2) Copy hello. h and hello. c In src2 to src4/lib. 3) Copy main. c from src2 to src4/src. 4) Create a New cmakelists.txt file in the src4root directory. Content: Project (Hello)

Add_subdirectory (SRC)

Add_subdirectory (LIB) 5) create a new file cmakelists.txt in src4/lib. Content: cmake_minimum_required (version 2.8)

Set (src_lib hello. c)

Add_library (libhello $ {src_lib}) 6) create a new price cmakelists.txt in src4/src. Content: cmake_minimum_required (version 2.8)

Include_directories ($ {project_source_dir}/LIB)

Set (src_app main. c)

Add_executable (Hello $ {src_app })

Target_link_libraries (Hello libhello) 7) Go to src4 through vs2012 x86 native tools command prompt and run the same code as 1: mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake 8) generate hello.exe under src4/build. 9) add_subdirectorycommand javascmaketo find the available cmakelists.txt file in the sub-directory. The include_directories command is used to specify the path of the header file. 5. Specify the output directory Example 1) output the execution file and library file to the specified file. They can be stored in different directories, or in the same directory. This article is stored in the same directory. 2) Create the cmaketest/src5 folder. 3) copy the content in src4 to src5 in addition to build. 4) Modify cmakelists.txt in src5/lib. Cmake_minimum_required (version 2.8)

Set (src_lib hello. c)

Add_library (libhello $ {src_lib })

# Add the following sentence. Note that/bin should be close to "}". If the bin Folder does not exist, it will be automatically created.
Set (library_output_path $ {project_binary_dir}/bin) 5) Modify cmakelists.txt in src5/src. Cmake_minimum_required (version 2.8)

Include_directories ($ {project_source_dir}/LIB)

Set (src_app main. c)

# Add this sentence to store Lib in the same directory as the execution file, or in different directories.
Set (executable_output_path $ {project_binary_dir}/bin)

Add_executable (Hello $ {src_app })

Target_link_libraries (Hello libhello) 6) Access src5 through vs2012 x86 native tools command prompt and execute the same code as 1: mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake 7) generate hello.exe under src5/build/bin. 6. Compile the dynamic library example 1) create a folder cmaketest/src6. 2) copy the content in src5 to src6 in addition to build. 3) modify the content of Hello. h In the src6/lib Folder: # ifndef _ hello_h __
# DEFINE _ hello_h __

# Ifdef Win32

# If libhello_build
# Define libhello_api _ declspec (dllexport)
# Else
# Define libhello_api _ declspec (dllimport)
# Endif // # ifdef libhello_build

# Else
# Define libhello_api
# Endif // # ifdef Win32


Libhello_api void hello2 (const char * text );

# Endif/_ hello_h _ 7) modify the cmakelists.txt content in the src6/libfolder: cmake_minimum_required (version 2.8)

Set (src_lib hello. c)

Add_definitions ("-dlibhello_build ")

Add_library (libhello shared $ {src_lib })

Set (library_output_path $ {project_binary_dir}/bin) 8) Go to src6 through vs2012 x86 native tools command prompt and run the same code as 1: mkdir build & cdbuild cmake .. -G "nmake makefiles" nmake 9) generate hello.exe under src6/build/bin. Run the libhello.dlland libhello.liblibraries under src6/build/libto the bindirectory and run hello.exe. 7. Use cmake to generate the SLN project 1.) The above are some basic applications. Now we will generate the SLN project for direct use by, it can also be generated for xcode or Linux. 2) Local configuration: win7 x64 and vs2012 x64. Cmaketest/src5 is used as an example. 3) Run cmake to include src5 in the source file, create a build directory under src5 (or do not create it first, it will be generated automatically), and include it in the generated file. # Configure cmake_step71.png 4) The Configure button indicated by the red box in the running state. In the pop-up box, select Visual Studio 11 win64 (select as needed. Note that the vs2012 version is 110, therefore, select 11. The version number corresponding to vs2010 is 100. Therefore, select 10 ). Click Finish in the red box. # Configure cmake_step72.png 5) as shown in Figure 5. If an error occurs, a prompt is displayed in the red box. Click the Configure button again. # Configure cmake_step73.png 6) as shown in Figure 6. Click Generate. # Configure cmake_step74.png 7) Open cmaketest/src5/build and the generated hello. sln is displayed. # Configure cmake_step75.png 8) Open the project with vs2012, select all_build as the startup item, and compile. # Matching cmake_step76.png

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.