【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中载入头文件:

#include"funcs/func.h"

保存,编译运行后:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值