編譯檔案
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"
}
}
]
}