gpt4 book ai didi

c++ - 如何与SDL库链接?

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:55 24 4
gpt4 key购买 nike

在Linux/Mint下,我正在尝试编译这个源代码here .

我收到了很多“对 Xxx 的 undefined reference ”,因此在谷歌搜索一段时间后,以下是我安装的所有内容:

sudo apt-get install libboost-all-dev libsdl1.2-dev libsdl-image1.2-dev libsdl-net1.2-dev
sudo apt-get install libsdl-ttf2.0-dev libspeexdsp-dev libzzip-dev
sudo apt-get install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev
sudo apt-get install libsdl-mixer1.2-dev libsdl2-dev libsdl2-mixer-dev libsdl2-image-dev

在此之后,当我尝试启动 make 时,我仍然得到大量 undefined reference :

.....blabla..........
font.cpp:(.text+0x388): undefined reference to `TTF_RenderUNICODE_Blended'
font.cpp:(.text+0x3ec): undefined reference to `SDL_UpperBlit'
font.cpp:(.text+0x3f8): undefined reference to `SDL_FreeSurface'
font.cpp:(.text+0x42a): undefined reference to `TTF_RenderUNICODE_Blended'
font.cpp:(.text+0x488): undefined reference to `SDL_UpperBlit'
font.cpp:(.text+0x494): undefined reference to `SDL_FreeSurface'
font.o: In function `Font::getWidth(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)':
font.cpp:(.text+0x566): undefined reference to `TTF_SizeUNICODE'
font.o: In function `Font::getWidth(wchar_t)':
font.cpp:(.text+0x5cc): undefined reference to `TTF_GlyphMetrics'
font.o: In function `Font::getHeight(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)':
font.cpp:(.text+0x634): undefined reference to `TTF_SizeUNICODE'
font.o: In function `Font::getSize(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, int&, int&)':
font.cpp:(.text+0x691): undefined reference to `TTF_SizeUNICODE'
topscores.o: In function `ScoresWindow::ScoresWindow(int, int, TopScores*, int)':
topscores.cpp:(.text+0xc85): undefined reference to `SDL_SetClipRect'
topscores.cpp:(.text+0xce9): undefined reference to `SDL_SetClipRect'
sound.o: In function `Sound::Sound()':
sound.cpp:(.text+0x48): undefined reference to `Mix_OpenAudio'
sound.o: In function `Sound::~Sound()':
sound.cpp:(.text+0xd1): undefined reference to `Mix_CloseAudio'
sound.cpp:(.text+0x128): undefined reference to `Mix_FreeChunk'
sound.cpp:(.text+0x140): undefined reference to `Mix_CloseAudio'
sound.o: In function `Sound::play(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)':
sound.cpp:(.text+0x269): undefined reference to `SDL_RWFromMem'
sound.cpp:(.text+0x276): undefined reference to `Mix_LoadWAV_RW'
sound.cpp:(.text+0x2d5): undefined reference to `Mix_VolumeChunk'
sound.cpp:(.text+0x2f0): undefined reference to `Mix_PlayChannelTimed'
sound.cpp:(.text+0x2f5): undefined reference to `SDL_PumpEvents'
collect2: error: ld returned 1 exit status
Makefile:52: recipe for target 'einstein' failed
make: *** [einstein] Error 1

查看 Makefile(这很容易理解),当我启动 sdl-config --libs 时,我得到 -L/usr/lib/x86_64-linux-gnu -lSDL

如果我这样做ls -alh/usr/lib/x86_64-linux-gnu我会得到大量文件(即该目录存在)

我错过了什么?

最佳答案

这似乎是一个 SDL 1.2 项目。不需要所有这些 SDL2 软件包。

该项目存在多个问题:

  • Makefile应该使用 pkg-config 在 Linux 上(或者由 CMake 之类的元数据生成),而不是 sdl-config 的混搭和原料-l旗帜。已修复:

    CXXFLAGS=-pipe -Wall $(OPTIMIZE) $(DEBUG) `pkg-config --cflags sdl SDL_mixer SDL_ttf zlib` -DPREFIX=L\"$(PREFIX)\" $(PROFILER)
    LNFLAGS=`pkg-config --libs sdl SDL_mixer SDL_ttf zlib` $(PROFILER)
  • $(TARGET)链接器选项位于错误的位置(在 $(OBJECTS) 之前)。已修复:

    $(TARGET): $(OBJECTS)
    $(CXX) $(OBJECTS) $(LNFLAGS) -o $(TARGET)
  • 所有#include <SDL/*>行应该有前导 SDL/剥离; pkg-config --cflags生成-I/usr/include/SDL ,不是-I/usr/include .

应用所有这些修复后,它将构建在此 Ubuntu 16.04 安装上。

完整补丁:

diff --git a/Makefile b/Makefile
index e682bb1..08f5463 100644
--- a/Makefile
+++ b/Makefile
@@ -16,8 +16,8 @@ PREFIX=/usr/local
OPTIMIZE=#-O6 -march=pentium4 -mfpmath=sse -fomit-frame-pointer -funroll-loops
PROFILER=#-pg
DEBUG=#-ggdb
-CXXFLAGS=-pipe -Wall $(OPTIMIZE) $(DEBUG) `sdl-config --cflags` -DPREFIX=L\"$(PREFIX)\" $(PROFILER)
-LNFLAGS=-pipe -lSDL_ttf -lfreetype `sdl-config --libs` -lz -lSDL_mixer $(PROFILER)
+CXXFLAGS=-pipe -Wall $(OPTIMIZE) $(DEBUG) `pkg-config --cflags sdl SDL_mixer SDL_ttf zlib` -DPREFIX=L\"$(PREFIX)\" $(PROFILER)
+LNFLAGS=`pkg-config --libs sdl SDL_mixer SDL_ttf zlib` $(PROFILER)
INSTALL=install

TARGET=einstein
@@ -49,7 +49,7 @@ all: $(TARGET)


$(TARGET): $(OBJECTS)
- $(CXX) $(LNFLAGS) $(OBJECTS) -o $(TARGET)
+ $(CXX) $(OBJECTS) $(LNFLAGS) -o $(TARGET)

clean:
rm -f $(OBJECTS) core* *core $(TARGET) *~
diff --git a/font.h b/font.h
index 40d617b..2e8e44d 100644
--- a/font.h
+++ b/font.h
@@ -3,7 +3,7 @@


#include <string>
-#include <SDL/SDL_ttf.h>
+#include <SDL_ttf.h>


class Font
diff --git a/iconset.h b/iconset.h
index 4faf055..0f971ac 100644
--- a/iconset.h
+++ b/iconset.h
@@ -2,7 +2,7 @@
#define __ICONSET_H__


-#include <SDL/SDL.h>
+#include <SDL.h>


class IconSet
diff --git a/main.cpp b/main.cpp
index d103861..2d4c33f 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,8 +1,8 @@
#include <stdlib.h>
#include <iostream>
-#include <SDL/SDL.h>
-#include <SDL/SDL_main.h>
-#include <SDL/SDL_ttf.h>
+#include <SDL.h>
+#include <SDL_main.h>
+#include <SDL_ttf.h>
#include "main.h"
#include "utils.h"
#include "storage.h"
diff --git a/screen.cpp b/screen.cpp
index a64dd31..7e48934 100644
--- a/screen.cpp
+++ b/screen.cpp
@@ -1,4 +1,4 @@
-#include <SDL/SDL.h>
+#include <SDL.h>
#include "screen.h"
#include "exceptions.h"
#include "unicode.h"
diff --git a/screen.h b/screen.h
index 12e99ab..2b5253d 100644
--- a/screen.h
+++ b/screen.h
@@ -2,7 +2,7 @@
#define __SCREEN_H__


-#include "SDL/SDL.h"
+#include "SDL.h"
#include <vector>
#include <list>

diff --git a/sound.cpp b/sound.cpp
index 3725245..998245f 100644
--- a/sound.cpp
+++ b/sound.cpp
@@ -1,7 +1,7 @@
#include "sound.h"

#include <iostream>
-#include <SDL/SDL_events.h>
+#include <SDL_events.h>
#include "resources.h"


diff --git a/sound.h b/sound.h
index 44e587e..dc2a449 100644
--- a/sound.h
+++ b/sound.h
@@ -4,7 +4,7 @@

#include <string>
#include <map>
-#include <SDL/SDL_mixer.h>
+#include <SDL_mixer.h>


class Sound
diff --git a/utils.h b/utils.h
index f4188cb..9ce7cb1 100644
--- a/utils.h
+++ b/utils.h
@@ -1,7 +1,7 @@
#ifndef __UTILS_H__
#define __UTILS_H__

-#include <SDL/SDL.h>
+#include <SDL.h>
#include <string>
#ifdef WIN32
#include <sys/time.h>
diff --git a/widgets.h b/widgets.h
index ce417ba..0bd7753 100644
--- a/widgets.h
+++ b/widgets.h
@@ -4,7 +4,7 @@
#include <string>
#include <list>
#include <set>
-#include <SDL/SDL.h>
+#include <SDL.h>
#include "font.h"

关于c++ - 如何与SDL库链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51971681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com