As I tried to upgrade from my graphics programming from "legacy openGL" x "SDL 1.x" to OpenGL3+ x SDL2 and I came across linking problems. I tried many linking parameters but nothing seems to work.
I tried SDL1 linking params: linking listing 1
-lmingw32 -lSDLmain -lSDL
It works fine with this code: code listing 1
#include <SDL2/SDL.h>
int main(int argc, char **argv)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
SDL_Quit();
return -1;
}
SDL_Quit();
return 0;
}
but when I add SDL2 codes to it like this: code listing 2
#include <SDL2/SDL.h>
int main(int argc, char **argv)
{
SDL_Window* window(0);
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
SDL_Quit();
return -1;
}
window = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I get linking errors: error listing 1
g++ -o dist/Debug/MinGW-Windows/sdl2 build/Debug/MinGW-Windows/main.o -lmingw32 -lSDLmain -lSDL
build/Debug/MinGW-Windows/main.o: In function `SDL_main':
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:21: undefined reference to `SDL_CreateWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:23: undefined reference to `SDL_DestroyWindow'
collect2.exe: error: ld returned 1 exit status
swaping linking params to this: linking listing 2
-lmingw32 -lSDL -lSDLmain
gives even more errors (I won't write them down). Adding a '2' to the params like this: linking listing 3
-lmingw32 -lSDL2main -lSDL2
gives me more linking errors than without the 2s: error listing 2
g++ -o dist/Debug/MinGW-Windows/sdl2 build/Debug/MinGW-Windows/main.o -lmingw32 -lSDL2main -lSDL2
build/Debug/MinGW-Windows/main.o: In function `SDL_main':
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:14: undefined reference to `SDL_Init'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:16: undefined reference to `SDL_Quit'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:21: undefined reference to `SDL_CreateWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:23: undefined reference to `SDL_DestroyWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:24: undefined reference to `SDL_Quit'
c:/programs/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
as if no single SDL functions were found and I do have the libraries (libSDL2.a, libSDL2.dll.a, libSDL2.la, libSDL2main.a, pkgconfig\sdl2.pc) in the lib directory. I also tried using this set of params in netbeans: linking listing 4
$(pkg-config --cflags --libs sdl2)
and it doesn't do any good. Can anyone please help me on this. Thank you!
Problem Solved! I tried to troubleshoot the linking errors. Now everything works fine!! LazyFoo's tutorial has the answer "Most importantly i686-w64-mingw32 which contains the 32bit library [...] This is important: most compilers still compile 32bit binaries by default to maximize compatibility. [...]". So I re-extracted all the 32bit files in their corresponding directories. Now everything works like a charm. My guess is I used the 64bit libs with the 32bit mingw compiler.
both linking params work fine. this:
$(pkg-config --cflags --libs sdl2)
or this:
-lmingw32 -lSDL2main -lSDL2
Related
UPDATE I switched to the meson build system. Everything is working fine now!
I am very new to using C++, OpenGl, and Gnome Builder. I have a very very basic foundation with C++ and I know how to link header files and libraries in CodeLite, however after messing around Gnome Builder I want to make the switch. I haven't found any beginner friendly tutorials on using Builder. I am just lost as to how I should link external libraries in Builder. Do I just manually edit the Makefile or is there a setting somewhere that will automate the makefile process with automake? Am I wrong in assuming that this is a makefile problem? Apologies if this is a very novice question.
I am using Ubuntu. I am getting the error "undefined reference to ..." for all the glfw and glew variables and headers. After installing libraries with apt, I have my libraries installed in usr/lib/x86-64-linux-gnu, headers in usr/include.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main ()
{
glewExperimental = true;
if (!glfwInit() )
{
fprintf(stderr, "Failed to initialize GLFW \n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if ( window == NULL )
{
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. \n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to Initialize GLEW. \n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
while ( glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
return 0;
}
When trying to build I get this error output~
g++ -o practice -Wall -ggdb -fno-omit-frame-pointer -O2 practice.cpp
/usr/bin/ld: /tmp/ccLx11Ky.o: in function main':
/home/joe/Projects/practice/practice.cpp:30: undefined reference toglewExperimental'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:31: undefined reference to glfwInit'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:36: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:37: undefined reference to glfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:38: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:39: undefined reference to glfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:40: undefined reference toglfwWindowHint'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:43: undefined reference to glfwCreateWindow'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:50: undefined reference toglfwMakeContextCurrent'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:51: undefined reference to glewExperimental'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:52: undefined reference toglewInit'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:58: undefined reference to glfwSetInputMode'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwWindowShouldClose'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:61: undefined reference to glClear'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:62: undefined reference toglfwSwapBuffers'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:63: undefined reference to glfwPollEvents'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwGetKey'
/usr/bin/ld: /home/joe/Projects/practice/practice.cpp:47: undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make: *** [Makefile:8: practice] Error 1
My default Makefile looks as follows~
all: practice
WARNINGS = -Wall
DEBUG = -ggdb -fno-omit-frame-pointer
OPTIMIZE = -O2
practice: Makefile practice.cpp
$(CXX) -o $# $(WARNINGS) $(DEBUG) $(OPTIMIZE) practice.cpp
clean:
rm -f practice
# Builder will call this to install the application before running.
install:
echo "Installing is not supported"
# Builder uses this target to run your application.
run:
./practice
You need to customize the build command (practice) to include the required libraries. Conside using pkg-config --libs glew (or pkg-config --libs -static glew) to find which libraries are needed, and pkg-config --cflags for command line flags, if any.
Most likely:
Add '-lglfw3' to the 'practice' build command,
If you use static libs, add '-DGLEW_STATIC'
See glfw Errors with glfwWindowHint, and GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
CodeBlocks' GLFW Project is outdated and works only with GLFW 2.7. I am using the latest version, which is 3.0.4, and trying to link it in CodeBlocks statically (I hope that I am using the correct terminology). I would be really happy if someone told me how to do it step by step. I would also like to create an empty project if possible and do everything manually.
This is the code that I am trying to run:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
This is what I have in my "Other linker options":
-lmingw32 -lopengl32 -lgdi32
and I also copied GLFW folder that is containing the header files in CodeBlocks/MinGW/include.
This is pretty much all I did and I get the following build log:
-------------- Build: Debug in Initializing OpenGL (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o "bin\Debug\Initializing OpenGL.exe" obj\Debug\main.o -lmingw32 -lopengl32 -lgdi32
obj\Debug\main.o: In function `main':
D:/Development/OpenGL/Initializing OpenGL/main.cpp:8: undefined reference to `glfwInit'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:12: undefined reference to `glfwCreateWindow'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:15: undefined reference to `glfwTerminate'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:28: undefined reference to `glfwSwapBuffers'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:31: undefined reference to `glfwPollEvents'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:23: undefined reference to `glfwWindowShouldClose'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:34: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
Jigger your project config so that the mingw invocation looks something like this:
x86_64-w64-mingw32-g++.exe -Llib -o bin\Debug\glfw-proj.exe obj\Debug\main.o -lglfw3 -lopengl32 -lgdi32
Note the -lglfw3.
Procedure:
Project -> Build options...
Select the top-level configuration (i.e., not Debug or Release)
In the Search directories -> Compiler tab add the path to your GLFW headers (GLFW/glfw3.h and friend)
In the Search directories -> Linker tab add the path to your GLFW library files (libglfw3.a and friends)
In the Linker settings tab add 3 Link libraries: glfw3, opengl32, and gdi32.
I am trying to compile SDL2 with mingw and I get this error:
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.
o): In function `main':
e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src
/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
I googled this error and it looks like I need to specify to link with the -mwindows tag but that doesn't work. Here is my batch file:
g++ -o Game.exe Main/Main.cpp -lmingw32 -LC:\MinGw\include\SDL2\lib\x86 -mwindows -lSDL2main -lSDL2
pause
start /d "C:\Users\Mathew Bergen\Documents\Programming\C++\LD Practice" Game.exe
How do I fix this error?
int main(int argc, char** agv){
//code here
}
For your main function, most of the time I've seen that link error this was the problem. I know I'm really late at posting an answer it's just for other people who have the same problem and stumble upon this page
I'm trying to create a SDL2 project with Eclipse Kepler and MinGW on Windows.
I already added SDL2 libs in MinGW (.a) in C:\MinGW\lib, SDL2 include in MinGW(C:\MinGW\include\SDL2) and I also added in projects properties -> C/C++ general -> paths and symbols -> librairies the following lines in that order :
mingw32
SDL2main
SDL2
Then I put '-mwindows' in MinGW C++ linker at the end of the line "Command line pattern"
I also added -Dmain=SDL_main for the entry point...
But the compiler gives me error :
main.cpp:7: undefined reference to `SDL_CreateWindow'
this is the code :
#include <SDL2/SDL.h>
int main(int, char**)
{
SDL_Window *pWindow = nullptr;
pFenetre = SDL_CreateWindow("Test SDL 2.0", 0, 0, 320, 240, SDL_WINDOW_SHOWN);
if (!pWindow)
{
return -1;
}
SDL_DestroyWindow(pWindow);
return 0;
}
And this is the build console :
Info: Internal Builder is used for build
g++ "-LC:\\MinGW\\lib" -o Test.exe main.o -lmingw32 -lSDL2main -lSDL2 -mwindows
main.o: In function `SDL_main':
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:7: undefined reference to `SDL_CreateWindow'
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:13: undefined reference to `SDL_DestroyWindow'
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: erreur: ld a retourné 1 code d'état d'exécution
what's wrong ?
Make sure you're using the right version of the library. You can't mix 64-bit import libraries with 32-bit compiler. For the SDL2 library you downloaded(SDL2-devel-2.0.0-mingw.tar.gz) it comes with both 32-bit and 64-bit. i686-w64-mingw32 is 32-bit and x86_64-w64-mingw32 is for 64-bit.
I'm using Code::Blocks, that's my code:
#include "SDL2/SDL.h"
int main(int argc, char* args[]) {
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Quit();
return 0;
}
I'm building like:
mingw32-g++.exe -o C:\..\main.exe C:\..\main.o -lmingw32 -lSDL2main -lSDL2
And getting that:
undefined reference to "SDL_Init"
undefined reference to "SDL_Quit"
I'm pretty sure the linker finds the libs cause if I change them to something random it complains "can't find whatever".
A bit late, but I just stumbled over a similar problem on Linux.
This results in linker errors:
g++ $(pkg-config --cflags --libs sdl2) sdl2test.cpp
sdl2test.cpp:(.text+0x11): undefined reference to `SDL_Init'
sdl2test.cpp:(.text+0x20): undefined reference to `SDL_GetError'
sdl2test.cpp:(.text+0x34): undefined reference to `SDL_Quit'
This works:
g++ sdl2test.cpp $(pkg-config --cflags --libs sdl2)
even if this is an Linux Problem, i came throug this post via google.
Mayb it could help someone else: i had the same Problem with Windows XP 32bit Codeblocks: Mingw + SDL2 and i fixed it after i copied the right SDLFolders (include, lib...) to the mingw-folder. The reason why i trapped to this pifall is that the naming of the SDL-Rootfolder, out of the DEV-Pack is a bit confusing. You got a "x86_64-w64-mingw32"-Folder which is for 64bit Compiler and "i686-w64-mingw32" which is for 32bit Compiler (like the moste still are). i messed this up because of the "x86..."naming, still dont know why they are writing it this way.
After overwriting the right files it works fine for me.
Are you sure you have your libraries in you path ?
Try adding -LC:/whatever/ with the folder that actually contains you libSDL2.a and other *.a to your compiler's arguments.