Cmake, glfw, and glew

pixelblender3d wrote on Sunday, July 31, 2011:

Hi,

I have this simple code:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
int main(int argc, char const* argv[] )
{
    if( !glfwInit() ){
        fprintf( stderr, "failed\n" );
    }
    return 0;
}

and in my CmakeLists.txt:

PROJECT(test C)
find_package(OpenGL)
ADD_DEFINITIONS(
    -std=c99
    -lGL
    -lGLU
    -lGLEW
    -lglfw
)
SET(SRC test)
ADD_EXECUTABLE(test ${SRC})

running “cmake .” doesn’t produce any error, but running make will says:

test.c:(.text+0x10): undefined reference to `glfwInit'
collect2: ld returned 1 exit status
make[2]: *** [tut1] Error 1

while running:

gcc -o test test.c -std=c99 -lGL -lGLU -lGLEW -lglfw

successfully compiles the code without error. How do i make cmake run with my
code?

also, if i add these lines to the main function:

glfwOpenWindowHint( GLFW_FSAA_SAMPLES, 4 );
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 1 );
glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

even running gcc with the same flags will produce error:

test.c: In function ‘main’:
test.c:14: error: ‘GLFW_OPENGL_VERSION_MAJOR’ undeclared (first use in this function)
test.c:14: error: (Each undeclared identifier is reported only once
test.c:14: error: for each function it appears in.)
test.c:15: error: ‘GLFW_OPENGL_VERSION_MINOR’ undeclared (first use in this function)
test.c:16: error: ‘GLFW_OPENGL_PROFILE’ undeclared (first use in this function)
test.c:16: error: ‘GLFW_OPENGL_CORE_PROFILE’ undeclared (first use in this function)

i’m running linux mint based on kubuntu 10.04, cmake v2.8, libglfw-dev,
libglfw2, libglew1.5, libglew1.5-dev, glew-utils.

Please help. I’m new to cmake, glew and glfw. Thanks for your help guys!

cheers!

P

elmindreda wrote on Sunday, July 31, 2011:

You seem to be attempting to use the GLFW 2.7 API using an older version of
the GLFW header.

The remaining issues stem from misuse of CMake. You should read the
documentation for CMake and/or use a forum for users of CMake. That said, here
are some pointers:

The add_definitions command is for compiler flags, not linker flags. Read
about target_link_libraries.

If you’re going to use the OpenGL CMake package, you should use the variables
it provides.

pixelblender3d wrote on Sunday, July 31, 2011:

thanks elmindreda!

i will look for more info on target_link_libraries.

cheers!