-1

Under Linux/Mint, I'm trying to compile this source code here.

I got a lot of "undefined reference to Xxx" so after googling for a while, here are all the things I've installed:

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

After this, when I try to launch make I still get tons of 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

Watching the Makefile (which is quite easy to understand), when I launch sdl-config --libs I get -L/usr/lib/x86_64-linux-gnu -lSDL

If I do ls -alh /usr/lib/x86_64-linux-gnu I get tons of file (i.e. this directory exists)

What am I missing?

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • 2
    Duplicate? Seriously? I thought Stackoverflow was here to help. If I have to check **each 12 suggestions** on the answer you redirect, I could do this on my own. Right now this is of 0/20 use. **0/20**. Even though it's very clean & precise, this answer here https://stackoverflow.com/a/12573818/106140 is of no use to me. Like someone said "**And how to define where is my case?!**". – Olivier Pons Aug 22 '18 at 17:01
  • 6
    You are using symbols not defined by whatever you link with. That is your problem. The linked dupe explains that. Link with whatever implements what you use. And (*IMHO*) reading 20 answers and doing a little thinking yourself is pretty much equal to a solution. But *fine*, I'll retract it. – Jesper Juhl Aug 22 '18 at 17:02
  • @OlivierPons - try this: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/linux/index.php (it's a pretty popular SDL tutorial, this exact link is to the setup part for Linux) – hauron Aug 22 '18 at 17:03
  • I tried to move the link information at the end of the command: I changed `$(CXX) $(LNFLAGS) $(OBJECTS) -o $(TARGET)` to `$(CXX) $(OBJECTS) -o $(TARGET) $(LNFLAGS)` and it worked. I did like all my students do: https://en.wikipedia.org/wiki/Infinite_monkey_theorem Trying random stuff... – Olivier Pons Aug 22 '18 at 17:10
  • @Olivier Pons Link order matters. You should always mention whatever *needs* a symbol before what *defines* the symbol. Otherwise the linker will just throw away the symbol since it is (to its mind) unused at that point. That's pretty basic and something that should be taught in an *introduction* C++ class (IMHO). – Jesper Juhl Aug 22 '18 at 17:13
  • 3
    @Oliver - Based on your problem description the dup was a good choice. All you provided was a lot of *`undefined reference to Xxx`* and *`.....blabla.....`*. You should be able to get better help if you provide more details. You are lacking compile and link commands and relevant makefile recipes. So you should not be surprised the suggestions were just as bad. If you just want to discuss things then maybe you should use [chat.stackoverflow.com](https://chat.stackoverflow.com/) until you are closer to your problem. – jww Aug 22 '18 at 17:24
  • @JesperJuhl Thank you very much for your answer. I'm new to CPP world and I really dont want to go further than "trying to compile, and understand what has been done to make the same in Python". I'm sorry if I sound aggressive. – Olivier Pons Aug 22 '18 at 17:33
  • Possible dupe (In *My* Opinion): https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix#12573818 – Jesper Juhl Aug 22 '18 at 17:50
  • @OlivierPons If it is of any help at all, when I set up SDL, none of the tutorials worked for me either. So I screwed them, sat down on my IDE and tried to figure it out without them. I found out that you don't have to compile any of the libraries. In Xcode at least, you just make sure that the required SDL frameworks are under "Linked Libraries and Frameworks" include the SDL header files and then you're done and ready to start coding. – Shades Aug 22 '18 at 17:55

1 Answers1

2

That appears to be a SDL 1.2 project. No need for all of those SDL2 packages.

Multiple problems with that project:

  • The Makefile should be using pkg-config on Linux (or generated by something more meta like CMake) instead of a mishmash of sdl-config and raw -l flags. Fixed:

    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)
    
  • The $(TARGET) linker options were in the wrong location (before $(OBJECTS)). Fixed:

    $(TARGET): $(OBJECTS)
        $(CXX) $(OBJECTS) $(LNFLAGS) -o $(TARGET)
    
  • All of the #include <SDL/*> lines should have the leading SDL/ stripped; pkg-config --cflags generates -I/usr/include/SDL, not -I/usr/include.

With all of those fixes applied it builds on this Ubuntu 16.04 install.

Full patch:

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"
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Thank you very much indeed! That's more than I could expect!! Did you try to share/apply this patch on the github repo? – Olivier Pons Aug 23 '18 at 19:42
  • @OlivierPons: Nope, didn't try to upstream it. Feel free! – genpfault Aug 23 '18 at 19:49
  • @OlivierPons: Looks like Debian already [has](https://sources.debian.org/patches/einstein/2.0.dfsg.2-9/01_sdl.diff/) some [patches](https://sources.debian.org/patches/einstein/2.0.dfsg.2-9/15_ld-as-needed.diff/) for the issues I identified. Looks like [ptitSeb's fork](https://github.com/ptitSeb/einstein-puzzle) already has a lot of them applied, as well as some SDL2 porting effort. – genpfault Aug 23 '18 at 19:54
  • Wow, didn't notice that, and the `make` did work right out of the box! Thank you very much for all the work here, that's much appreciated! – Olivier Pons Aug 23 '18 at 20:01