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

保存,编译运行后:

相关推荐
樱木Plus2 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_5 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星5 天前
虚函数表:C++ 多态背后的那个男人
c++
追逐时光者6 天前
精选 5 款基于 .NET 开源的 Visual Studio 实用插件
visual studio
端平入洛7 天前
delete又未完全delete
c++
端平入洛8 天前
auto有时不auto
c++
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端