main.cpp,factorial.cpp,printhello.cpp,founctions.h文件见Makefile笔记
Makefile学习笔记(ubuntu18+vscode)_ssz__的博客-CSDN博客
其中CMakeLists.txt文件内容为
cmake_minimum_required(VERSION 3.10)
project(hello)
add_executable(hello main.cpp factorial.cpp printhello.cpp)
法一:
执行cmake .命令,运行CMakeLists.txt文件
执行make命令,编译Makefile文件
生成的文件为:
运行hello可执行文件:

法二:
camke .后会生成许多中间文件,可以建立一个build文件来存放这些中间文件
cmake .. 的意思就是去上一层文件夹找CMakeLists.txt文件
执行make命令
查看可执行文件hello并运行

笔记参考视频:
CMake 6分钟入门,不用再写复杂的Makefile_哔哩哔哩_bilibili
补充:
当文件架构如下时:

main.cpp
#include <iostream>
#include <stdio.h>
#include "swap.hpp"
using namespace std;
int main(int argc, char const *argv[])
{
int a = 1, b = 2;
swap(&a,&b);
printf("a = %d, b = %d\n",a,b);
return 0;
}
swap.cpp
#include <iostream>
#include "swap.hpp"
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
swap.hpp
#pragma once
void swap(int *a, int *b);
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(swap)
include_directories(include)
add_executable(test_demo main.cpp src/swap.cpp)
若文件架构如下:

则CMakeLists.txt文件为:
cmake_minimum_required(VERSION 2.8)
project(swap)
include_directories(include)
add_executable(test_demo src/main.cpp src/swap.cpp)
文章介绍了如何在Ubuntu18环境下使用CMake和Makefile进行C++项目构建。通过CMakeLists.txt文件配置,包括添加源文件、设置项目名称和执行目标。文中提到了两种方法,一种是直接使用cmake和make命令,另一种是在build目录下管理中间文件。此外,还展示了不同项目结构下CMakeLists.txt的编写方式,并包含了一个简单的main.cpp和swap函数的例子。
&spm=1001.2101.3001.5002&articleId=129859747&d=1&t=3&u=c570532e69094e70ba969928ad80e80e)
1539

被折叠的 条评论
为什么被折叠?



