编译文件
cmake --build ./build --config Release
自己的的通用模板,可以用在vscode
#CmakeLists.txt
#设置版本号
cmake_minimum_required(VERSION 3.5)
#设置项目名称和语言
project(myEngine VERSION 0.1.0 LANGUAGES C CXX)
#包含测试
include(CTest)
enable_testing()
#需要的包含目录,这里是头文件,cmake会自动去找这些头文件,这里路径都是相对于CmakeLists.txt的位置
include_directories(include)
include_directories(include/bullet)
include_directories(include/freetypefont)
include_directories(include/freetypefont/freetype)
include_directories(include/freetypefont/freetype/config)
# 有了header,还需要lib
# 添加lib的文件夹,必须在add_executable之前
link_directories(src/libs)
# 设置需要编译的文件路径
set(SOURCES
src/main.cpp
src/Assets/Camera.cpp
src/Assets/LightRenderer.cpp
src/Assets/Mesh.cpp
src/Assets/ShaderLoader.cpp
src/Assets/MeshRenderer.cpp
src/Assets/TextureLoader.cpp
src/Assets/TextRenderer.cpp
)
# 添加到自己的项目
add_executable(myEngine ${SOURCES})
# c++版本
set_property(TARGET myEngine PROPERTY CXX_STANDARD 17)
# 这里以后改成相对路径
# 相对路径参考示例:${CMAKE_CURRENT_SOURCE_DIR}/src/libs/glfw3.lib
# 一次添加一个lib的方式
add_library(MyLibName STATIC IMPORTED)
set_target_properties(MyLibName
PROPERTIES IMPORTED_LOCATION
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64/OpenGL32.Lib")
# 回答说下面的find_library方式更好,但这里一次只有一个。
# find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu)
# target_link_libraries(test PUBLIC ${PROTOBUF_LIBRARY})
# 会自动把文件里面带lib的给你找齐
target_link_libraries(myEngine glew32 glfw3 glfw3_mt glfw3dll BulletCollision BulletDynamics LinearMath freetype)
target_link_libraries(myEngine MyLibName)
cmake本身的一些参数设置模板,也是用在vscode
// CmakePresets.json
{
"version": 3,
"configurePresets": [
{
"name": "default",
"description": "Default Settings for my engine",
"hidden": false,
"generator": "Visual Studio 17 2022",
"binaryDir": "${workspaceFolder}/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES"
}
}
]
}
标签:src,简要,Cmake,Assets,lib,directories,配置,cpp,include
From: https://www.cnblogs.com/zhaobangliu/p/18385509