我使用CMAKE将程序链接到SDL2,OpenGL,并将tinyxml2编译为共享库。生成的程序二进制文件是1.4 MB,但程序中只有1k行代码。我怀疑某些图书馆是静态链接的。我更喜欢将它们作为共享库链接。我试着打开和关闭调试符号:
cmake -DCMAKE_BUILD_TYPE=Debug .
和关闭:
cmake .
并且生成的二进制文件仍然是每个1.4 MB,这很奇怪,因为调试应该更大。这是我的CMakeLists.txt:
project(ProjectName)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake-find-scripts")
aux_source_directory(. SRC_ROOT)
aux_source_directory(./extlib SRC_EXTLIB)
aux_source_directory(./engine SRC_ENGINE)
aux_source_directory(./utils SRC_UTILS)
include_directories(./extlib)
include_directories(./engine)
include_directories(./utils)
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${OPENGL_INCLUDE_DIRS})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
set(LONE_HEADERS ./engine/stc_config.h)
add_executable(${PROJECT_NAME} ${SRC_ROOT} ${SRC_ENGINE} ${SRC_UTILS} ${LONE_HEADERS})
find_package(SDL2 REQUIRED)
find_package(SDL2IMAGE REQUIRED)
find_package(OpenGL REQUIRED)
add_library(tinyxml2 SHARED ./extlib/tinyxml2.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${OPENGL_LIBRARIES} ${SDL2IMAGE_LIBRARIES} tinyxml2)
编辑:我已使用ldd
命令检查了二进制文件链接的库文件,输出确认它们都是共享库:
linux-vdso.so.1 (0x00007fff9dbf0000)
libSDL2-2.0.so.0 => /usr/lib/libSDL2-2.0.so.0 (0x00007f3accff7000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f3accdda000)
libGLU.so.1 => /usr/lib/libGLU.so.1 (0x00007f3accb59000)
libGL.so.1 => /usr/lib/libGL.so.1 (0x00007f3acc7fb000)
libSDL2_image-2.0.so.0 => /usr/lib/libSDL2_image-2.0.so.0 (0x00007f3acc5d5000)
libtinyxml2.so => /home/hacow/OGLTEST/libtinyxml2.so (0x00007f3acc3bd000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f3acc0ae000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f3acbda9000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f3acbb93000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f3acb7f0000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f3acb5ec000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f3acb3e4000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3acd2fa000)
libnvidia-tls.so.349.16 => /usr/lib/libnvidia-tls.so.349.16 (0x00007f3acb1e1000)
libnvidia-glcore.so.349.16 => /usr/lib/libnvidia-glcore.so.349.16 (0x00007f3ac8472000)
libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f3ac8130000)
libXext.so.6 => /usr/lib/libXext.so.6 (0x00007f3ac7f1e000)
libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f3ac7cfc000)
libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f3ac7af8000)
libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f3ac78f2000)
答案 0 :(得分:3)
使用cmake .
构建使用与该目录中最新版本相同的CMAKE_BUILD_TYPE
。使用cmake -DCMAKE_BUILD_TYPE=Release .
构建发布模式。如果先构建调试然后使用您描述的过程发布,那么它只会重建您的调试二进制文件。