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
Related
I'm using VSCode on Windows 10 with MinGW compiler installed. I've tried using CImg library to edit images (http://cimg.eu/), and when I'm trying to compile code from tutorial (http://cimg.eu/reference/group__cimg__tutorial.html) I get this error:
C:\Users\Martini\AppData\Local\Temp\ccBswQ5w.o:tutorial.cpp:(.text$_ZN12cimg_library11CImgDisplay5paintEv[_ZN12cimg_library11CImgDisplay5paintEv]+0xba): undefined reference to `__imp_SetDIBitsToDevice'
collect2.exe: error: ld returned 1 exit status
I have CImg.h file located in folder with tutorial.cpp. Here's command I'm using to compile:
g++ tutorial.cpp -o tutorial.exe
It's my first time working with libraries in C++, and CImg looks fairly easy to use, but if you have used other libraries to edit photos, let me know.
Thanks in advance!
You need to link to GDI32 on Windows I think. Something like:
g++ tutorial.cpp -lgdi32 -o tutorial.exe
I got a Qt environment that I need to compile.
But when compiling, I get the following messages:
g++ -Wl,-s -mthreads -Wl,-subsystem,windows -o release\Prog.exe object_script.Prog.Release -L"c:\DevProgs\Qt\4.8.6\lib" -lmingw32 -lqtmain build\objects\myapp_res.o -LC:/Windows/System32 -lftd2xx -LC:/qwt-6.0.1/lib -lqwt -lQtSql4 -lQtGui4 -lQtNetwork4 -lQtCore4
[MGWPATH]/i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x1fb): undefined reference to `_imp__Sleep#4'
[MGWPATH]/i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x293): undefined reference to `_imp__SetUnhandledExceptionFilter#4'
Makefile.Release:303: recipe for target 'release\Prog.exe' failed
[MGWPATH]/i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x2a7): undefined reference to `_set_invalid_parameter_handler'
[MGWPATH]/i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x475): undefined reference to `_imp__GetStartupInfoA#4'
[MGWPATH]/i686-w64-mingw32/bin/ld.exe: [MGWPATH]/i686-w64-mingw32/lib/../lib/crt2.o: bad reloc address 0x0 in section `.CRT$XCAA'
collect2.exe: error: ld returned 1 exit status
where [MGWPATH] is C:/DevProgs/mingw32/bin/../lib/gcc/i686-w64-mingw32/4.8.2/../../../..
I sucessfully ran small demos of Qt4.8.6 alone and one with Qwt.
The only doubt I have is regarding the ftd2xx lib.
But, I think it is linked properly because I am able to produce errors of undefined function if I don't link the DLL.
The error message is not clear to me. So, I have absolutely no idea where to look from here!
Could someone help me?
Thank you!
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I was following http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php tutorial on how to use SDL with Code::Blocks since I've been having trouble with this in pretty much every damn IDE I've tried.
The tutorial is pretty straight forward, on step number 7 it states
"we have to tell the compiler to link against the libraries. Go under Linker Settings and paste -lmingw32 -lSDL2main -lSDL2". I did exactly that. Then it says that if you get a bunch of undefined reference errors, you messed up this step, I don't really see how it is possible for me to mess up this step, since it is a simple step.
I would really like to get started with this, while using MinGW and Code::Blocks.
Information that might help resolve this:
I have MinGW directory located in my C:
I have a folder SDL in my C: directory, within that folder I am linking the include and lib files from SDL to Code::BLocks by right clicking on project properties and adding the directories. This all seems to be working fine.Include Directory = C:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 Lib Directory = C:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\lib
As stated above, on the Linker Setting -> Other Linker Options: I wrote -lmingw32 -lSDL2main -lSDL2, yet I get a bunch of reference errors. I don't know what to try, I have been searching online for hours, I even replaced SDL_platform.h because it was causing issues and the undefined references are still there.
Please help. This is the code I am using to check if SDL is working, it isn't.
#include "SDL.h"
#include <iostream>
#include <cstdio>
#include <Windows.h>
int main( int argc, char* argv[])
{
// Fire up SDL, this starts all subsystems; audio video etc.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Now Shut it down
atexit(SDL_Quit);
return 0;
}
These are the errors I am getting:
-------------- Build: Debug in TITLE (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -IC:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -c C:\Users\Bryan\Desktop\CodeBlocks\TITLE\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LC:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\TITLE.exe obj\Debug\main.o -lmingw32 -lSDL2main -lSDL2
obj\Debug\main.o: In function `SDL_main':
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:10: undefined reference to `SDL_Init'
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:11: undefined reference to `SDL_GetError'
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:15: undefined reference to `SDL_Quit'
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
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 0 warning(s) (0 minute(s), 0 second(s))
You have created a Windows executable project. The wizard set the entry point to WinMain. Your code implements a command line program with main(int argc, char**argv) as entry point.
If you want to stay with the main you should create a new command line project and add you
source files to this project. Alternativly you could try to change the project type.
For the SDL errors you should check, that you use matching compiler and libraries (32 vs. 64 bit).
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
i'm trying to get into opengl programming, but fail to compile my first very very simple program. the linking process fails every time. i found this answer on stackoverflow, and have had all packages installed and told g++ which libraries to link against.
here's my sample program:
#include <GL/glut.h>
#include <GL/gl.h>
int main(int argc, char **argv) {
glutInit(&argc, argv);
return 0;
}
compiling results in the following error from the linker:
$ g++ -Wall -lglut -lGL -lGLU opengl.cpp
/tmp/cc1UAFPU.o: In function `main':
opengl.cpp:(.text+0x3b): undefined reference to `glutInit'
collect2: ld returned 1 exit status
anybody got any idea on this issue? there must be something which i am missing, but i just cannot see what. any hints to resolve this issue are highly appreciated!
might be order - either reorder libs, or put them after opengl.cpp