OpenGL program compiles but doesn't start correctly - c++

I'm trying out OpenGL and C++, and I followed this video tutorial on writing my program (my code is exactly the same as his). I also followed the instructions on the freeglut website here to set up freeglut, compile, and link my program. The source code compiles with no problem, but when I try running the exe I get an error. The only reason I could think of is that I'm not using an IDE, so I'm probably missing some compilation steps or missing some command line arguments when running the exe, which the IDE would have done automatically. Can someone tell me what I need to do to run my program correctly?
Here's my code:
#include <GL/glut.h>
void init() {
glClearColor(1.0, 1.0, 0.0, 1.0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("Window 1");
glutDisplayFunc(display);
init();
glutMainLoop();
}
When I compile I run
gcc -c -o hello.o hello.cpp -I"C:\MinGW\include"
gcc -o hello.exe hello.o -L"C:\MinGW\lib" -lfreeglut -lopengl32 -Wl,--subsystem,windows
Then I try to run hello.exe but I only get an error message "The application was unable to start correctly (0xc000007b)".
BTW I saw this duplicate question but I've tried putting the dll in the same directory (it was there from the start) but that didn't change anything.

Using the 32 bit freeglut dll (instead of the 64 bit dll) in my project fixed the problem.

Related

Why won't this OpenGL program open a window?

I am currently trying to learn both C++ and OpenGL, and I am beginning with the following code example:
simple.cpp
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}
It says here that this code can be compiled on MacOS using the following command:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
So I adapt the command in the following way:
gcc -Wno-deprecated-declarations -o simple simple.cpp -framework GLUT -framework OpenGL -framework Carbon
This seems to work and creates an executable called simple.
I am told that this code should generate a white square on a black background as follows:
However, when I try to run this file from the terminal using ./simple, the program runs continuously but nothing happens (that is, no window is generated at all), and so I have to terminate the program from the terminal.
Did I do something wrong here, or is this expected for the given code on MacOS?
EDIT1
To see what would happen, I tried to use the code presented in the aforementioned guide:
hello.c
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutDisplayFunc(display);
glutMainLoop();
}
As the guide says, this is "a simple OpenGL program that does nothing".
I compile it as follows:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
This compiles fine. However, when I try to run the executable, I get the following error:
GLUT Fatal API Usage: main loop entered with no windows created.
zsh: abort ./hello
According to this error message, the program was terminated because no windows were created. However, as I said, my simple.cpp program does not terminate (I have to forcefully terminate it), and it is written to create windows. So I'm guessing this means that windows are created in simple.cpp, but for some reason they just don't show up on MacOS? Can anyone else confirm this? Is the issue perhaps that blank windows don't show up on MacOS, and you need to include other graphical elements in order to make the window show?
EDIT2
The problem is that my understanding is that glutCreateWindow("simple"); in simple.cpp is supposed to create a window with "simple" as its title, so I don't understand why it isn't working.
EDIT3
Thanks to derhass's answer, I was able to make it work:
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}
YOu did not include the call to glutInit() into your simple.cpp example. You can't call any other GLUT commands before this intialization, and doing so results in undefined behavior.

How to Set Up SDL2 using Eclipse ubuntu

I've been trying to get SDL2 working with Eclipse on Ubuntu.
I have tried following this, this, and this but I just cannot get it working.
How can I get SDL2 working with Eclipse and have everything properly linked together?
EDIT:
When ever I compile the program it comes back saying that SDL.h is missing or there is no such directory, even though i can see SDL in the includes from the project list.
When compiling i have tried using:
gcc SDLTest.cpp
g++ SDLTest.cpp
gcc -o test SDLTest.cpp `sdl-config --cflags --libs`
I'm unsure of the difference between using GCC or G++, and i got the third compile from here.
I've added the SDL include folder to the project but still nothing
Image Project Explorer and Code
GCC C++ Linker Libraries
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"an SDL2 Window",
20,
20,
640,
480,
SDL_WINDOW_OPENGL);
if (window == NULL)
{
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
`
This will install everything necessary to build programs that use SDL: sudo apt-get install libsdl2-dev
Create a new Eclipse project.
Go to properties of the project
C/C++ Build
Settings
GCC C++ Linker -> Libraries
Click on Add... and type SDL2
Apply and reindex the project
I used this documentation. Tested with SDL CreateWindow.

Freeglut error: fgInitGL2: fghGenBuffers is NULL

I'm transferring a program from OSX to Windows, but one error is still nagging me. The error occurs during run-time in gdb. Compiling and linking goes all fine.
freeglut (C:\path\to\file.exe): fgInitGL2: fghGenBuffers is NULL
Outside the GDB environment it gives an APPCRASH (windows-shell) or Segmentation fault (mingw64-shell).
My linker flags are:
-std=c++11 -lstdc++ -lz -lm -lmysqlcleint -lpthread -lboost_thread-mgw49-mt-d-1_57 -lboost_system-mgw49-mt-d-1_57 -lboost_regex-mgw49-mt-d-1_57 -lcurl -lfreeglut -lglu32 -lopengl32 -lws2_32 -lwsock32 -U__CYGWIN__
I'm working in msys2 mingw-w64. During runtime, the program tries to open a new window (at least a pictogram rices in the windows taskbar), but the construction of the window won't succeed. The program runs fine on OSX, where I use glut instead of freeglut.
Header (amongst others):
#include <direct.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
CPP (amongst others):
void interface::startInterface(int &argc, char **argv){
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (width, height);
glutInitWindowPosition (1920, 0);
glutInit (&argc, argv);
glutCreateWindow ("TIFAR 2.0");
LoadGLTextures(); // Load The Texture(s) ( NEW )
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
// Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc (interface::display);
glutReshapeFunc (interface::reshape);
glutIdleFunc (interface::idle);
glutKeyboardFunc (interface::processNormalKeys);
glutMainLoop();
}
There are some other parts in the program, like where images are loaded, but I think it will be to much information when I mention everything here.
It took me some time, but the cause of the problem was in the hardware. I was running on a virtual machine (VMware) and although the specifications said that it supported OpenGL up to 2.1 I found out that it doesn't support OpenGL at all.
My solution was to take an old machine, install Windows on it, and copy all files. It compiled and runs as smooth as a can.
If anyone else runs into the same problem I can advise to get it working on a native installation before virtualising. It can safe you a lot of time.

GTKmm3 (GTK+ 3 C++) compiles but throws GLib-GIO-CRITICAL error when run

I've just started trying to get GTK+ 3 working with C++ and have tried to compile the simplest GTKmm Hello-World example.
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(200, 200);
return app->run(window);
}
After compiling with:
g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
There are no errors, however when I run the newly compiled application with:
./simple
I get the following error:
GLib-GIO-CRITICAL **: g_application_list_actions: assertion `application->priv->is_registered' failed
Segmentation fault (core dumped)
The code has been copied, unchanged from https://developer.gnome.org/gtkmm-tutorial/stable/sec-basics-simple-example.html.en so presumably it's likely to be a configuration issue, however I've frequently used this machine for PyGtk and CommandLine C++ without any issues.
Any help would be greatly appreciated.
This was a bug in gtkmm (probably triggered by a change in GTK+) that has been fixed in gtkmm:
https://git.gnome.org/browse/gtkmm/commit/?id=506cba376c0a0e09217ad7f5d597b6723d7f91a6
Maybe you can update your gtkmm version.

compiled program cannot find freeglut.dll

I'm new to this site, and relatively new to programming.
I've been doing some C++ programming for a while using Visual Studio 2010, and I wanted to get into OpenGL, so I bought the OpenGL Superbible to get started. I've gotten stuck on the second chapter's "simple" project.
After hours of research I've been able to download all the necessary files to use freeGLUT and GLtools. I've made sure that everything is in the right place for the program to work.
Now, it appears as though everything has been worked out... except for one odd problem.
I was instructed that I needed to place freeglut.dll into Windows\System32, so I did. The project will build now, but when I go to run it, it tells me
"The program can't start because freeglut.dll is missing from your computer. Try
reinstalling the program to fix this problem."
Now, I am certain that freeglut.dll is in Windows\System32 as it should be, so what's the problem? how do I solve it?
Here's the original code from the book:
#include "../../shared/gltools.h" //OpenGL toolkit
//////////////////////////////////////////////
//called to draw scene
void RenderScene(void)
{
// clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//Flush drawing commands
glFlush();
}
////////////////////////////////////////////////////////
//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f , 0.0f, 1.0f, 1.0f );
}
///////////////////////////////////////////////////////////
//main program entry point
void main(void)
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
This is the code that actually compiled, but would not run (it's a bit of a mess from all the conflicting data I got from different resources):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GLTools.h>
#include <gl/GLUT.h>
//called to draw scene
void RenderScene(void)
{
// clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f , 0.0f, 1.0f, 1.0f );
}
//void main(void)
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
Try putting the DLL in the same folder as your exe file. The advice to put it in Windows\System32 predates a lot of newer Windows security restrictions.
#ScottMcP-MVP 's answer solved my problem, but I thought I'd add some detail that doesn't really fit in the comment.
My solution:
Add the following subdirectory structure to your solution folder:
In x86, put the 32-bit versions of GLut, GLew and anything else you need.
In x64, put the 64-bit versions of same.
I went ahead and put all .dll, .lib, and .h in the corresponding folders here rather than placing them in the Windows SDK (Or, in Win7+, the "Windows Kits" folder) to ensure that my projects in SVN had the correct version, and checking out on another machine would retrieve all dependencies. This required adding the include and target-specific lib folders to the project properties:
Set your Include Directories field to
$(SolutionDir)ThirdParty\Include\;$(IncludePath)
And your Library Directories field to
$(SolutionDir)\ThirdParty\$(PlatformTarget)\lib\;$(LibraryPath)
Note that all of these should be applied to the "All Platforms" build configuration. The $(PlatformTarget) macro will make sure that the correct lib's and dll's are used. The include folder is target-agnostic, so I've placed it in the root of my ThirdParty folder.
To get the required files into your output folder, add the following post-build event to your project configuration (under "All platforms"):
xcopy $(SolutionDir)ThirdParty\$(PlatformTarget)\*.dll $(OutputPath) /Y
That will copy the correct version of the DLLs to your output folder on build. This keeps you from having to manually put the DLLs in our output folder, and is more compatible with source control where you typically don't want to include your output, bin or debug folders.
Once I had all of this configured, I created a VC OpenGL project template since getting everything configured took 30 minutes of my life I'd rather have back.