Improve: ship release builds without debug info and symbols

linux/CMakeLists.txt forced -g into the global flags regardless of build
type, so even a Release build carried full DWARF: 7.7MB of the 10.3MB
binary was .debug_* material the client never uses at runtime.

Drop the forced -g and default CMAKE_BUILD_TYPE to Release, stripping the
symbol table at link time (-s) so only Debug builds keep symbols. Do the
same for macOS with ld64's -Wl,-x on Release, which drops the 8.3k local
symbols while keeping external ones and the signature ld appends.

linux/ghost drops from 10.3MB to 2.5MB (UPX on top: 0.87MB). Rebuilt
under WSL and verified with readelf: no .debug_*/.symtab/.strtab, BuildID
kept. The macOS change is not verified here; it needs a build on the Mac.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-20 17:40:00 +01:00
1 parent 326e162d16
commit 913b74097b
3 files changed
+22 -3

No files matched your search

+9 -3
View File
@@ -35,9 +35,15 @@ set(SOURCES
add_executable(ghost ${SOURCES})
# 设置为可以调试
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
# 默认使用 Release:无调试信息、-O3,链接时剥离符号表(ghost 约 2.5MB);
# 需要 gdb 调试时用:cmake -DCMAKE_BUILD_TYPE=Debug . && make
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
# 注意:这里不要再往 CMAKE_CXX_FLAGS 里塞 -g,否则 Release 也会带上 DWARF
# (ghost 会涨到 10MB 以上,其中 75% 是 .debug_* 段)。
target_link_options(ghost PRIVATE $<$<CONFIG:Release>:-s>)
# 链接 ZSTD 库
message(STATUS "链接库文件: ${CMAKE_SOURCE_DIR}/lib/libzstd.a")
+8
View File
@@ -140,6 +140,14 @@ make -j$(nproc)
编译成功后生成可执行文件 `ghost`。
默认按 Release 构建(`-O3`,链接时 `-s` 剥离符号表,产物约 2.5MB,不含调试信息)。
需要 gdb 调试或保留 DWARF 时改用:
```bash
cmake -DCMAKE_BUILD_TYPE=Debug . # -g(未优化)
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo . # -O2 -g -DNDEBUG
```
## 使用方法
### 基本用法
+5
View File
@@ -124,6 +124,11 @@ target_compile_options(ghost PRIVATE
-fobjc-arc
)
# Release 链接时删掉 local 符号(对应 Linux 那边的 -s;ld64 用 -x)。
# 只删 non-global 符号,external 符号保留,dyld 绑定不受影响;签名由 ld 在链接
# 末尾完成,所以改符号表不会让签名失效。ghost 目前 ~19k 个符号里 8.3k 是 local。
target_link_options(ghost PRIVATE $<$<CONFIG:Release>:-Wl,-x>)
# Output directory
set_target_properties(ghost PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin