Search code examples
c++cmakearm64apple-m1glew

glfw-3.3.5 - Undefined symbols for architecture arm64


Having linked all the libraries in cmake and wrtten code that'd open a window with GLFW, running build with the make command returns such output:

[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

A thing to be noted is that it builds successfully with only the imports, but not the code responsible for opening a GLFW window, so it doesn't seem to be a problem with the linker.

My CMakeLists.txt looks like this:

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)

And my main.cpp project root file, from which the executable is built looks like this:

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

I am using a MacBook Pro 2020 with a M1 chip.


Solution

  • link_libraries should be target_link_libraries. link_libraries only applies to targets defined after it