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.
Related
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.
I'm relatively new to c++ and have been following the Lazy Foo' tutorial for installing SDL2 in visual studio 2010 (But I am using visual studio 2015).
I have followed the guide a couple of times now, so pretty certain I have incuded the right files etc. Here is a screen shot of my VC++ Directories settings:
#include <SDL.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = 0;
window = SDL_CreateWindow("SDL window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
SDL_Delay(1000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
When compiled (32 bit) I receive this error:
......\documents\sdl\sdl2-2.0.4\include\sdl_stdinc.h(50): fatal error C1083: Cannot open include file: 'stddef.h': No such file or directory
I also have the error "cannot open source file stdio.h" when hovering over the include statement for stdio.h
I'm going to continue to look at it all, but if anyone has any suggestions that would be greatly appreciated. Failing that I'll probably just install on Linux :)
Cheers and thanks for your time.
Suggestions: Use the dropdown, (inherit from parent or project defaults) to repair the visual c++ directories which you shouldn't ever edit.
Don't add it to the c++ directories, That's a weird idea. Add it to Configuration Properties > c/c++ > additional include directories. If you had done that, then you couldn't have possibly messed up the compiler. It's a lot simpler too. Just paste the one directory in there. That's what it's meant for.
Also Lazy Foo seems to only to have a VS2010 guide. Maybe I just couldn't find the VS2015 guide...
The error message
fatal error C1083: Cannot open include file: 'stddef.h': No such file or directory
tends to happen when you're building your program with a version of MSVC different from the one used to build the library.
You should find out which version the SDL binaries were built with, and try using that. A newer MSVC might work, an older one might not.
You can always try building SDL yourself.
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.
I have been trying to compile the most basic SDL application, but no matter what I do I keep getting this error:
c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain#16'
I searched for solutions for this, but they all had to do with either Visual C++ or a missing main. I am not using Visual C++, and I have defined main.
Here's my code:
#include "SDL/SDL.h"
int main( int argc, char* args[] )
{
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
Don't use "Other linker options". Use the "Link libraries" section. Add the following items.
mingw32
SDLmain
SDL
You can put -mwindows in the "Other linker options" section.
In case someone else comes across this, I put -lmingw32 after -lSDLmain and -lSDL which caused this issue for me. Putting -lmingw32 first fixed it.
I encountered the same error in a project of mine that I want to compile both on linux and windows.
I use a makefile to compile the project.
A solution that has worked for me, although I admit it is a bit of a hack is adding this to main.cpp (wherever your main function would be)
extern "C" {
int WinMain(int argc, char** argv)
{
return main(argc, argv);
}
}
This makes the linker find WinMain and use it as the entry point in the program. I can also hope that this solution doesn't break linux compilability, hopefully it will be considered just an unused function.
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!