我对如何静态包含SDL2的源代码感到困惑。我正在尝试这样做,以使我正在开发的库更加可移植。
当我将其设置为可执行文件时,可以很好地编译该库,但是当我将其更改为库时,它将不包含该库。
当前,当我尝试将库包含在另一个项目中时,它会显示“无法打开包含文件:'SDL2 / SDL.h':没有此类文件或目录” 。因此,我认为错误的原因是include目录没有随静态库一起导出。
我的文件系统:
include
--Header Files
src
--Source Files
extern
--SDL2
build
以下是导致错误的文件示例:
#include <iostream>
#include <SDL.h> //Error
using namespace std;
/* The code */
这是我的主要CMakeLists.txt的示例:
cmake_minimum_required(VERSION 3.7)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
project(MyProject VERSION 1.0.0)
set(SDL2_SOURCE_DIR “${CMAKE_SOURCE_DIR}/extern/SDL2”)
add_subdirectory(extern/SDL2)
add_subdirectory(src)
这是我的src CMakeLists.txt的示例:
set(PROJECT_NAME MyProject)
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
add_library(${PROJECT_NAME} ${SOURCES} ${HEADER_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include")
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
答案 0 :(得分:1)
从sdl2 CMakeLists.txt开始尝试:
set(SDL_STATIC 1)
add_subdirectory(extern/SDL2) # And I recommend EXCLUDE_FROM_ALL
还请确保添加支票:
foreach(i IN ITEMS SDL2main SDL2-static)
if(NOT TARGET ${i})
message(FATAL_ERROR "${i} is not a target")
endif()
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)