自学内容网 自学内容网

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

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


原文地址:https://blog.csdn.net/Antonio915/article/details/142309087

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!