Use CMake to build complex projects and cmake to build projects

Source: Internet
Author: User

Use CMake to build complex projects and cmake to build projects

0. What is CMake?

CMake is a cross-platform compilation, installation, testing, and packaging tool. CMake does not directly compile the software, but builds the software with the native build system. The cmakeconfiguration file is a cmakelist.txt file (each source code folder must have one). CMake generates Unix makefiles or VS solution files based on the configuration file.

1. Why CMake?

I don't know why others choose CMake. I am because it is too painful to directly write Makefile on Linux, and the project will be cross-platform.

2. Use a simple project to describe CMake usage.

  • Project name soTest, which contains two so (dll) files: so1 and so2, and a test project st
  • The file structure in the project is as follows:
    • File so1/so1.c: used to generate libso1.so
    • File so2/so2.c: used to generate libso2.so, dependent on libso1.so, will call the libso1.so Function
    • File test/test. c: generate an executable file, call libso2.so, and call the functions in libso2.so.
    • File test/test. py: Use python to call the libso2.so Function
    • The build directory is used to store the Compilation Time and files, so that the source code folder can be kept clean.
    • File structure
    • Each directory contains a cmakelist.txt file, which is the configuration file of CMake. CMake generates other build files based on these files.

3. The project code is as follows:

  

1 // so1/so1.c2 int add_fun(int a, int b)3 {4     return a+b;5 }
1 // so1.h2 #ifndef _so1_h_3 #define _so1_h_4 5 int add_fun(int a, int b);6 int fun_test(int a);7 8 #endif//_so1_h_
 1 // so2/so2.h 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include "so1.h" 5  6 int fun_test(int a) 7 { 8     for (int i=0; i<a; ++i) 9     {10         printf("add_fun(%d+%d)=%d\n", i, i, add_fun(i, i));11     }12     13     return 0;14 }
 1 // test/test.c 2 #include <stdio.h> 3 #include <stdlib.h> 4  5 #include "so1.h" 6  7 int main(int argc, char **argv) 8 { 9     return fun_test(9);10 }
1 #. /CMakeLists.txt, project configuration file 2 3 # minimum VERSION requirements, must be 4 cmake_minimum_required (VERSION 3.0) 5 6 # project name, here use soTest name 7 project (soTest) 8 9 # Add the include directory. In this example, close the file in. /, so take ${PROJECT_SOURCE_DIR}/10 include_directories ($ {PROJECT_SOURCE_DIR}/) 11 12 # Add the link library to find the directory, query 13 link_directories ($ {PROJECT_BINARY_DIR}/libs/usr. local/libs/usr/lib) 14 15 # set the generated library file directory. Here we set 16 sets (LIBRARY_OUTPUT_PATH $ {PROJECT_BINARY_DIR}/libs) as the same as the library directory) 17 18 # set the executable file directory. 19 sets (EXECUTABLE_OUTPUT_PATH $ {PROJECT_BINARY_DIR}/libs) 20 21 # Add file 22 add_subdirectory (so1) 23 add_subdirectory (so2) 24 add_subdirectory (test)
# ./so1/CMakeLists.txt# project name: soTestproject(soTest)# set source filesset(so1 so1.c)# set make to a shared libraryadd_library(so1 SHARED ${so1})
 1 # ./so2/CMakeLists.txt 2  3 project(soTest) 4 set(so2 so2.c) 5 add_library(so2 SHARED ${so2}) 6  7 # link so1 8 target_link_libraries(so2 so1)
1 project (soTest) 2 set (st test. c) 3 4 # generate the executable file 5 add_executable (st test. c) 6 target_link_libraries (st so2 so1)

4. CMake supports building out of the source file directory. Here we select the directory where the./keystore file is located ):

cmake .. && make && ./libs/st

After the command is run, the result is as follows:

  

Related Article

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.