"Undefined symbols for architecture x86_64" In SDL 2 (macOS) - c++

Basically, I was trying to make my first SDL2 game in C but encountered the same error every time. Undefined symbols for architecture x86_64 Where i can't call simple functions like the SDL_CreateWindow from SDL. I thought something like this would happen because I have a very limited understanding of C and C++ as I only just learnt the language and concepts.
Originally, I downloaded the SDL 2 source code (SDL2.framework and put it into) the /Library/Frameworks file. Then I simply tried to include SDL2 by using the path #include </Library/Frameworks/SDL2.framework/Headers/SDL.h> to absolutely no avail.
I have tried looking up solutions but they all seem to be tutorials of EXACTLY what I have done or just answers for Windows users (I could rant about this).
I followed tutorials on the SDL website but I didn't understand them very well (I am new to shell and only 13) and most of the commands didn't work.
I tried troubleshooting the problem myself by looking inside of the SDL2 files and found these functions.
Please help, I have been searching for a while. Ideally I would just want a clear explanation (for newbies) on how to setup SDL2 so that I can compile my program with SDL2. This is what I tested:
#include <stdio.h>
#include "/Users/christianlincoln/Documents/programs/c/SDL2.framework/Versions/A/Headers/SDL.h"
int main(int argc, char * argv[]) {
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"Game", // window title
0, // initial x position
0, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
printf("Hello!\n");
return 0;
}
Sorry for wording my question wrong or being outright incompetent.

I think I figured it out, the problem was I also needed to compile the source libraries but I had no idea how to do this and how to ensure that gcc would link everything together.
All the other tutorials were just telling me to put the framework in /Library/Frameworks which was wrong.
Turns out I had to mess around with gcc for a while after compiling:
gcc game.c -I/usr/local/include/SDL2 -lSDL2
Although I am still unsure of how to do this with things like OpenGL.

Related

sdl, sdl2 error: SDL_window (among others) not declared

I have played around with C++ for a while and just recently started to get into SDL and SDL2.
I was able to get the dot demo program to work.
But other programs, such as Lazy Foo' Productions's copied and pasted don't seem to work.
I have both SDL and SDL2 installed (and uninstalled and reinstalled.) I am on Ubuntu 15.04 and I have the IDE CodeBlocks linked (-ISDL2)
The errors are SDL_Window - SDL_WINDOWPOS_UNDEFINED - SDL_WINDOW_SHOWN - SDL_CreateWindow - SDL_GetWindowSurface - SDL_UpdateWindowSurface and finally, SDL_DestroyWindow -- was not declared in this scope.
Also, I include:
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
I'm pretty sure that I don't need all of that location, but it didn't work without it either. One other note, when I type the #includes, CodeBlocks will suggest SDL2/SDL.h but not SDL/SDL.h.
What am I missing?
I don't think I can put Lazy Foo' code here - I didn't get permission...
The code you listed;
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
Why don't you change it to
#include <SDL2/SDL.h>
#include <stdio.h>
as the first header is where SDL_CreateWindow and other SDL2 functions are declared?
Also you don't need to include both SDL and SDL2 headers. Indeed that could very well be the source of your problem as you would only need to include the version you're using.
If you're following the tutorials from lazyfoo's site, you can check if the ones you're following are using SDL1.2 or SDL2 from their table of contents, as the site actually have the tutorials for both versions.
UPDATE:
I didn't notice that your platform is a Linux platform. Then it is so much easier to solve your problem. The demo that you followed previously was done using SDL-1.2, whereas the gcc error hinted that you're using SDL-2.0, hence SDL_CreateWindow and other undefined errors. You should install SDL-2.0 library and SDL-2.0 development files (which will provide you with the necessary SDL-2.0 headers). You may refer this to SDL-2.0 packages provided by your platform distribution.
As for the compilation, it'll be the same as the tutorial you've followed with a minor change, instead of gcc sdltest.o -lSDL -o sdltest, you'll issue gcc sdltest.o -lSDL2 -o sdltest to indicate that you're linking your code against SDL2 library.
EDIT
A simple SDL program to test your environment. You can use any of the simpler text editor such as nano or gedit or others to edit this, and run the compilation command above to test your setup.
The simplest way to do this is by copy the code, then from your terminal, issue cat > sdltest.cpp and paste the code, then hit [ENTER] and [CTRL-C] to end it. Then you can issue the compilation command as mentioned previously,g++ sdltest.cpp -lSDL2 -o sdltest.
Code;
#include <SDL2/SDL.h>
#include <stdio.h>
int main()
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
w = SDL_CreateRenderer(p, -1, 0);
SDL_RenderClear(w);
SDL_SetRenderDrawColor(w,255,0,0,255);
SDL_Rect Rect = {220,140,200,200};
SDL_RenderFillRect(w,&Rect);
SDL_RenderPresent(w);
SDL_Delay(3000);
SDL_DestroyRenderer(w);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
Hope that helps.

C++ strange "undefined reference"

I'm relatively new to c++, so bear with a little.
I have a class with the constructor:
Window(int width, int height, const std::string& title);
As defined in the header file.
I then have the code:
#include "window.h"
int main(int argc, char** argv) {
new Window(800, 600, "Elysian Engine");
}
in Main.
When building, I am getting the error "undefined reference to 'Window(int, int, std::string const&)'" Which I do not understand, as I thought I am correctly importing it and everything. I understand this to be a linking error, but I'm not sure why.
Thanks!
--- EDIT ---
The code for window.cpp:
#include "window.h"
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
#include <GL/glew.h>
Window::Window(int width, int height, const std::string& title) :
width(width),
height(height),
title(title),
isCloseRequested(false) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
}
}
Window::~Window() {
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
Because your code does not appear to be causing the issue, I will address the IDE.
When I first started using Code::Blocks, I ran into the "undefined reference" problem quite a bit. There are multiple ways that I solved this.
Exit Code::Blocks and re-open it. This solved my issues more times than I would care to count. It's similar to finally realizing that an executable is acting strangely because you needed to run make clean before compiling; sometimes, you are compiling an old version of code without realizing it.
Delete window.h and window.cpp from the project and re-add them. This is similar to the solution above. Although this has worked for me before, I couldn't figure out exactly why it worked.
Include the full path of window.h. Instead of #include "window.h", try #include "/path/goes/here/window.h". It is possible that your current location (as specified in your #include statement) is incorrect.
Check the compiler that is selected for the project. When I used Code::Blocks, I typically used gcc. However, there was a case where I was receiving all sorts of errors (upon attempting compilation) that I either did not understand or could not resolve, only to learn that I had accidentally selected some gcc variant from the compiler selection list that I did not even have installed.
Create a new project. Move your source files out of the project folder that is created by Code::Blocks, and then remove that project folder. Launch Code::Blocks, create a new project, and add your source files to the new project.
When all else fails and you absolutely must use Code::Blocks, reinstall the full package. I had to do this my first time using Code::Blocks (which was also my first time using an IDE). I realized that I had a bare-bones installation of the IDE, which was limiting what I could do (including compiling). Naturally, I cannot access the Code::Blocks website right now, otherwise I would offer a link here as well. In this case, I think it is safe to say that the full-featured stable version will be the largest (in MB) you can find on Code::Blocks' Downloads page. After you have reinstalled, create a new project and add your files back to it.
I eventually moved to using writing code in an IDE (for the linting!), and taking care of a makefile on my own. It has been awhile since I've used Code::Blocks, but each of the above bullets represents solutions/work-arounds that have helped me for your specific problem (or, in general) while I was using Code::Blocks.

Compiling Glew and SDL in C++ with Code::Blocks

I've spent the last 24 work hours trying to get this to compile, using multiple searches and tutorials (Some say I need to link it dynamically while others say I need to statically), I can not get this to work. I'm relatively new to compiling and linking so any help would be appreciated.
Here is my code(As you can tell by my comments I was very tired last night with coding this):
#define NO_SDL_GLEXT
#define GLEW_STATIC
#include "GL/glew.h"
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
int main (int argc, char* args[]){
//Loads the SDL video module
SDL_Init(SDL_INIT_VIDEO);
//Creates the SDL Surface for OpenGL to draw to
SDL_Surface* surface = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL );
//Sets the caption...
SDL_WM_SetCaption("OpenGL",0);
glewExperimental = GL_TRUE;
glewInit();
//Main event loop, this is the BREAD AND BUTTER LADIES AND GENTLEMEN
SDL_Event windowEvent;
while (true){
if (SDL_PollEvent(&windowEvent)){
//If you close the appplication, causes it to actually stop running :D
if (windowEvent.type == SDL_QUIT) break;
//Close it with the ESC key! :D:D:D:D:D:D:D:D:D:
if (windowEvent.type == SDL_KEYUP && windowEvent.key.keysym.sym == SDLK_ESCAPE) break;
}
//Literally the one extra line of code needed to do double buffering
SDL_GL_SwapBuffers();
}
//I wish I knew what this ended...LOPE JK
SDL_Quit();
return 0;
}
And in the search directories I have:
Compiler:
E:\Programs\CodeBlocks\glew-1.10.0\include
E:\Programs\CodeBlocks\SDL-1.2.15\include
Linker:
E:\Programs\CodeBlocks\glew-1.10.0\lib\Release\x64
E:\Programs\CodeBlocks\SDL-1.2.15\lib
And finally in the Linker settings I have
Link libraries:
E:\Programs\CodeBlocks\glew-1.10.0\lib\Release\x64\glew32s.lib
Other linker options:
-lmingw32 -lglew32s -DGLEW_STATIC -lSDLmain -lSDL
The error I am currently getting is:
In function 'SDL_main':
undefined reference to `glewExperimental'
undefined reference to `glewInit#0'
Thank you in advance for all the help!
You are getting linker errors. It looks like you are compiling your application using the Mingw compiler. But, are you sure the glew library you are using was also built with/for Mingw? If you didn't build glew yourself and downloaded a prebuilt binary, it was most probably built using Visual Studio. Note that its not straightforward to mix libraries from different compilers like this.
Look at this:
http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
P.S: Unless philosophical reasons against proprietary software is preventing you from doing so, you really ought to consider using Visual Studio compilers. The express editions of Visual Studio compilers are free (beer-like). You can use the Visual Studio compiler in really great IDEs like QtCreator.

Having trouble using SDL framework on XCode

I am having trouble using the SDL framework in my xcode project, my main (all there is in the project at the moment) currently looks like this:
#include <SDL/SDL.h>
#include <iostream>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
And the error I am receiving when building is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __Z8SDL_mainiPPKc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It does not give me an error about the framework not being found. and I have looked around Stackoverflow for this problem and I did find one person who had their framework in the wrong place on their OS. But I have tried all places (/library/frameworks && /~username~/library/frameworks && /system/library/frameworks) and still no luck.
Additional info:
I did however notice after some searching on the internet that the official website http://www.libsdl.org/download-1.2.php
Does not have a OSX version of the Development library. While many tutorials on how to use SDL on OSX say these are required.
Also I am adding my library via Xcode itself ,not through drag'ndrop. Xcode seems to reckognize it.
Would be much appreciated if anyone could help, going crazy over this error.
UPDATE:
Still no luck, I have followed every step provided by solutions here below. perhaps this screenshot is of any help.
The main() function is corrected, but didn't help. I tried changing the path to the header files, im lost on this one. Does anyone perhaps have any alternatives to building this in xcode?
UPDATE2:
The suggestion worked, however now it is giving me this warning, which won't let me run the application.
Fixed! I removed the path in the build settings. Strange how I still don't know what went wrong, either way. thanks a lot for the help! made my day!
I see three possible problems. The first problem is how you're including SDL.h in your code. You should include SDL.h with the following code:
#include "SDL.h"
The second possible problem is you haven't added the files SDLMain.h and SDLMain.m to your project. Those files are necessary to compile SDL code on Mac OS X. The files are in the devel-lite folder on the SDL disk image.
The third possible problem is that your project doesn't link to the Cocoa framework. The Mac version of SDL uses Cocoa so you need the Cocoa framework in your project.
The following article walks you through the setup of an Xcode 4 project for SDL:
Using SDL with Xcode 4
UPDATE
I noticed a possible problem in how you defined the main() function. You have a space between char and * and another space between * and argv, which could be causing the error. The main() function in my SDL code is defined in the following way:
int main(int argc, char *argv[])

Compile with MinGW and install FTGL lib to render text in OpenGL

Context
I'm making a game engine using SDL and OpenGL. I'm trying to find the best way to output text to the screen with fancy true type font and I came across FTGL. (If you have better/easier solution, feel free to share it.)
Compiling FTGL
It requires FreeType2 in order to work so I got it, compiled it (using Code::Block) and then I compiled the FTGL lib using Code::Block too.
I'm using Eclipse Indigo with the CDT plugin. I'm not sure which lib to take, since there are a .lib and a .dll file. I tried both, but this doesn't seem to change my problem at all, which curently is on this line:
FTfont* m_Font = new FTTextureFont(filename.c_str());
The error is:
The type 'FTTextureFont' must implement the inherited pure virtual method 'FTFont::MakeGlyph'
It doesn't change if I use another FTfont subclass, e.g. FTPixmapFont constructor.
Here are the linker flags which I tried:
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lftgl -lfreetype234_D -lopengl32 -lglu32
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lftgl -llibfreetype -lopengl32 -lglu32
I'm using eclipse with MinGW.
I have copied and pasted
the src/FTGL folder and
the content of the Include folder of the freetype library
to the include folder of the MinGW directory.
Both freetype2 (there a are freetype.lib, freetype248_D.lib and freetype-bcc.lib, not sure wich to link in the project properties) and ftgl (ftgl.dll, ftgl_D.lib, ftgl_static_D.lib) lib files into the corresponding lib folder of the MinGW directory.
If I click on the line that has the error and uses the Open Declaration option, eclipse takes me to the FTTextureFont header file which has FTTextureFont(char const*) defined and the MakeGlyph function defined too.
You can find the full code on the GetRandomGame project page if you want to see more, or as a reference.
Here's the Compiler version
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.1/configure --enable-languages=c,c++,fortran,objc,ob
j-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp -
-disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runti
me-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.1 (GCC)
EDIT
Similar question: FTGL undefined references everywere?, but the answer of that question doesn't solve my problem, I still get these two errors.
EDIT 2
I was compiling using MSVC when I first tried to use FTGL and had two errors.
I finally used code::block to import the MSVC project provided with both libraries. One of the two errors got solved! But, I still got this one:
The type 'FTTextureFont' must implement the inherited pure virtual method 'FTFont::MakeGlyph'
EDIT 3
I've tried to compile FTGL 2.1.2 with freetype 2.3.4 and that hasn't solved my problem. I've tried FTGL 2.1.3 rc5 with freetype 2.3.4, same thing. Just to be sure, I've also tried freetype 2.3.5 and 2.4.8 (the first time was already 2.4.8).
I've tried moving the MakeGlyph code into the header file, but that hasn't solved the problem.
Here are some useful links i've found, but they haven't solved the problem yet.
OpenGL font rendering with FTGL
Glut and FTGL
EDIT 4
When I remove the ftgl linker flag, the compiler complains about this:
undefined reference to FTPixmapFont::FTPixmapFont(char const*)'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
And I still get the pure virtual method MakeGlyph error.
With the ftgl linker flag, the only error I get is the "must implement the inherited Pure Virtual method MakeGlyph". The compiler builds the project fine (but crashes at start up):
-lmingw32 -lSDLmain -lSDL -lSDL_image -lSDL_net -lSDL_ttf -lfreetype -lftgl -lopengl32 -lglu32
Build complete for project GetRandomGame
So I'm thinking that the FTGL lib isn't compiled correctly since it should have the FTPixmapFont.cpp defining the pure virtual method MakeGlyph. I'm kind of new with all those lib things in c++ and I might be doing something wrong building the lib.
EDIT 5
I have read and seen in an example that I needed to have a config.h file, so I added a properly configured config.h file that comes with FTGL.
Then I found the following code in an example provided in the ftgl directory and I added that to my Font class.
class FTHaloFont : public FTFont
{
public:
FTHaloFont(char const *fontFilePath) : FTFont(fontFilePath) {}
protected:
virtual FTGlyph* MakeGlyph(FT_GlyphSlot slot)
{
return new FTHaloGlyph(slot);
}
};
Even if I define MakeGlyph there, Eclipse complains about an unimplemented pure virtual method FTFont::MakeGlyph.
EDIT 6
I've tried debugging from command line with gdb and it tells me that freetype6.dll can't be found. I'm trying right now to find a solution.
The compiler is telling you that the method FTTextureFont::MakeGlyph has not been written.
You have to either:
Inherit from FTTextureFont and write the MakeGlyph method.
Find a child class of FTTextureFont that does what you want.
According to the FTGL docs, any class derived from FTFont (which FTTextureFont has as a parent) has to implement MakeGlyph.
I'd look for code on the 'net to see what others have done were I you.
You're using wrong class.
The correct class to use is FTGLPixmapFont.
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <common/timer.h>
#include <common/keyboard.h>
#include <FTGL/FTGL.h>
#include <FTGL/FTGLPixmapFont.h>
#include <wchar.h>
#include <math.h>
using Keyboard::keyPressed;
static Timer timer;
static int scrWidth = 640;
static int scrHeight = 480;
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
Keyboard::init();
timer.reset();
timer.setMaxFps(100);
timer.setMinFps(5);
if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}
glewInit();
FTGLPixmapFont font("DejaVuSans.ttf");
font.CharMap(ft_encoding_unicode);
font.FaceSize(10);
SDL_ShowCursor(SDL_DISABLE);
glClearColor(0.2, 0.2, 0.2, 0.2);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
bool running = true;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
timer.update();
Keyboard::update();
if (keyPressed(SDLK_ESCAPE))
running = false;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-scrWidth/2.0f, scrWidth/2.0f, scrHeight/2.0f, -scrHeight/2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
wchar_t buf[256];
swprintf(buf, 256, L"fps: %0.2f", timer.getFps());
glRasterPos2i(10-scrWidth/2, 20-scrHeight/2);
font.Render(buf);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
Also, read the tutorial.
Judging by my source code, the version I'm using is 2.1.2 (since 2.1.3 is rc5 which is "release candidate", which is not "stable"). It looks like you're trying to use 2.1.3 - rc5. Downgrade to 2.1.2 and try again. Also, remember to tell what library version you're using next time you run into problem like this.
Alternatively you could try to fix FTGL (which is pain, and project seems to be abandoned). Or you could try to make your own freetype2-based font renderer (which is also pain, but might pay off in the long run).
EDIT 6
I've tried debugging from command line with gdb and it tells me that freetype6.dll can't be found.
I have placed freetype6.dll besides my executable file in the debug folder and it worked.
These are things that have slowed me or that I have learn while searching a solution:
Compiling with the same Compiler that I'm using
Learn to use code::block to import Visual Studio project
Actually be able to compile libraries in code::block without any warnings
Playing with linker flag (remove one and see what's going on) to help solve error and find problems
Learn to read provided example to find files that are necessary
And use gdb from command line
I got another problem while renaming some file of the project and I had to reconfigure my whole project in Eclipse (literally delete all files and checkout the SVN again). After that, the pure virtual error had disappeared! So I tried to debug the project and I got this error:
gdb: unknown target exception 0xc0000135 at 0x7724f52f
Eclipse wasn't helping me that much, so I tried the command line debugger and it told me that freetype6.dll was missing. And that was it!