2011-07-31 24 views
6

tengo este código simple:banderas cmake para OpenGL usando Glew y GLFW

#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; 
} 

y en mi CMakeLists.txt:

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

corriendo "cmake". no se produce ningún error, pero con make voluntad dice:

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

mientras se ejecuta:

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

compila correctamente el código sin error. ¿Cómo hago que cmake se ejecute con mi código?

también, si puedo añadir estas líneas a la función principal:

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); 

incluso corriendo gcc con las mismas banderas producirá 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) 

estoy corriendo menta Linux basada en Kubuntu 10.04, cmake v2.8, libglfw-dev, libglfw2, libglew1.5, libglew1.5-dev, glew-utils.

Soy nuevo en cmake, glew y glfw. ¡Gracias por su ayuda chicos!

¡salud!

P

Respuesta

3

Para ver los comandos que el CMake genera makefile está ejecutando, ejecute make como:

make VERBOSE=1 

Al ver los comandos es muy útil cuando se depura proyectos CRealice. Para el ejemplo proporcionado, los siguientes comandos se ejecutan:

/usr/bin/gcc -std=c99 -lGL -lGLU -lGLEW -lglfw -o CMakeFiles/test.dir/test.c.o -c test.c 
/usr/bin/gcc CMakeFiles/test.dir/test.o -o test -rdynamic 

Los makefiles generados por CMake se compilan cada archivo fuente en un fichero objeto (esto es lo que hace gcc -c) de forma individual, y luego enlaza todos los archivos del objeto junto con un comando separado. Con el ejemplo proporcionado, las bibliotecas relacionadas con OpenGL se están especificando durante la etapa de compilación, y no durante la etapa de enlace. En lugar de especificar las bibliotecas con add_definitions, se debe usar el comando target_link_libraries.

archivo

Un CMakeLists.txt como esto debería funcionar:

cmake_minimum_required(VERSION 2.8) 
project(test C) 
add_definitions(-std=c99) 
set(SRC test.c) 
add_executable(test ${SRC}) 
target_link_libraries(test GL GLU GLEW glfw) 

El -l prefijo no tiene que ser especificado para las bibliotecas porque target_link_libraries añadirá automáticamente el prefijo -l para Entornos UNIX/Linux y la extensión .lib para entornos Windows. Más información sobre target_link_libraries se puede encontrar en http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

+0

gracias por la explicación. le dará una oportunidad – pixelblender

4

Puede ver aquí un ejemplo de cómo uso cmake con glfw. http://code.google.com/p/assembly3d/source/browse/tools/viewer/CMakeLists.txt

utilizo FindGLFW.cmake para encontrar GLFW http://code.google.com/p/assembly3d/source/browse/tools/viewer/cmake_modules/FindGLFW.cmake

Además, la versión GLFW en Ubuntu es 2.6. GLFW_OPENGL_VERSION_MINOR y GLFW_OPENGL_VERSION_MAJOR solo funcionan en glfw 2.7 y creo que OpenGL 3.x funciona solo con glfw 2.7.

Mejor

+0

gracias por los enlaces. ¡aclamaciones! – pixelblender