glfw-3.3.5 - 架构 arm64 的未定义符号

glfw-3.3.5 - Undefined symbols for architecture arm64

链接了 cmake 中的所有库并编写了使用 GLFW 打开 window 的代码,运行 使用 make 命令构建 returns 这样的输出:

[main] Building folder: game 
[build] Starting build
[proc] Executing command: /opt/homebrew/bin/cmake --build /Users/matt/projects/game/build --config Debug --target all -j 10 --
[build] Consolidate compiler generated dependencies of target game
[build] [ 50%] Building CXX object CMakeFiles/game.dir/main.cpp.o
[build] [100%] Linking CXX executable game
[build] Undefined symbols for architecture arm64:
[build]   "_glClear", referenced from:
[build]       _main in main.cpp.o
[build]   "_glewExperimental", referenced from:
[build]       _main in main.cpp.o
[build]   "_glewInit", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwCreateWindow", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwGetKey", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwInit", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwMakeContextCurrent", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwPollEvents", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwSetInputMode", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwSwapBuffers", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwTerminate", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwWindowHint", referenced from:
[build]       _main in main.cpp.o
[build]   "_glfwWindowShouldClose", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture arm64
[build] collect2: error: ld returned 1 exit status
[build] make[2]: *** [game] Error 1
[build] make[1]: *** [CMakeFiles/game.dir/all] Error 2
[build] make: *** [all] Error 2

需要注意的是,它 仅通过导入成功构建 ,而不是负责打开 GLFW 的代码 window,因此它不会似乎是链接器的问题。

我的 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 3.12)
project(Game VERSION 1.0.0)
include_directories(${PROJECT_SOURCE_DIR}/deps/glfw-3.3.5/include/GLFW)
#---------------Configure dependency directories--------------------------------------------------------------
SET (PROJECT_DEPS "${PROJECT_SOURCE_DIR}/deps")
SET (glew_inc "${PROJECT_DEPS}/glew-2.1.0/include/GL/")
SET (glew_src "${PROJECT_DEPS}/glew-2.1.0/src/")
SET (glfw_inc "${PROJECT_DEPS}/glfw-3.3.5/include/GLFW/")
SET (glfw_src "${PROJECT_DEPS}/glfw-3.3.5/src/")
SET (glm "${PROJECT_DEPS}/glm/glm/")
#---------------Configure libraries----------------------------------------------------------------------------
include_directories(
    ${PROJECT_SOURCE_DIR}
    ${PROJECT_BINARY_DIR}
    ${glm}
    ${glew_inc}
    ${glew_src}
    ${glfw_inc}
    ${glfw_src}
    ${PROJECT_SOURCE_DIR}
    )
#---------------Add executable---------------------------------------------------------------------------------
add_executable(game ${PROJECT_SOURCE_DIR}/main.cpp)
link_libraries(Game PRIVATE glfw-3.3.5)
link_libraries(Game PRIVATE glew-2.1.0)
link_libraries(Game PRIVATE glm)

我的 main.cpp 项目根文件,从中构建可执行文件如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <glew.h>
#include <glfw3native.h>
#include <glfw3.h>
#include <glm.hpp>

int main () {
  glewExperimental = true; // Needed for core profile
  if (!glfwInit()) {
      fprintf(stderr, "Failed to initialize GLFW\n");
      return -1;
  }

  glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL 

  GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
  window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
  if( window == NULL ){
      fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
      glfwTerminate();
      return -1;
  }
  glfwMakeContextCurrent(window); // Initialize GLEW
  glewExperimental=true; // Needed in core profile
  if (glewInit() != GLEW_OK) {
      fprintf(stderr, "Failed to initialize GLEW\n");
      return -1;
  }

  glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

  do {
      glClear( GL_COLOR_BUFFER_BIT );
      glfwSwapBuffers(window);
      glfwPollEvents();

  } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);

  return 0;  
}

我使用的是配备 M1 芯片的 MacBook Pro 2020。

link_libraries 应该是 target_link_librarieslink_libraries 仅适用于其后定义的目标