GLFW functions not found

1.3k Views Asked by At

I try to compile a program that uses GLFW3 library on Ubuntu 16.04 x86_64. I installed libglfw3 and libglfw3-dev packages. Next, I wrote CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Test)
set (CMAKE_CXX_FLAGS "-lGL -lGLEW")
set (CMAKE_EXE_LINKER_FLAGS -lglfw )
add_executable(Test src/main.cpp)

And main.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main() {

    if (!glfwInit()) {
        return -1;
    }

    glfwTerminate();

    return 0;
}

But I get an error from make command:

main.cpp:(.text+0x5): undefined reference to `glfwInit'
main.cpp:(.text+0x1a): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
CMakeFiles/Test.dir/build.make:94: recipe for target 'Test' failed
make[2]: *** [Test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Test.dir/all' failed
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

What do I doing wrong? Why can it not find GLFW3?

1

There are 1 best solutions below

1
On BEST ANSWER

Native way for link with library in CMake is target_link_libraries:

cmake_minimum_required (VERSION 2.6)
project (Test)
add_executable(Test src/main.cpp)
target_link_libraries(Test GL GLEW glfw)

Note, that this works only if GL and other libs are installed to the default places, known to compiler and linker. Otherwise it is better to use find_package(GLEW) and other find_package() calls, as noted by @tambre.