是否可以创建一个静态链接库,在归档文件中包含git子模块?

vybvopom  于 2023-09-29  发布在  Git
关注(0)|答案(1)|浏览(106)

我目前正在用C开发一个图形库,它使用SDL 2沿着他们的库生态系统的其他部分,并希望将整个库打包到一个.a文件中,以便在有人在本地编译后使用。我已经将SDL 2和其他库(SDL-TTF、SDL-Mixer和SDL-Image)包含在一个名为external的文件夹中,它们作为git子模块添加到项目中。然后将它们作为子目录添加到各自的CMakeLists.txt文件中(如下所示),并将它们链接到根目录的CMakeLists.txt文件中,然后静态构建库。然而,静态库不包括SDL依赖项,只包括我自己的代码,所以我想问是否可以将所有SDL依赖项和我的代码捆绑到一个带有cmake的.a文件中。
CMakeLists.txt,位于子模块文件夹“external”中

set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )

# SDL
# https://github.com/libsdl-org/SDL
set( SDL_LIBC ON CACHE BOOL "" FORCE )
set( SDL_TEST OFF CACHE BOOL "" FORCE )
add_subdirectory(SDL)
target_link_libraries( Pixelinator SDL2::SDL2main SDL2::SDL2-static )

# SDL_image
# https://github.com/libsdl-org/SDL_image
set( SDL2IMAGE_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2IMAGE_INSTALL OFF CACHE BOOL "" FORCE )
set( SDL2IMAGE_VENDORED ON CACHE BOOL "" FORCE )
set( SDL2IMAGE_DEPS_SHARED OFF CACHE BOOL "" FORCE )
add_subdirectory( sdl-image )
target_link_libraries( Pixelinator SDL2_image::SDL2_image-static)

# SDL_ttf
# https://github.com/libsdl-org/SDL_ttf
# Make sure you run ./download.sh in the directory.
set( SDL2TTF_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2TTF_INSTALL OFF CACHE BOOL "" FORCE )
set( SDL2TTF_VENDORED ON CACHE BOOL "" FORCE )
set( SDL2TTF_HARFBUZZ ON CACHE BOOL "" FORCE )
add_subdirectory( sdl-ttf )
target_link_libraries( Pixelinator SDL2_ttf::SDL2_ttf-static )

# SDL_mixer
# https://github.com/libsdl-org/SDL_mixer
# Make sure you run ./download.sh in the directory.
set( SDL2MIXER_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_INSTALL OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_VENDORED ON CACHE BOOL "" FORCE )
set( SDL2MIXER_DEPS_SHARED OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_FLAC OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_MOD OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_MP3 OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_MIDI OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_OPUS OFF CACHE BOOL "" FORCE )
add_subdirectory( sdl-mixer )
target_link_libraries( Pixelinator SDL2_mixer::SDL2_mixer-static )

用于构建静态库的项目根目录中的CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
cmake_policy(SET CMP0075 NEW)
cmake_policy(SET CMP0091 NEW)
project(Pixelinator VERSION 1.0 DESCRIPTION "A 2D Engine using SDL and C")

set( CMAKE_CXX_STANDARD 11 CACHE STRING "" FORCE )
set( CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "" FORCE )

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

if(CMAKE_BUILD_TYPE EQUAL Release)
    message("Building Release...")
endif()

set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-Ofast")

add_library(Pixelinator STATIC
        ../../Pixelinator/src/engine/audio/sound.c
        ../../Pixelinator/src/engine/config/config.c
        ../../Pixelinator/src/engine/core/core.c
        ../../Pixelinator/src/engine/graphics/bitmap/bitmap.c
        ../../Pixelinator/src/engine/graphics/bitmap/pixel_threading.c
        ../../Pixelinator/src/engine/graphics/sprite/sprite.c
        ../../Pixelinator/src/engine/graphics/draw/shapes.c
        ../../Pixelinator/src/engine/input/input.c
        ../../Pixelinator/src/engine/io/io.c
        ../../Pixelinator/src/engine/render/render.c
        ../../Pixelinator/src/engine/render/render_init.c
        ../../Pixelinator/src/engine/render/render_util.c
        ../../Pixelinator/src/engine/font_cache/SDL_FontCache.c
        ../../Pixelinator/src/engine/text/text.c
        ../../Pixelinator/src/engine/global.c
        ../../Pixelinator/src/engine/time.c

        # SDL (Not sure if needed)
        ../external/SDL
        ../external/sdl-image
        ../external/sdl-mixer
        ../external/sdl-ttf
)

set(APP_EXE PixelinatorDemo)

if( ${CMAKE_SYSTEM_NAME} MATCHES "Android" )
    add_library( ${APP_EXE} SHARED )
else()
    add_executable(${APP_EXE} main.c)
    set_target_properties( ${APP_EXE} PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME} )
endif()

find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
    target_compile_options(${APP_EXE} PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
    target_link_libraries(${APP_EXE} "${CMAKE_THREAD_LIBS_INIT}")
endif()

add_subdirectory("external")
add_subdirectory(glad/)
include_directories("include")
#target_link_libraries(Pixelinator SDL2::SDL2main SDL2::SDL2-static SDL2_image::SDL2_image-static SDL2_ttf::SDL2_ttf-static SDL2_mixer::SDL2_mixer-static)

# Library is generated inside of the cmake-build-debug or release folder!
target_link_libraries(${APP_EXE} glad Pixelinator)

任何人可以提供的任何帮助将不胜感激!
编辑:修正了一些拼写错误。

ia2d9nvy

ia2d9nvy1#

例如,可以创建一个静态链接库,在归档文件中包含git子模块吗?不能将一个静态库静态链接到另一个。
也许它会更简单,只是添加外部源到您的库源?

execute_process(
    COMMAND bash "-c" "ls external/SDL_image/src/*.c;'" 
    OUTPUT_VARIABLE SDL_SOURCES
)

在那之后

add_library(Pixelinator STATIC
            # your sources:
            ....
            # external sources:
            ${SDL_SOURCES}
)

相关问题