c++ - 如何在mac上使用cmake链接OpenGL相关库?

标签 c++ macos cmake glfw opengl-3

我正在尝试使用 cmake 在 mac 上完成一个简单的 GLFW 教程,其中我遇到了一系列 undefined symbol 链接错误。我对这个问题进行了研究,但没有找到任何帮助。下面是我的 CMakeLists.txt。

cmake_minimum_required(VERSION 3.3)
project(GL_Template)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})

find_package(GLM REQUIRED)
find_package(GLEW REQUIRED STATIC)

include_directories(${GLM_INCLUDE_DIR})
include_directories(/usr/local/include)
include_directories(./include)
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(IOKIT_LIBRARY IOKit REQUIRED)
find_library(COREVID_LIBRARY CoreVideo REQUIRED)
message(${COCOA_LIBRARY})
message(${IOKIT_LIBRARY})
message(${COREVID_LIBRARY})


file(GLOB A_SOURCE ./src/*.cpp)

add_executable(Hello example/main.cpp ${A_SOURCE})
target_link_libraries(Hello ${GLEW_LIBRARY} ${GLFW3_LIBRARIES})
target_link_libraries(Hello ${COCOA_LIBRARY} ${COREVID_LIBRARY} ${IOKIT_LIBRARY})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")

任何帮助都会有帮助。 下面是错误信息

Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      _main in main.cpp.o
  "_glfwGetTime", referenced from:
      _main in main.cpp.o
  "_glfwInit", referenced from:
      _main in main.cpp.o
  "_glfwMakeContextCurrent", referenced from:
      _main in main.cpp.o
  "_glfwPollEvents", referenced from:
      _main in main.cpp.o
  "_glfwSetCursorPosCallback", referenced from:
      _main in main.cpp.o
  "_glfwSetInputMode", referenced from:
      _main in main.cpp.o
  "_glfwSetKeyCallback", referenced from:
      _main in main.cpp.o
  "_glfwSetScrollCallback", referenced from:
      _main in main.cpp.o
  "_glfwSetWindowShouldClose", referenced from:
      key_callback(GLFWwindow*, int, int, int, int) in main.cpp.o
  "_glfwSwapBuffers", referenced from:
      _main in main.cpp.o
  "_glfwTerminate", referenced from:
      _main in main.cpp.o
  "_glfwWindowHint", referenced from:
      _main in main.cpp.o
  "_glfwWindowShouldClose", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Hello] Error 1
make[1]: *** [CMakeFiles/Hello.dir/all] Error 2
make: *** [all] Error 2

最佳答案

我找到了解决方案。显然GLFW3_LIBRARY没有定义,定义的变量应该是GLFW_LIBRARY。

关于c++ - 如何在mac上使用cmake链接OpenGL相关库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559886/

相关文章:

c++ - 函数模板签名中忽略了成员 typedef 的访问说明符

c++ - 当 SWIG 包含命名空间时,如何在 SWIG 中包含 C++ header ?

linux - 你能帮我使用 smbclient 从 Ubuntu 访问 Mac SMB 共享吗? (NT_STATUS_ACCESS_DENIED 错误)

c++ - 为什么 link_libraries(stdc++fs) 有效但 -lstdc++fs 无效?

c++ - 如何使用gnu autotool构建qt项目

c++ - 如何有条件地为模板头设置编译器优化

python - Pipenv 安装运行时错误 : location not created nor specified

macos - 您的 Mac 运行的 OS X 版本低于项目的最低部署目标

cmake - Qt5.5:如何将 Qt 的 qtpcre.lib 和 qtharfbuzzng.lib 包含在 CMake 中?

c++ - 使用cmake构建静态库的静态库