【CMake】使用CMake在Visual Studio内构建多文件夹工程

一、配置准备

打开VIsual Studio,载入写好的 C M a k e l i s t s . t x t CMakelists.txt CMakelists.txt,在项目中添加以下文件:

创建一个文件夹 f u n c s funcs funcs,里面放入 f u n c . h func.h func.h、 f u n c . c p p func.cpp func.cpp、 C M a k e l i s t s . t x t CMakelists.txt CMakelists.txt文件。

接着,配置主 C M a k e CMake CMake文件:

复制代码
#需求的最低cmake程序版本
cmake_minimum_required(VERSION 3.12)

#本工程的名字
project(OpenGL)

#支持的C++版本
set(CMAKE_CXX_STANDARD 20)

#搜所有的cpp,加入SRCS变量中
aux_source_directory(. SRCS)

#本工程主程序文件及输出程序名称,生成exe
add_executable(glStudy ${SRCS})

#将funcs文件夹添加为子文件夹
add_subdirectory(funcs) 

#添加myFuncs链接库
target_link_libraries(glStudy myFuncs)

然后再 f u n c s funcs funcs文件夹内也创建一个 C M a k e l i s t s . t x t CMakelists.txt CMakelists.txt文件,配置信息如下:

复制代码
#递归将本文件夹下所有cpp放到FUNCS中
file(GLOB_RECURSE FUNCS ./  *.cpp)

#将FUNCS中所有cpp编译为funcs这个lib库
add_library(myFuncs ${FUNCS} )

在 m a i n . c p p main.cpp main.cpp中载入头文件:

cpp 复制代码
#include"funcs/func.h"

保存,编译运行后:

相关推荐
冻柠檬飞冰走茶2 分钟前
PTA基础编程题目集 7-20 打印九九口诀表(C语言实现)
c语言·开发语言·数据结构·算法
鱼子星_3 分钟前
《算 · 法》题目解析篇(1):最近公共祖先问题,线段树,最长回文子序列
c++·算法·动态规划·递归
Richard.Wong40 分钟前
qt生成dll供C#调用
开发语言·qt
wang_xin_8881 小时前
PHP函数
开发语言·php
Immortal__y1 小时前
php函数
开发语言·php
仙宇觉尘1 小时前
【译】Visual Studio Copilot 中更智能的人工智能编辑
人工智能·copilot·visual studio
AI砖家1 小时前
多商户多租户系统架构设计文档(Java版)
java·开发语言·系统架构·多租户·多商户
计算机内卷的N天2 小时前
CMake与Visual Studio的使用
c++·ide·visual studio
汉克老师2 小时前
GESP2026年3月认证C++七级( 第三部分编程题(1、物流网络))精讲
c++·最短路·gesp7级
鱼子星_2 小时前
【C++】深入剖析list:list及其双向迭代器实现
开发语言·数据结构·c++·笔记·stl·list