使用GLEW和GLFW的OpenGL的CMake标志

7

我有这段简单的代码:

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

而在我的CmakeLists.txt文件中:

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

运行 "cmake ." 没有产生任何错误,但运行 make 会报错:
test.c:(.text+0x10): undefined reference to `glfwInit'
collect2: ld returned 1 exit status
make[2]: *** [tut1] Error 1

运行时:

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

成功编译代码且无错误。我该如何让 CMake 运行我的代码?
此外,如果我将以下这些行添加到主函数:
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 );

即使使用相同的标志运行gcc也会产生错误:

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)

我正在运行基于Kubuntu 10.04的Linux Mint,使用CMake v2.8、libglfw-dev、libglfw2、libglew1.5、libglew1.5-dev和glew-utils。
我对CMake、GLEW和GLFW还不熟悉。感谢你们的帮助!
祝好!
3个回答

4

3
要查看CMake生成的makefile执行的命令,请运行以下命令:

make

make VERBOSE=1

在调试CMake项目时,查看命令非常有帮助。对于所提供的示例,执行以下命令:

/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

由CMake生成的makefile将每个源文件单独编译成目标文件(这就是"gcc -c"所做的),然后使用单独的命令将所有的目标文件链接在一起。在提供的示例中,OpenGL相关的库在编译阶段进行了指定,而不是在链接阶段。应该使用"target_link_libraries"命令来指定库,而不是使用"add_definitions"来指定库。像这样的CMakeLists.txt文件应该可以工作:
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)

在库中,不需要指定-l前缀,因为在UNIX/Linux环境下,target_link_libraries会自动添加-l前缀,在Windows环境下添加.lib扩展名。有关target_link_libraries的更多信息,请访问http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接