未找到 GLFW 函数

GLFW functions not found

我尝试在 Ubuntu 16.04 x86_64 上编译一个使用 GLFW3 库的程序。我安装了 libglfw3libglfw3-dev 包。接下来,我写了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)

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

但是我从 make 命令中得到一个错误:

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

我做错了什么?为什么找不到GLFW3?

link 在 CMake 中使用库的原生方式是 target_link_libraries:

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

注意,这只有在 GL 和其他库安装到编译器和 linker 已知的默认位置时才有效。否则,最好使用 find_package(GLEW) 和其他 find_package() 调用,如@tambre所述。