C++ strange "undefined reference" - c++

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.

Related

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

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.

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.

Undefined reference to 'glewExperimental', 'glewInit#0', '__glewGenBuffers'

I'm sorry if this is a duplicate question but I've looked through StackOverflow and Google and haven't come up with an answer yet.
I'm using Code::Blocks and SDL and trying to get GLEW working with openGL.
The program code is:
#include <Windows.h>
#include <SDL.h>
#define GLEW_STATIC
#include <glew.h>
int main(int argc, char *argv[])
{
//initialize SDL
SDL_Init(SDL_INIT_VIDEO);
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);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
SDL_GL_DeleteContext(context);
SDL_Quit();
return 0;
}
When I build it, it returns:
undefined reference to 'glewExperimental'
undefined reference to 'glewInit#0'
undefined reference to '__glewGenBuffers'
It's not returning an error finding glew.h, so I assume it's a problem with the linker finding the .lib files.
I've added the folder where the .lib files are under Project > Build Options > Search Directories > Linker and added 'glew32s', 'glew32' (I've tried switching the precedence on glew32s and glew32), and 'opengl32' under Linker Settings > Link Libraries, and added '-lglew32s', '-lglew32' (again tried switching the precedence and removing one or the other), and '-lopengl32' under Linker Settings > Other linker options.
I even tried adding the .lib files, .h files, and .dll files to the project folder and adding search directories to the project folder. It still returns the same error.
I'm using the most recent version of glew (1.13.0) on a 64-bit OS. I've tried linking to the 64-bit and 32-bit versions (because I'd read most compilers compile in 32-bit by default for compatibility). Am I linking to the wrong files?
Thanks for reading.

Why cant I use some GLFW methods

I'm using Linux OS and Eclipse. For some reason eclipse does not recognise some of the GLFW commands despite the fact that glfw3.h is included properly. I'm unable to use GLFWwindow, glfwCreateWindow and glfwDestoyWindow. Anyone know why?
Edit
Just to be specific my IDE is underlining these methods as if they are not defined.
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if (!glfwInit()){
return 0;
}
GLFWwindow* window = glfwCreateWindow(680, 480, "test", NULL, NULL);
cin.get();
if (window){
glfwDestroyWindow(window);
}
glfwTerminate();
return 0;
}
If I remember correctly, the Eclipse editor in C/C++ mode uses the compiler to generate its symbol database, so unless you build the editor will not recognize new symbols.
So the solution is to actually build your project, even if the editor tells you there are errors.

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.