Learn how to use cmake

Source: Internet
Author: User

Preliminary StudyCmakeUse

 

Transferred from:

Http://hi.baidu.com/%D4%B6%B9%C5%B5%A5%CE%BB/blog/item/29888288267e5f90a5c27270.html

After studying cmake over the past few days, we feel that we will need this tool one day in the future development.

Cmake

Cmake no longer makes you feel depressed when building a project.-a kde developer.

1. Background Knowledge:
Cmake is a derivative of kitware and some open-source developers in developing several tool kits (VTK). It eventually forms a system and becomes an independent open-source project. The project was born in 2001. Its official website is www.cmake.org. You can visit the official website to obtain more information about cmake. The popularity of cmake is actually attributed to the development of kde4 (it seems that like Svn in the past, Kde migrated the code repository from CVS to SVN and proved the availability of SVN management for large projects ), after KDE developers used autotools for nearly 10 years, they finally decided to select a new engineering build tool for kde4. The root cause is that KDE developers: only a few "Compilation experts" can master the current KDE build system (admin/makefile. after unsermake, scons, and cmake selections and attempts, kde4 decided to use cmake as its own build system. During the migration process, the process went exceptionally smooth and was supported by cmake developers. Therefore, the current kde4 development version has been fully built using cmake. Projects such as kdesvn and rosegarden also begin to use cmake, Which is doomed to become a mainstream building system.

2. Features
Cmake has the following features:
1. Open source code (BSD license ). 2. cross-platform. 3. Ability to manage large projects. 4. Simplify the compilation process and compilation process. 5. high efficiency. 6. scalable.

3. Install cmake
In ubuntu, run sudo apt-Get install cmake directly. The cmake version I installed is 2.4-patch 6 and Ubuntu version 7.04.

4. Easy to use
Create a temporary directory mkdir-P cmake/T1 & CD cmake/T1, and write the simplest Program Main. C in this folder.
Main. c file content:
// Main. c
# Include <stdio. h>
Int main ()
{
Printf ("Hello world from T1 Main! \ N ");
Return 0;
}
Write a file in the same directory of your website. The file name is cmakelists.txt( ), and the content of cmakelists.txt is:
Project (Hello)
Set (src_list main. c)
Message (status "this is binary dir" $ {hello_binary_dir })
Message (status "this is source dir" $ {hello_source_dir })
Add_executable (Hello $ {src_list })

Cmakelists.txt. The script content is as follows:
# Identify the name of our project
Project (my_project)
# Add the include path of glib
Include_directories (/usr/include/Glib)
# Write all the file names in the common directory to the variable named common_var.
Aux_source_directory (Common common_var)
# Add main. C main. H to the project_file variable
Set (project_file main. C main. h)
# Put the content of common_var and project_file variables in the all_file variable.
Set (all_file $ {project_file }$ {common_var })
# Generate an executable file bt_server Based on all_file
Add_executable (bt_server $ {all_file })
# Link dynamic link libraries libglib2.0.so and libpthread. So
Target_link_libraries (bt_server libglib-2.0.so libpthread. So)

Well, the script compilation is finished. It's easy.
The following steps are simpler:
Create a directory under your project. The directory name is build # whatever name you want to write is good, but we recommend using build, which looks more formal :)

Enter the terminal, and then tap the command cmake... # Remember the two following points. The export cmakelists.txt file is stored in the parent directory.

If cmake is successful, you will see a makefile generated under the build directory. In this case, press make on the terminal. Your project will be compiled into an executable file.

4.1, start component
Run in this projectCmake.(. Represents the local directory)
Then the makefile of the file is generated.
Run laterMakeTo generate the hello program.

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.