使用 glew 和 glfw 的 opengl 的 cmake 标志

发布于 2024-11-27 10:23:16 字数 1803 浏览 2 评论 0原文

我有这个简单的代码:

#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

while running:

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、cmake v2.8、libglfw-dev、libglfw2、libglew1.5、libglew1 的 linux mint .5-dev、glew-utils。

我是 cmake、glew 和 glfw 的新手。谢谢你们的帮助!

干杯!

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.

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

cheers!

P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

财迷小姐 2024-12-04 10:23:17

要查看 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 前缀,因为 target_link_libraries 将自动添加 -l 前缀适用于 UNIX/Linux 环境,.lib 扩展适用于 Windows 环境。有关 target_link_libraries 的更多信息,请访问 http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

To see the commands that the CMake generated makefile is executing, run make as:

make VERBOSE=1

Seeing the commands is very helpful when debugging CMake projects. For the example provided, the following commands are executed:

/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

The makefiles generated by CMake will compile each source file into an object file (this is what gcc -c does) individually, and then link all of the object files together with a separate command. With the provided example, the OpenGL related libraries are being specified during the compilation stage, and not during the link stage. Instead of specifying the libraries with add_definitions, the target_link_libraries command should be used.

A CMakeLists.txt file like this should work:

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)

The -l prefix does not need to be specified for the libraries because target_link_libraries will automatically add the -l prefix for UNIX/Linux environments and the .lib extension for Windows environments. More information about target_link_libraries can be found at http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

梦回梦里 2024-12-04 10:23:17

皮克斯已经做到了。下面是它的源码,大家可以参考一下:
https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/cmake /FindGLFW.cmake

Pixar has did it yet. The below is its source code, you can refer it:
https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/cmake/FindGLFW.cmake

久光 2024-12-04 10:23:16

你可以在这里看到我如何将 cmake 与 glfw 结合使用的示例。
http://code.google.com/p/ assembly3d /source/browse/tools/viewer/CMakeLists.txt

我使用 FindGLFW.cmake 来查找 glfw
http://code.google.com/p / assembly3d/source/browse/tools/viewer/cmake_modules/FindGLFW.cmake

另外,ubuntu中的glfw版本是2.6。
GLFW_OPENGL_VERSION_MINOR 和 GLFW_OPENGL_VERSION_MAJOR 仅适用于 glfw 2.7,我认为 OpenGL 3.x 仅适用于 glfw 2.7。

最好的

you can see here an example how I use cmake with glfw.
http://code.google.com/p/assembly3d/source/browse/tools/viewer/CMakeLists.txt

I use FindGLFW.cmake for finding glfw
http://code.google.com/p/assembly3d/source/browse/tools/viewer/cmake_modules/FindGLFW.cmake

Also, glfw version in ubuntu is 2.6.
GLFW_OPENGL_VERSION_MINOR and GLFW_OPENGL_VERSION_MAJOR just works in glfw 2.7 and I think OpenGL 3.x works only with glfw 2.7.

Best

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文