Cmgl flags for opengl using glew and 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}) 

"cmake" works. does not cause errors, but make will work:

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

during operation:

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

successfully compiles code without errors. How to make cmake make code 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 result in an 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 am running linux mint based on kubuntu 10.04, cmake v2.8, libglfw-dev, libglfw2, libglew1.5, libglew1.5-dev, glew-utils.

I am new to cmake, glew and glfw. Thanks for the help guys!

Hurrah!

R

+6
source share
3 answers

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

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

In addition, the ubuntu version of glfw is 2.6. GLFW_OPENGL_VERSION_MINOR and GLFW_OPENGL_VERSION_MAJOR only works in glfw 2.7, and I think that OpenGL 3.x only works with glfw 2.7.

The best

+4
source

To see the commands that the CMake make file created runs, run make as:

 make VERBOSE=1 

Seeing commands is very useful when debugging CMake projects. In the above example, the following commands are executed:

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

CMake's make files will compile each source file into an object file (this is what gcc -c does) separately, and then link all the object files together with a separate command. In the above example, libraries associated with OpenGL are specified at the compilation stage, and not during the link stage. Instead of specifying libraries with add_definitions , use the target_link_libraries command.

A CMakeLists.txt file such as 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 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

+3
source

Pixar has done it yet. Below is its source code, you can direct it: https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/cmake/FindGLFW.cmake

0
source

Source: https://habr.com/ru/post/893977/


All Articles