You are not logged in.
I've recently switched over to Arch (and love it) from Windows 7, and am trying to start developing in OpenGL using C++. I've used Eclipse a lot on the Windows side for Java development, but I'm having trouble referencing the OpenGL libraries in the C++ stuff.
My code is as follows:
#include <GL/gl.h>
#include <GL/freeglut.h>
int main(int argc, char **argv) {
glutInit(&argc, argv);
return 0;
}
I don't believe the problem's there because the code won't even begin to compile.
I tried to find and select the libraries I need in the project properties, but I really have no idea what I'm doing in there. In the current state of things, this is my console output:
**** Build of configuration Debug for project CGame ****
make all
Building target: CGame
Invoking: Cross G++ Linker
g++ -L/usr/include/GL -o "CGame" ./main.o -l/usr/include/GL
/usr/bin/ld: cannot find -l/usr/include/GL
collect2: error: ld returned 1 exit status
make: *** [CGame] Error 1
**** Build Finished ****
This is what some parts of Eclipse's configuration look like:
Can anybody lend me a hand? I've been searching around on the Internet for a little while now, but I'm getting tired of all the shitty information there is. Many thanks if you can point out how to include the OpenGL/freeglut libraries in my project. If you want to see how my Eclipse stuff is configured, I can show you over Skype or TeamViewer.
Last edited by BLACKwave (2012-04-04 21:37:47)
Offline
Short version:
- Delete the entries from "include paths" and "include folders" - GCC searches /usr/include by default, so if you #include <GL/gl.h>, then it will find /usr/include/GL/gl.h and freeglut.h
- Same for the libraries - no need for the library search path as /usr/lib is searched by default, and in the "libraries (-l)" you need to add "GL" and "freeglut"
Long version:
It's often useful to look at what the command line arguments to g++ look like.
g++ -L/usr/include/GL -o "CGame" ./main.o -l/usr/include/GL
Some of the options that are really useful to know are:
- The -L (upper case L) option specifies the library search path - the directory in which gcc searches for libraries (you can add as many of these as you wish).
- The -l (lower case L) option specifies the name of the library to be linked (can add more than one here - e.g. "-l GL -l freeglut", etc).
- The -I (upper case I) option specifies the header search path
- The -o option is the name of the output file
So, as you can see, the above g++ command attempts to link your executable with a folder (-l /usr/include/GL), which you have specified in the "Libraries (-l)" window, and obviously this does not work.
Alternatively, you can just play around with the command line to get familiar with the compiler. You can compile your code from the command line like this:
g++ main.cpp -o main -l GL -l freeglut
Often it is easier to just start with a simple text editor, and compile your code using the command line. That will enable you to identify these kind of problems easier, as Eclipse is really just a glorified tool on top of all this.
And welcome to ArchLinux! :-)
Last edited by krigun (2012-04-04 23:05:06)
Offline
Do you know how I can specify the command line command directly in Eclipse? One of the reasons I came to Arch is for the CLI stuff. I don't like having the reality of the computer abstracted from me. I like the code editor and intellisense that Eclipse provides, though.
Offline
Do you know how I can specify the command line command directly in Eclipse? One of the reasons I came to Arch is for the CLI stuff. I don't like having the reality of the computer abstracted from me. I like the code editor and intellisense that Eclipse provides, though.
Well, if you want to use Eclipse, then you don't _have_ to use the CLI of course. Do whatever you find best, but unfortunately I do not know if eclipse has an integrated console of any kind.
Offline
See the "Cross G++ Linker" -> "Libraries" -> "Libraries (-l)" area? Just add "GL" and "freeglut" there, and remove the contents of the other windows, and it should compile and run.
Offline
I tried directly from terminal:
g++ main.cpp -o main -l GL -l freeglut
/usr/bin/ld: cannot find -lfreeglut
collect2: error: ld returned 1 exit status
Not sure what's up.
By the way, thanks a lot for your help!!
Last edited by BLACKwave (2012-04-04 23:21:12)
Offline
Oh, sorry - my bad. Try:
g++ main.cpp -o main -l GL -l glut
freeglut is the name of the package. You can see the name of the libraries by looking in the /usr/lib folder, e.g. /usr/lib/libglut.so means you need to supply "-l glut". The libraries are searched by "lib{name}.so" or "lib{name}.a". So if you want to link with "libmylibrary.so", then you supply the option "-l mylibrary"
Last edited by krigun (2012-04-04 23:30:08)
Offline
filler
Offline
Got it working? Great!
Personally, I use the GLFW window library for OpenGL coding. You can get it by:
pacman -S glfw
triangle.cpp
//========================================================================
// This is a small test application for GLFW.
// The program opens a window (640x480), and renders a spinning colored
// triangle (it is controlled with both the GLFW timer and the mouse).
//========================================================================
#include <stdio.h>
#include <stdlib.h>
#include <GL/glfw.h>
int main( void )
{
int width, height, x;
double t;
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
exit( EXIT_FAILURE );
}
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
exit( EXIT_FAILURE );
}
glfwSetWindowTitle( "Spinning Triangle" );
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Enable vertical sync (on cards that support it)
glfwSwapInterval( 1 );
do
{
t = glfwGetTime();
glfwGetMousePos( &x, NULL );
// Get window size (may be different than the requested size)
glfwGetWindowSize( &width, &height );
// Special case: avoid division by zero below
height = height > 0 ? height : 1;
glViewport( 0, 0, width, height );
// Clear color buffer to black
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
// Select and setup the projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );
// Select and setup the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.0f, 1.0f, 0.0f, // Eye-position
0.0f, 20.0f, 0.0f, // View-point
0.0f, 0.0f, 1.0f ); // Up-vector
// Draw a rotating colorful triangle
glTranslatef( 0.0f, 14.0f, 0.0f );
glRotatef( 0.3f*(GLfloat)x + (GLfloat)t*100.0f, 0.0f, 0.0f, 1.0f );
glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( -5.0f, 0.0f, -4.0f );
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 5.0f, 0.0f, -4.0f );
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.0f, 0.0f, 6.0f );
glEnd();
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Close OpenGL window and terminate GLFW
glfwTerminate();
exit( EXIT_SUCCESS );
}
Then you can compile the code above by:
g++ triangle.cpp -o triangle -l GLFW -l GL
It just draws a rotating triangle, but should get you started on something more exiting.
Last edited by krigun (2012-04-04 23:39:45)
Offline
Bizarre, almost immediately after I got it working I said to myself, "Wow, GLUT is kind of lame." I proceeded to search around the Internet, and settled on GLFW as a replacement. I was in the process of learning about it when I came back here and saw that you suggested it less than ten minutes after my reply. o_o
Should've just came here. I do have a problem with GLFW, though. When I run the code my whole screen turns black for a little while, then my old stuff comes back but with an 800x600 resolution and the screen is frozen. I can sometimes switch to another tty and restart X.
When I go to the terminal with output from X and my window/desktop manager (gnome shell) I see some stuff like "meta_window_move_resize: assertion `!window->override_redirect` failed" and "meta_window_activate cld by a pager with a 0 timestamp." If I restart my computer, Arch says "clearing orphaned i-node" a ton of times.
Any idea what's causing this?
EDIT: Just kidding. I thought I was running your code but was still running my crappy code. The problem stemmed from not calling glfwSwapBuffers().
Last edited by BLACKwave (2012-04-05 02:55:29)
Offline
krigun
@krigun
(Don't know how/if that works in this forum)
Thought I'd just ask here because I'm not finding anything on Google. Do you know how to make it so that the program terminates when the 'X' button is clicked? In LWJGL you would check Display.isCloseRequested(). Not sure how to do it here.
Offline
Good you got it working!
The GLFW project actually has very good documentation. Check out the users guide, and reference manual from here. It's surprisingly well documented.
I found this from the users guide:
bool running;
// Main loop
while( running )
{
// OpenGL rendering goes here...
glClear( GL_COLOR_BUFFER_BIT );
// Swap front and back rendering buffers
glfwSwapBuffers();
// Check if ESC key was pressed or window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
}
Offline
Thanks- I actually had to set a window close callback function, but I got it working.
C++ can be pretty frustrating, man.
Last edited by BLACKwave (2012-04-05 19:24:26)
Offline
Yup, it can be, but it's still a great language despite its flaws.
Btw, if you find out that you don't like Eclipse, give Qt Creator a shot. It's a great IDE with a good debugger, and works great on Arch.
Offline
Yup, it can be, but it's still a great language despite its flaws.
Btw, if you find out that you don't like Eclipse, give Qt Creator a shot. It's a great IDE with a good debugger, and works great on Arch.
Thanks, it's nicer (lighter) than Eclipse. Every IDE is good and bad in its own ways. NetBeans has a simple interface but crashes unexpectedly, IntelliJ has awesome intellisense + text editor but has a crappy interface, Eclipse... is widely supported but poorly organized, etc. This one is pretty nice.
Reviewing MIT's OCW Intro to C++ course before I continue with OpenGL.
Last edited by BLACKwave (2012-04-08 00:35:06)
Offline
No problem. Also, if you ever want to develop Android applications using the NDK with the SDK in Qt Creator, you can do it. Look up "necessitas qt".
Offline
I'll keep that in mind. I want an Android device- just waiting until my crappy phone breaks down.
Offline
...if you ever want to develop Android applications using the NDK with the SDK in Qt Creator, you can do it. Look up "necessitas qt".
Nice. I have been brushing up on my Java playing with Android apps -- I prefer C++. I will be trying that.
Last edited by ewaller (2012-04-08 05:43:31)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline