如何链接到定义sdl_image.h函数的代码?

时间:2019-06-05 17:35:00

标签: c++ cmake linker sdl

我正在尝试向我的openGL项目添加声音和音乐。我正在使用Cmake。我收到未定义的参考错误,并尝试使用已经读过的许多方法和建议进行了几天的尝试,但显然我感到头疼。

我已经安装了我认为需要的库,已经配置并进行安装,尝试,查找软件包,添加模块代码以帮助实现该目的。我在某种程度上知道并非所有这些方法都可能对我有用。或者,也许我需要在我的项目文件夹和系统中安装这些库。好的,对此不太清楚。我正在使用带代码块的Ubuntu Linux编辑我的脚本,但没有进行编译。

我的cmakelists.text

cmake_minimum_required (VERSION 3.0)


project (maficengine LANGUAGES C CXX ASM)



set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/external/")




find_package(OpenGL REQUIRED)
#FIND_PACKAGE(SDL2_MIXER)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)

include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
#target_link_libraries(maficengine ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})

find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)

set(SDL2_INCLUDE_DIR /usr/include/SDL2)
set(SDL2_LIBRARY /usr/lib/x86_64-linux-gnu/libSDL2.so)
#file(GLOB_RECURSE SOURCE_FILES/usr/lib/x86_64-linux-gnu
#   ${CMAKE_SOURCE_DIR}/src/*.c
#   ${CMAKE_SOURCE_DIR}/src/*.cpp)

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL REQUIRED sdl)

set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)

if(SDL_FOUND)
  message(STATUS "SDL FOUND")
  elseif(!SDL_FOUND)
     message(STATUS "SDL not FOUND")
endif()


find_library(SDL_MIXER_LIBRARY
  NAMES SDL2_mixer
  HINTS
    ENV SDLMIXERDIR
    ENV SDLDIR
  PATH_SUFFIXES lib
)

if( CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR )
    message( FATAL_ERROR "Please select another Build Directory ! (and give it a clever name, like bin_Visual2012_64bits/)" )
endif()
if( CMAKE_SOURCE_DIR MATCHES " " )
    message( "Your Source Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()
if( CMAKE_BINARY_DIR MATCHES " " )
    message( "Your Build Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()

file(GLOB SOURCES "external/myCustomHeaders/*.cpp")






add_library(loaders SHARED ${SOURCES}
${SDL2_INCLUDE_DIR}
external/SDL2_image-2.0.4/SDL_image.h
external/myCustomHeaders/include/loadTexture.h
external/myCustomHeaders/loadTexture.cpp
/usr/include/SDL2/SDL.h
/usr/include/SDL/SDL_image.h
external/SDL2_mixer-2.0.4/SDL_mixer.h
)
include_directories( external/myCustomHeaders/include )
include_directories( external/SDL2_mixer-2.0.4/acinclude )
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}external/SDL2_mixer-2.0.4 )
include_directories(/usr/include/SDL2)
include_directories(/usr/include/SDL)
include_directories(${SDL2_INCLUDE_DIRS})
include(FindPackageHandleStandardArgs)

find_path(
  SDL_MIXER_INCLUDE_DIR
  PATHS
  /usr/include/SDL
  /usr/include/SDL2
  /usr/include
  /usr/local/include
  /sw/include
  /opt/local/include
  ${SDL_MIXER_ROOT_DIR}/include
  DOC "The directory where SDL_mixer.h resides")


link_directories(external/myCustomHeaders/include)
link_directories(external/myCustomHeaders)
link_directories(external/SDL2_mixer-2.0.4)
link_directories(/usr/include/SDL2)
link_directories(/usr/include/SDL)
# Compile external dependencies
add_subdirectory (external)


# On Visual 2005 and above, this module can set the debug working directory
cmake_policy(SET CMP0026 OLD)
cmake_policy(SET CMP0079  NEW)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-fe2273")
include(CreateLaunchers)
include(MSVCMultipleProcessCompile) # /MP

if(INCLUDE_DISTRIB)
    add_subdirectory(distrib)
endif(INCLUDE_DISTRIB)



include_directories(
    external/AntTweakBar-1.16/include/
    external/glfw-3.1.2/include/
    external/glm-0.9.7.1/
    external/glew-1.13.0/include/
    external/assimp-3.0.1270/include/
    external/bullet-2.81-rev2613/src/
    external/myCustomHeaders/include
    common/
)

set(ALL_LIBS
    ${OPENGL_LIBRARY}
    glfw
    GLEW_1130
    loaders

)

add_definitions(
    -DTW_STATIC
    -DTW_NO_LIB_PRAGMA
    -DTW_NO_DIRECT3D
    -DGLEW_STATIC
    -D_CRT_SECURE_NO_WARNINGS
)

# Tutorial 17
add_executable(mageengine
${SDL2_LIBRARY}
    Mafic/mafic.cpp
    common/shader.cpp
    common/shader.hpp
    common/controls.cpp
    common/controls.hpp
    common/texture.cpp
    common/texture.hpp
    common/objloader.cpp
    common/objloader.hpp
    common/vboindexer.cpp
    common/vboindexer.hpp
    common/quaternion_utils.cpp
    common/quaternion_utils.hpp
    Mafic/StandardShading.vertexshader
    Mafic/StandardShading.fragmentshader
)


set_target_properties(loaders
   PROPERTIES LINKER_LANGUAGE CXX)

#target_sources(loaders
# PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/myCustomHeaders/loadTexture.h
# )
target_link_libraries(mageengine
    ${ALL_LIBS}
    ANTTWEAKBAR_116_OGLCORE_GLFW
    ${SDL2_LIBRARIES}

   loaders
)
#target_link_libraries(${maficengine } SDL2::Main SDL2::Image)

# Xcode and Visual working directories
set_target_properties(mageengine PROPERTIES XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")
create_target_launcher(mageengine WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")

SOURCE_GROUP(common REGULAR_EXPRESSION ".*/common/.*" )
SOURCE_GROUP(shaders REGULAR_EXPRESSION ".*/.*shader$" )


if (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )

add_custom_command(
   TARGET mageengine POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/mageengine${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/"
)
  #target_link_libraries(mageengine LINK_PUBLIC ${mylibrary}
  # )

elseif (${CMAKE_GENERATOR} MATCHES "Xcode" )

endif (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )

这是我正在使用的模块代码,我认为它可能被cmake使用


# Locate SDL_image library
#
# This module defines:
#
# ::
#
#   SDL2_IMAGE_LIBRARIES, the name of the library to link against
#   SDL2_IMAGE_INCLUDE_DIRS, where to find the headers
#   SDL2_IMAGE_FOUND, if false, do not try to link against
#   SDL2_IMAGE_VERSION_STRING - human-readable string containing the version of SDL_image
#
#
#
# For backward compatibility the following variables are also set:
#
# ::
#
#   SDLIMAGE_LIBRARY (same value as SDL2_IMAGE_LIBRARIES)
#   SDLIMAGE_INCLUDE_DIR (same value as SDL2_IMAGE_INCLUDE_DIRS)
#   SDLIMAGE_FOUND (same value as SDL2_IMAGE_FOUND)
#
#
#
# $SDLDIR is an environment variable that would correspond to the
# ./configure --prefix=$SDLDIR used in building SDL.
#
# Created by Eric Wing.  This was influenced by the FindSDL.cmake
# module, but with modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).

#=============================================================================
# Copyright 2005-2009 Kitware, Inc.
# Copyright 2012 Benjamin Eikel
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)

find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h
        HINTS
        ENV SDL2IMAGEDIR
        ENV SDL2DIR
        PATH_SUFFIXES SDL2
        # path suffixes to search inside ENV{SDLDIR}
        include/SDL2 include
        PATHS ${SDL2_IMAGE_PATH}
        )

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(VC_LIB_PATH_SUFFIX lib/x64)
else()
    set(VC_LIB_PATH_SUFFIX lib/x86)
endif()

find_library(SDL2_IMAGE_LIBRARY
        NAMES SDL2_image
        HINTS
        ENV SDL2IMAGEDIR
        ENV SDL2DIR
        PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
        PATHS ${SDL2_IMAGE_PATH}
        )

if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h")
    file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
    file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
    file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
    string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL2_IMAGE_VERSION_MAJOR_LINE}")
    string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL2_IMAGE_VERSION_MINOR_LINE}")
    string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL2_IMAGE_VERSION_PATCH_LINE}")
    set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH})
    unset(SDL2_IMAGE_VERSION_MAJOR_LINE)
    unset(SDL2_IMAGE_VERSION_MINOR_LINE)
    unset(SDL2_IMAGE_VERSION_PATCH_LINE)
    unset(SDL2_IMAGE_VERSION_MAJOR)
    unset(SDL2_IMAGE_VERSION_MINOR)
    unset(SDL2_IMAGE_VERSION_PATCH)
endif()

set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY})
set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image
        REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS
        VERSION_VAR SDL2_IMAGE_VERSION_STRING)

# for backward compatibility
set(SDLIMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
set(SDLIMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
set(SDLIMAGE_FOUND ${SDL2_IMAGE_FOUND})

mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)

我的Cmake错误输出,我是cmake btw的初学者,所以请大笑。

CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o: In function `load_image(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
mafic.cpp:(.text+0x2c): undefined reference to `IMG_Load'
mafic.cpp:(.text+0x43): undefined reference to `SDL_DisplayFormat'
CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o: In function `init()':
mafic.cpp:(.text+0x133): undefined reference to `SDL_SetVideoMode'
mafic.cpp:(.text+0x152): undefined reference to `TTF_Init'
mafic.cpp:(.text+0x17c): undefined reference to `Mix_OpenAudio'
mafic.cpp:(.text+0x19e): undefined reference to `SDL_WM_SetCaption'
CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o: In function `load_files()':
mafic.cpp:(.text+0x21c): undefined reference to `TTF_OpenFont'
mafic.cpp:(.text+0x25b): undefined reference to `Mix_LoadMUS'
mafic.cpp:(.text+0x298): undefined reference to `Mix_LoadWAV_RW'
mafic.cpp:(.text+0x2bf): undefined reference to `Mix_LoadWAV_RW'
mafic.cpp:(.text+0x2e6): undefined reference to `Mix_LoadWAV_RW'
mafic.cpp:(.text+0x30d): undefined reference to `Mix_LoadWAV_RW'
CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o: In function `clean_up()':
mafic.cpp:(.text+0x3ba): undefined reference to `Mix_FreeChunk'
mafic.cpp:(.text+0x3c9): undefined reference to `Mix_FreeChunk'
mafic.cpp:(.text+0x3d8): undefined reference to `Mix_FreeChunk'
mafic.cpp:(.text+0x3e7): undefined reference to `Mix_FreeChunk'
mafic.cpp:(.text+0x3f6): undefined reference to `Mix_FreeMusic'
mafic.cpp:(.text+0x405): undefined reference to `TTF_CloseFont'
mafic.cpp:(.text+0x40a): undefined reference to `Mix_CloseAudio'
mafic.cpp:(.text+0x40f): undefined reference to `TTF_Quit'
collect2: error: ld returned 1 exit status
CMakeFiles/mageengine.dir/build.make:197: recipe for target 'mageengine' failed
make[2]: *** [mageengine] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/mageengine.dir/all' failed
make[1]: *** [CMakeFiles/mageengine.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:0)

好的,我似乎已经解决了一些问题。比我让自己和其他人相信的幅度更大。-可能是做错了路,但似乎很多人都没有按照我的方式去做。我只是通过执行以下target_link_libraries(loaders -lSDLmain)target_link_libraries(loaders -lSDL)target_link_libraries(loaders -lSDL2_ttf)target_link_libraries(loaders -lSDL2_mixer)并根据需要下载并安装了lib库,从而链接了我的cmake文件中的库。话虽这么说,我仍然有很多问题,但我认为这还差一步。