simple openGL program fails to link in ubuntu - c++

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

Related

Freeglut 64 bit program linking errors

I'm trying to compile a 64 bit version of an OpenGL C++ program using freeglut. I followed the exact instructions on this website to set up freeglut with MinGW. I have the header files in C:\MinGW\include\GL, I have the 32 bit libraries in C:\MinGW\lib and 64 bit libraries in C:\MinGW\lib\x64, and I have the 64 bit freeglut.dll in my project directory. However, even the simplest of OpenGL programs don't link successfully...
My code is minimal:
// test.cpp
#include <GL/glut.h>
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
}
And I compile it with the exact commands given on the readme/on the website:
g++ -c -o test.o test.cpp -I"C:\MinGW\include"
g++ -o test.exe test.o -L"C:\MinGW\lib\x64" -lfreeglut -lopengl32 -Wl,--subsystem,windows
(except of course I changed the directories and changed gcc to g++)
The compile runs fine, but linking throws these error messages:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit#12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit#8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit#8'
collect2.exe: error: ld returned 1 exit status
I've tried to change -lopengl32 in the command to -lopengl64 and -lopengl, but the linker couldn't find those libraries.
I can't answer your specific case, but another approach would be to use mingw-w64, with MSYS2 as a package manager. The old mingw.org project is not well maintained, unlike mingw-w64 and the MSYS2 package database. There is probably some mismatch between how your precompiled binaries were built and the compiler you are currently using.
On my system I compiled your code with g++ -o gl gl.cpp -lfreeglut and it worked first time .
How to install MinGW-w64 and MSYS2?
Then use pacman -Ss glut to find the freeglut package.

OpenCV: Undefined reference to xcb_poll_for_reply

As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.

Linking OpenGL using MinGW on Windows x86_64

I would like to develope an application using C++ and OpenGL for Windows 64bit.
I am using the following Compiler x86_64-w64-mingw32-g++.
The following code snippet (Test.cpp) is sufficient to trigger the error I get:
#include <GL/gl.h>
int main(int argn, char **argv) {
glClear(GL_COLOR_BUFFER_BIT);
}
(I know this code is meaningless, but it is sufficient to trigger the error during linking.)
I use the following Makefile:
Test:
g++ -lopengl32 -o Test Test.cpp
This yields the following error:
undefined reference to `__imp_glClear'
I have no clue what I am missing, and would be very thankfull for any advice.
For me the g++ main.cpp -o run.exe -lopengl32 seems to work just fine, so you most likely wont need -Wl,--enable-stdcall-fixup to compile it.

Compiling SDL2 with mingw using sublime text 2 errors

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

SDL2 won't link properly

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.