繁体   English   中英

使用CMAKE“未定义对FT_Load_Glyph的引用”和其他SDL2_ttf函数

[英]“undefined reference to `FT_Load_Glyph'” and other SDL2_ttf functions using CMAKE

在Windows 10上的Clion中使用此CMAKE文件(使用MinGW 5.0):

cmake_minimum_required(VERSION 3.3)
project(ClionProjects)

# configure the SDL (cf. "SDL2-2.0.3\i686-w64-mingw32\lib\pkgconfig\sdl2.pc")
# C++ flags
set(SDL2_Flags "-mwindows -Wl,--no-undefined -static-libgcc")
# library paths
set(SDL2_ROOT     "C:/SDL2/i686-w64-mingw32")
set(SDL2_Includes "${SDL2_ROOT}/include")
set(SDL2_LibDir   "${SDL2_ROOT}/lib")
# imported targets for CMake (cf. https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets)
add_library(SDL2     STATIC IMPORTED)
add_library(SDL2main STATIC IMPORTED)
add_library(SDL2_image STATIC IMPORTED)
add_library(SDL2_ttf STATIC IMPORTED)
set_property(TARGET SDL2     PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2.a")
set_property(TARGET SDL2main PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2main.a")
set_property(TARGET SDL2_image PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2_image.a")
set_property(TARGET SDL2_ttf PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2_ttf.a")
# the libs to link against
# note: as always with gcc, the order is important...
set(SDL2_Libs mingw32 SDL2 SDL2main m SDL2_image SDL2_ttf dinput8 dxguid dxerr8 user32 gdi32 winmm imm32 ole32 oleaut32 shell32 version uuid)

# configure the project
# include the SDL flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${SDL2_Flags}")
# collect the sources
set(SOURCE_FILES
        Kod/Graphics/Graphics.cc
        Kod/Graphics/Graphics.h
        Kod/Game/Game.cc
        Kod/Game/Game.h
        Kod/Gameboard/Gameboard.cc
        Kod/Gameboard/Gameboard.h
        Kod/Meeple/Meeple.cc
        Kod/Meeple/Meeple.h
        Kod/Player/Player.cc
        Kod/Player/Player.h
        Kod/Resource/Resource.cc
        Kod/Resource/Resource.h
        Kod/Tile/Tile.cc
        Kod/Tile/Tile.h
        Kod/Carcassonne.cc)
# define the target
add_executable(ClionProjects ${SOURCE_FILES} Kod/Carcassonne.cc)
# include the SDL headers
target_include_directories(ClionProjects SYSTEM PRIVATE ${SDL2_Includes})
# link against the SDL (and its dependencies)
target_link_libraries(ClionProjects ${SDL2_Libs})


#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lSDL2 -lSDL2-image")


#add_executable(ClionProjects ${SOURCE_FILES} Kod/Carcassonne.cc)
#target_link_libraries(ClionProjects SDL2main SDL2 SDL2_image)

我收到此错误:

C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `Load_Glyph':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:609: undefined reference to `FT_Load_Glyph'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:671: undefined reference to `FT_Outline_Transform'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:677: undefined reference to `FT_Get_Glyph'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:678: undefined reference to `FT_Stroker_New'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:694: undefined reference to `FT_Render_Glyph'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:895: undefined reference to `FT_Done_Glyph'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:607: undefined reference to `FT_Get_Char_Index'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:682: undefined reference to `FT_Stroker_Set'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:683: undefined reference to `FT_Glyph_Stroke'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:684: undefined reference to `FT_Stroker_Done'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:686: undefined reference to `FT_Glyph_To_Bitmap'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:688: undefined reference to `FT_Done_Glyph'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_Init':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:340: undefined reference to `FT_Init_FreeType'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_CloseFont':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:927: undefined reference to `FT_Done_Face'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_OpenFontIndexRW':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:432: undefined reference to `FT_Open_Face'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:460: undefined reference to `FT_Set_Char_Size'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:454: undefined reference to `FT_Set_Charmap'
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:484: undefined reference to `FT_Set_Pixel_Sizes'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_SizeUTF8':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:1241: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_RenderUTF8_Solid':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:1429: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_RenderUTF8_Shaded':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:1610: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_RenderUTF8_Blended':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:1780: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_RenderUTF8_Blended_Wrapped':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:2048: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_Quit':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:2196: undefined reference to `FT_Done_FreeType'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_GetFontKerningSize':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:2209: undefined reference to `FT_Get_Kerning'
C:/SDL2/i686-w64-mingw32/lib/libSDL2_ttf.a(SDL_ttf.o): In function `TTF_GlyphIsProvided':
/Users/slouken/release/SDL_ttf/SDL2_ttf-2.0.12-source/foo-x86/../SDL_ttf.c:1138: undefined reference to `FT_Get_Char_Index'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\ClionProjects.dir\build.make:289: recipe for target 'ClionProjects.exe' failed
mingw32-make.exe[3]: *** [ClionProjects.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/ClionProjects.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/ClionProjects.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/ClionProjects.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/ClionProjects.dir/rule] Error 2
mingw32-make.exe: *** [ClionProjects] Error 2
makefile:117: recipe for target 'ClionProjects' failed

我已将SDL2_ttf.dll添加到system32,并将可执行文件放置在该文件夹中,将SDL2_ttf.h添加到/ include,其中包含SDL2的所有其他.h文件,还将libSDL2_ttf.a添加到/ lib。 我不明白为什么会收到这些错误...我的标志顺序有问题吗?

以下是我与CLion一起使用SDL2,SDL2_ttf,SDL2_mixer等的方式。 这与您到目前为止所做的完全不同,因此请记住这一点。 我将在类似POSIX的环境中使用MSYS2(而不是您正在使用的MINGW / MSys)。

首先,我将从安装MSYS2开始。 http://msys2.github.io/获取 然后,根据您安装的版本,您将在开始菜单中获得2或3个快捷方式。

从现在开始,我将坚持使用MINGW-W64-x86_64版本的工具链。 如果您使用的是i686mingw64 x86_64替换为i686 (将mingw64mingw32 )。

从开始菜单中打开MinGW-w64 Win64 shell快捷方式。 bash shell将欢迎您。 在这一点上,我将从首先安装工具链开始。 在提示符下,输入以下命令

pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-cmake mingw-w64-x86_64-pkg-config mingw-w64-x86_64-make mingw-w64-x86_64-gdb

软件包管理器将为您安装gcc,cmake,pkg-config,make和gdb。 这包括这些工具的所有依赖关系,因此将花费很多时间。

然后通过在提示符处调用以下命令来安装库:

pacman -S mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_ttf mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_image

pacman(程序包管理器)将下载并安装这3个库及其依赖项。

上面的步骤很重要。 我尚未对其进行重新测试(因为我已经具有工作环境)。 如果您遇到问题,请告诉我。 我可能稍后再测试。 同样,上面的步骤也适用于Arch Linux用户,只需替换软件包名称,您就可以了。

接下来,打开CLion。 转到“ 文件”->“设置” Build,Execution和Deployment下选择工具链 然后在您的MSYS2安装路径下将MinGW主页更改为mingw64。

CLion设置

然后一切准备就绪。

我将使用FindPkgConfig模块获取链接器标志,并包含我使用的每个库的标志。 该模块回复我们之前安装的pkg-config命令。 CMakeList.txt ,添加以下行:

INCLUDE(FindPkgConfig)

pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMG REQUIRED SDL2_image)
pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_MIX REQUIRED SDL2_mixer)

include_directories(${SDL2_INCLUDE_DIRS}
    ${SDL2_IMG_INCLUDE_DIRS}
    ${SDL2_TTF_INCLUDE_DIRS}
    ${SDL2_MIX_INCLUDE_DIRS})

link_directories (${SDL2_LIBRARY_DIRS}
    ${SDL2_IMG_LIBRARY_DIRS}
    ${SDL2_TTF_LIBRARY_DIRS}
    ${SDL2_MIX_LIBRARY_DIRS})

target_link_libraries (TestSDL2
    ${SDL2_LIBRARIES}
    ${SDL2_IMG_LIBRARIES}
    ${SDL2_TTF_LIBRARIES}
    ${SDL2_MIX_LIBRARIES})

将TestSDL2更改为您的可执行文件。 您可能必须删除现有的FindSDL或寻找SDL库的任何命令。

这种方法比自定义模块干净得多。 缺点是它几乎与系统的库文件相关联(尤其是在使用Linux的情况下)。 另外,如果必须打包exe文件,则必须手动在MSYS2 / mingw64 / bin中查找dll文件(顺便说一句,这并不是很困难)。

另一种可能的方法是将SDL作为子项目。 我之前没有做过,所以我无法解释。

我想在您熟悉此工作原理之后,您也可以将相同的方法应用于其他库(例如,box2d)。 您可以在https://github.com/Alexpux/MINGW-packages上查看可用软件包的列表,必须通过在库名称之前插入Arch名称来对软件包名称进行一些修改(例如mingw-w64-x86_64-box2d用于mingw-w64-box2d软件包)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM