GLUT program link error - c++

I want to compile a GLUT program
#include <GL/glut.h>
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Lighthouse3D- GLUT Tutorial");
return 1;
}
Compile and link command:
gcc -o main.exe main.c -lglut32
Result:
main.o:main.c:(.text.startup+0x1c): undefined reference to `glutInit'
main.o:main.c:(.text.startup+0x28): undefined reference to `glutInitDisplayMode'
main.o:main.c:(.text.startup+0x3c): undefined reference to `glutInitWindowPosition'
main.o:main.c:(.text.startup+0x50): undefined reference to `glutInitWindowSize'
main.o:main.c:(.text.startup+0x5c): undefined reference to `glutCreateWindow'
collect2: ld returned 1 exit status
The actual lib file of glut (3.7.6, called glut32.lib) is in the lib folder of mingw and the include file in include/GL.
What to do now?

Try it with libglut.a and libglut32.a instead of glut32.lib. To compile glut programs with minGW.
Do GLUT for Win32 is a Windows port of the original GLUT library. It’s no longer maintained or supported. The MinGW “w32api” package already comes with two GLUT import libraries, “libglut.a” and “libglut32.a”, but doesn’t come with a glut header file.
If you’ve ever downloaded a GLUT header from the internet, and attempted to link an application against either of these import libraries, you likely would have seen it fail with various undefined references.
Setting Up GLUT for Win32 With MinGW Look for Setting Up GLUT for Win32 With MinGW
If you want to have your own "build" of libglut32.a and glu32.dll.
Download Source glut-3.7.6-src.zip (4.76 MB)
Download makefile
Make the following modifications:
Add the following lines to include/GL/glut.h starting at line 12:
#ifdef __MINGW32__
#define APIENTRY __stdcall
#define CALLBACK __stdcall
#endif
Comment out line 21 in lib/glut/win32_winproc.c so that it looks:
//#include <crtdbg.h>
makefile line 5 replace:
-enable-auto-import with --enable-auto-import
run make
Have tried it with the example.c file from the guidance in the link Setting Up GLUT for Win32 With MinGW.
With the prebuild libs and the self compiled libs. Works both.
below is the result.
Example GLUT Application
example.c from the link above Setting Up GLUT for Win32 With MinGW.
#include <stdlib.h>
#include <GL/glut.h>
void keyboard(unsigned char key, int x, int y);
void display(void);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT Test");
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}

Related

OpenGL Glut display function showing a black screen on Mojave [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to run a very simple example on OpenGlut for my class assignment and for some reason the code is not working on my xcode. I am currently using Xcode 10 on macOS Mojave.
Following is the code:
#include <GLUT/glut.h>
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f( -0.5, -0.5 );
glVertex2f( 0.5, -0.5 );
glVertex2f( 0.0, 0.5 );
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Hello, GL");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
This is the error message:
2018-09-29 14:38:03.737378-0700 gluttest[18974:837022] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported
2018-09-29 14:38:03.738891-0700 gluttest[18974:837022] MessageTracer: Falling back to default whitelist
2018-09-29 14:38:03.857540-0700 gluttest[18974:837022] flock failed to lock maps file: errno = 35
2018-09-29 14:38:03.858276-0700 gluttest[18974:837022] flock failed to lock maps file: errno = 35
To make things more clear, I am adding more information about what I have already tried:
The same code is running absolutely fine on Xcode 10 in High Sierra.
I have changed destination target to macOS 10.8 in Xcode
I have made sure OpenGL and Glut framework binaries are linked to the project.
For some reason, the window isn't drawn initially whenever glutMainLoop() is called on Mojave. You can kind of work around this by calling it in the keyboard function:
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
int main(int argc, char** argv) {
// ...
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
EDIT: A better idea:
bool hasDrawn = false;
void display() {
// ...
if (!hasDrawn) {
glutPostRedisplay();
hasDrawn = true;
}
}
void main() {
glutDisplayFunc(display);
}

Mac OS X refuses to create my OpenGL Window

#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
void GraphiqueAffichage() {
glClearColor(1.0, 1.0, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glFlush();
}
int main(int argc, const char * argv[]) {
// insert code here...
glutInitWindowPosition(10, 10);
glutInitWindowSize(480, 272);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutCreateWindow("Bonjour");
glutDisplayFunc(GraphiqueAffichage);
glutMainLoop();
return 0;
}
Hello
I am on a Mac using OS X 10.12, ans with this code, no window is displayed, is it normal ? Why ? Please help me.
The compilation is correct, no error, build successful, but no window is created !
I tried this code that works with windows but I have a Mac and it does not work, how to make it working ?
The compilation is correct, no error, build successful ...
but you get a list of errors when you run the program, right? "Successfully compiling" does not (alas) mean your code is correct.
Looking up the very first error message, it seems you forgot to call glutInit first:
int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitWindowPosition(10, 10);
...
(right where your code says, "insert code here"...)
man glutInit tells you why it failed as well:
glutInit will initialize the GLUT library and negotiate a session with the window system.
where "the window system" is Mac OS X.
In addition, your main is wrong. argv is not a const char * – with that const specifier, your compiler will yell at you.
With these changes, I get a nice yellow window – your glClearColor – and with the custom title "Bonjour".
You need initializer glut
glutInit(&argc, argv);
in your main.
//#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
void GraphiqueAffichage() {
glClearColor(1.0, 1.0, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glFlush();
}
int main(int argc, const char * argv[]) {
// insert code here...
glutInit(&argc, argv);
glutInitWindowPosition(10, 10);
glutInitWindowSize(480, 272);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutCreateWindow("Bonjour");
glutDisplayFunc(GraphiqueAffichage);
glutMainLoop();
return 0;
}

Setting up GLUT with VisualStudio 2012 on Windows 8

I'm running into trouble setting up GLUT (3.7.6 binaries obtained from Nate Robins) on Windows 8 64bit with VS2012. The glut32.dll is copied to the SysWOW64 dir, both include and lib path are set in my project files and the libraries are set in the Linker->Input settings ("...;glut32.lib;glu32.lib;opengl32.lib;...").
My code looks like this:
#include <GL/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutDisplayFunc(display);
glutMainLoop();
}
The build process is successful but the application crashes with the following error message:
Unhandled exception at 0x1000BBAE (glut32.dll) in HelloOpenGL.exe: 0xC0000005: Access violation writing location 0x000000A8.
The setup seems fairly simple. Any ideas what I'm missing?
The call to glutDisplayFunc() without opening a window caused the crash. This is the updated code that opens a new window before passing the display function:
#include <GL/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//Set the window size
glutInitWindowSize(250,250);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("Hello OpenGL");
//Set the display function
glutDisplayFunc(display);
//Enter the main loop
glutMainLoop();
}

math_3d.h with opengl

So, I was reading http://ogldev.atspace.co.uk/www/tutorial02/tutorial02.html and it said I needed math_3d.h for Vector3f.
I tried to include it:
#include <stdio.h>
#include "GL/glew.h"
#include "GL/gl.h"
#include "GL/freeglut.h"
#include "math_3d.h"
void render() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - First window demo");
/* Set */
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
Vector3f vertices[1];
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
G++ said "main.cpp:7:21: fatal error: math_3d.h: No such file or directory". I looked for an Arch Linux package for it, but I found nothing.
I found the file here:
http://ogldev.googlecode.com/svn-history/r75/trunk/tutorial36/math_3d.h
Am I supposed to download that file and place it my project directory, or is there a cleaner way of doing it?
Also, if I do include it in my directory, how can I add it to the g++ line?
gcc main.cpp -o main -lGLEW -lglut -lGL
Am I supposed to download that file and place it my project directory?
Yes
It's sufficient download the file in your directory. Is not required to add it to gcc command line.
Yes you have to download it, if you need this library in more than one project you can save it in /usr/include and use it as a C library with #include <math_3d.h>, avoiding to copy it in every single project's directory.

how to compile some code in visual studio in my MAC

I'm running win 7 in virtual machine in my mac (parallels).
I wrote some small code and I try to run it and I get this problem:
C:\Users\Administrator\Documents\Visual Studio 10\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
It tells me that he can't find the path in the users.
I check and all the path exists.
what I can do now?!
Edit: Here's the sample code being built:
#include<glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void myinit()
{
glClearColor(0.,0.,0.,0.);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5., 5., -5., 5);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutInitWindowPosition(100, 100);
glutCreateWindow("Black Window");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
This is not a path problem, this is a linker error.
What linker is telling you is that you need an external library to complete compilation of your code (maybe you're using some function of another library and you forgot to add them in your project parameters).