cmake-1

什么是cmake?#

百度官方:
CMake 是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的 Makefile 或者 project 文件,CMake 并不直接建构出最终的软件,而是产生标准的建构档(如 Makefile 或 projects)。

C/C++的编译工具之前用的是 g++ 

如何使用?#

参考:链接   链接
Windows和Linux下都可以使用,本次演示为Ubuntu环境下

g++编译#

1、安装g++
1
apt install g++

2、新建test.c

1
2
3
4
5
6
7
vim test.c
#include<stdio.h>
int main(void)
{
  printf("Hello, world!\n");
  return 0;
}

3、编译文件

1
g++ test.c -o test

 4、运行

1
./test

cmake 编译 #

1、安装cmake

1
apt install cmake

2、编写程序

(1)创建文件/文件夹

1
2
3
4
mkdir helloworld
cd helloworld
mkdir bin lib src include build
touch CMakeLists.txt

(2)、进入Src目录,新建源文件

1
2
cd src
touch main.cpp  helloworld.cpp

(3)、返回上级目录,进入include目录,新建头文件

1
2
cd ../include/
touch helloworld.h

(4)、对源文件和头文件进行编写并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//  main.cpp
#include <helloworld.h>
int main()
{
    helloworld obt;
    obt.outputWord();
    return 0;
}
  
//  helloworld.cpp
#include "helloworld.h"
void helloworld::outputWord()
{
    std::cout << "hello world!" << std::endl;
}
  
//  helloworld.h
#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
#include <iostream>
class helloworld
{
public:
    void outputWord();
};
#endif

(5)、编写CMakeLists.txt文件 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#cmake最低版本以及工程名称
cmake_minimum_required(VERSION 2.8)
project(helloworld)
 
#设置编译方式(“debug”与“Release“)
SET(CMAKE_BUILD_TYPE Release)
 
#设置可执行文件与链接库保存的路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
 
#设置头文件目录使得系统可以找到对应的头文件
include_directories(
${PROJECT_SOURCE_DIR}/include
)
 
#选择需要编译的源文件,凡是要编译的源文件都需要列举出来
add_executable(helloworld src/helloworld.cpp src/main.cpp)

(6)、编译

1
2
3
cd build
cmake ..
make

(7)、运行

1
2
ls bin
./helloworld

作者:Hang Shao

出处:https://www.cnblogs.com/pam-sh/p/13885959.html

版权:本作品采用「知识共享」许可协议进行许可。

声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.

posted @   PamShao  阅读(357)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu