如何使用CMAKE在Mac上链接与OpenGL相关的库

How to link OpenGL related libraries on mac using cmake?

本文关键字:OpenGL 链接 CMAKE 何使用 Mac      更新时间:2023-10-16

我正在尝试使用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。