【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"

保存,编译运行后:

相关推荐
端平入洛17 小时前
auto有时不auto
c++
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
蜡笔小马2 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost