save code
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
# 抽卡世界 GUI 教程 —— 总 CMake
|
||||
# 用法:cmake -B build -DLESSON=1 ← 切到第 1 课(默认 1)
|
||||
# cmake --build build
|
||||
# ./build/gacha1 ← Mac/Linux
|
||||
# build\Release\gacha1.exe ← Windows
|
||||
#
|
||||
# 想看「老师写好的完整版」:cmake -B build -DLESSON=1 -DUSE_SOLUTION=ON
|
||||
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(gacha LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Windows MSVC:让源码里的中文(UTF-8)正确编译
|
||||
if(MSVC)
|
||||
add_compile_options("/utf-8")
|
||||
endif()
|
||||
|
||||
# ===== 找 FLTK =====
|
||||
# 先试 CONFIG(vcpkg / 自己编 FLTK 1.4 时会有 FLTKConfig.cmake)
|
||||
# 找不到再回退到 CMake 自带的 FindFLTK 模块(Homebrew 走这条)
|
||||
find_package(FLTK CONFIG QUIET)
|
||||
if(NOT FLTK_FOUND)
|
||||
find_package(FLTK REQUIRED)
|
||||
endif()
|
||||
|
||||
# ===== 选课 =====
|
||||
set(LESSON 1 CACHE STRING "Which lesson to build: 1, 2, 3, 4, 5, 6")
|
||||
option(USE_SOLUTION "Build from solutions/ instead of student starter file" OFF)
|
||||
|
||||
set(TARGET_NAME gacha${LESSON})
|
||||
|
||||
if(USE_SOLUTION)
|
||||
set(SRC_FILE solutions/lesson_gui_${LESSON}.cpp)
|
||||
else()
|
||||
set(SRC_FILE lesson_gui_${LESSON}.cpp)
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/${SRC_FILE}")
|
||||
message(FATAL_ERROR "找不到源文件:${SRC_FILE}\n请确认 LESSON=${LESSON} 是否正确,或文件是否存在。")
|
||||
endif()
|
||||
|
||||
message(STATUS "Building lesson ${LESSON}: ${SRC_FILE}")
|
||||
|
||||
add_executable(${TARGET_NAME} ${SRC_FILE})
|
||||
|
||||
if(TARGET fltk)
|
||||
# CONFIG 模式:imported target 自带头文件路径
|
||||
target_link_libraries(${TARGET_NAME} PRIVATE fltk)
|
||||
else()
|
||||
# 模块模式:手动加头文件路径和库
|
||||
target_include_directories(${TARGET_NAME} PRIVATE ${FLTK_INCLUDE_DIR})
|
||||
target_link_libraries(${TARGET_NAME} PRIVATE ${FLTK_LIBRARIES})
|
||||
# Homebrew 的 FLTK 头文件是 *.H,教程里写 #include <FL/Fl.h> 会触发可移植性警告
|
||||
# 在 AppleClang 下静音这个无害告警
|
||||
if(APPLE)
|
||||
target_compile_options(${TARGET_NAME} PRIVATE
|
||||
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-nonportable-include-path>)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# macOS SDK 把 sprintf 标为 deprecated(让用 snprintf)
|
||||
# 对 11 岁初学者来说 sprintf 已经够用且写法简单,统一屏蔽这个告警
|
||||
if(APPLE)
|
||||
target_compile_options(${TARGET_NAME} PRIVATE
|
||||
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-deprecated-declarations>)
|
||||
endif()
|
||||
Reference in New Issue
Block a user