Using CMake to generate executable files under Ubuntu

Source: Internet
Author: User

1. CMake of a single CPP file

Start by writing a simple program (main.cpp):

#include <iostream>usingnamespace  std; int  Main () {cout<<"hello cmake"<<Endl;  return0;}

Write the CMakeLists.txt and put it in the same directory as the main.cpp.

Project (Hello) cmake_minimum_required (VERSION 2.8) add_executable (Hello main.cpp)

Enter the directory where CMakeLists.txt is located, create a build directory in the directory where CMakeLists.txt resides, and enter the build directory:

CMake. Make

After execution, there will be a lot of compiled files in the build directory, but only the executable files, if you want to put the executable file in the same folder as the build directory, then add the following two words at the end of the CMakeLists.txt:

Set (Cmake_install_prefix "${cmake_source_dir}/bin/") INSTALL (TARGETS Hello RUNTIME DESTINATION ${cmake_install_ PREFIX})

Change the make command to do install, and when you are done, you can see the executable in the Bin folder Hello

2. Introduction of the Data folder

If the executable needs to call some files, assuming the file is in the Data folder, and data and CMakeLists.txt in the sibling directory, you can also put the data folder into the Bin folder by the following statement

Install (DIRECTORY data DESTINATION ${cmake_install_prefix})

3. CMake of multiple CPP and H files

If the content of another file is used in main.cpp, what should I do if the file directory is like?


Where hello.h is the declaration of a class, hello.cpp is the definition of a class function.
You can then change the CMakeLists.txt to the following:

Project (Hello) cmake_minimum_required (VERSION 2.8) Set (SRCs main.cpp hello.cpp) add_executable (Hello ${srcs})

What if Hello.cpp and main.cpp are not in the same folder?


There is a CMakeLists.txt under each folder.
The CMakeLists.txt in the Main.cpp sibling directory are as follows:

Project (Hello) cmake_minimum_required (VERSION 2.8) add_subdirectory (Hello) set (SRCs main.cpp) add_executable (Hello ${ SRCS}) target_link_libraries (Hello Hell)

The CMakeLists.txt under the Hello directory are as follows:

Add_library (Hell STATIC hello.cpp)

description : The add_subdirectory (hello) in the first CMakeLists.txt indicates that the Hello directory is included at compile time, while target_link_libraries (Hello Hell) Indicates that the static link library hell is called when the Hello is generated, and hell is generated in add_library (Hell STATIC hello.cpp) in CMakeLists.txt in the Hello directory.

In the same vein, if you use other link libraries in Hello.cpp, you will also want to join target_link_libraries (Hell XXX) in their CMakeLists.txt.
Note : When writing a target_link_libraries statement, the first name inside the parentheses must be the name of the currently required makefile, such as the above Hello and hell. However, this is written in the reference header file to be written #include "hello/hello.h", appears too long, at this time, you can add the following sentence in the CMakeLists.txt:

Include_directories (./hello)

This can be written as #include "hello.h" by referencing the header file.
More ways to use Baidu itself.

Using CMake to generate executable files under Ubuntu

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.