cmake的一个测试demo

目录

  • 一、ubuntu中安装cmake
  • 二、单个源文件
    • main.c
    • CMakeLists.txt的编写
  • 三、多个源文件
    • main.c
    • test1.c
    • test1.h
    • CMakeLists.txt的编写

一、ubuntu中安装cmake

sudo apt-get install cmake

cmake的一个测试demo_第1张图片

  • 查看cmake的版本号
cmake --version

在这里插入图片描述

二、单个源文件

main.c

#include

int main()
{
    printf("this is my first cmake test\n");
    return 0;
}

CMakeLists.txt的编写

  • 不能是cmakelist.txt,应该是CMakeLists.txt
    在这里插入图片描述
mv cmakelists.txt CMakeLists.txt

在这里插入图片描述

  • 生成Makefile
cmake .

cmake的一个测试demo_第2张图片

  • 生成可执行文件
make

在这里插入图片描述

  • 执行编译生成的可执行文件
    在这里插入图片描述

三、多个源文件

main.c

#include"test1.h"

int main()
{
    printf("this is my first cmake test\n");
    print_test_1();
    return 0;
}

test1.c

#include "test1.h"


void print_test_1()
{
    printf("this is in print_test_1 function\n");
}

test1.h

#ifndef TEST_H_
#define TEST_H_
#include 
void print_test_1();

#endif

CMakeLists.txt的编写

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
 
# 项目信息
project (demo1)
 
# 指定生成目标
add_executable(Demo main.c test1.c)

cmake .
make

cmake的一个测试demo_第3张图片

你可能感兴趣的:(make,c语言)