CMake quick tutorial

Source: Internet
Author: User

CMake quick tutorial

If you think makefile is a headache, if the project is getting bigger and bigger, you are getting confused. If you get tired of running a lot of commands during compilation, so it is necessary for you to take ten minutes to view the following content.

1. HelloWorld

First create a test1 folder, which creates a main. c file with the following content:

# Include <stdio. h>
Int main ()
{
Printf ("Hello World! \ N ");
Return 0;
}

Create a cmakelists.txt File

PROJECT (HELLO)
SET (SRC_LIST main. c)
ADD_EXECUTABLE (hello $ {SRC_LIST })

Line 1: set the project name;

Row 2: Set the SRC_LIST value to main. c.

Row 3: generate the executable file hello. $ {} Refers to a value.

In Terminal, cd enters the test1 directory, creates a build directory for external build (the files generated by the compilation are generated in the build directory), and runs the following three commands in sequence:

Cmake ..
Make
./Hello

The result is as follows:

If you want to introduce the internal database, add

TARGET_LINK_LIBRARIES (hello SDL)
TARGET_LINK_LIBRARIES (hello GLU)

The compilation command of the corresponding terminal is:

-LSDL-lGLU

If the internal library is not referenced, add the corresponding directory to use the INCLUDE_DIRECTORIES command.

Ii. database construction and installation

Our goal this time is:

1. Create a static and dynamic library and provide HelloFunc functions for programming of other programs. HelloFunc outputs to the terminal.

Hello World string.

2. Install the header file and shared library.

The directory arrangement is as follows:

Build-for external compilation;

Source file of libhello-hello library;

Src-Main Program

First, check the files in libhello:

/* Filename: hello. h */
# Ifndef DBZHANG_HELLO _
# Define DBZHANG_HELLO _
Void hello (const char * name );
# Endif // DBZHANG_HELLO _

 

/* Filename: hello. c */
# Include <stdio. h>
# Include "hello. h"

Void hello (const char * name)
{
Printf ("Hello % s! \ N ", name );
}

CMakeLists.txt

Cmake_minimum_required (VERSION 2.8)
Set (LIB_SRC hello. c)
Add_library (libhello STATIC $ {LIB_SRC })
Set (LIBRARY_OUTPUT_PATH $ {PROJECT_BINARY_DIR}/lib)
Set_target_properties (libhello PROPERTIES OUTPUT_NAME "hello ")
Install (TARGETS libhello
Library destination lib
Archive destination lib)
Install (FILES hello. h DESTINATION include/hello)

Src folder

/* Filename: main. c */
# Include "hello. h"
Int main ()
{
Hello ("Jack ");
Return 0;
}

CMakeLists.txt

Cmake_minimum_required (VERSION 2.8)
Include_directories ($ {PROJECT_SOURCE_DIR}/libhello)
Set (APP_SRC main. c)
Set (EXECUTABLE_OUTPUT_PATH $ {PROJECT_BINARY_DIR}/bin)
Add_executable (main $ {APP_SRC })
Target_link_libraries (main libhello)

The most external cmakelists.txt

Project (HELLO)
Add_subdirectory (src)
Add_subdirectory (libhello)

Explanation:

A cmakelists.txt file must be created for each directory except the builddirectory.

Statement for generating the Library: add_library (libhello STATIC $ {LIB_SRC })

This statement is used to create a STATIC database. To create a dynamic database, change STATIC to SHARED.

The install command is used to install the library.

The result is as follows:

Sudo make install again.

After the library is installed, when we want to use the hello method, we only need to add the header file # include. during compilation

G ++ main. c-o main-lhello

You can reference the compiled library.

Ubuntu 12.04 uses CMake-2.8.10.4 to compile OpenCV-2.4.4

CMake is not ideal

CMake sets the cross-compilation environment

CMake for MySQL compilation and Installation

Compile and install MySQL 5.5 Based on CMake

CMake details: click here
CMake: click here

This article permanently updates the link address:

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.