1

I've read a similar answer, which solved the problem caused by different chip architecture. But it seems totally different from the problem here.

====================[ Build | OpenGLTest | Debug ]==============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/baix3xiaoruo/Code/ClionProjects/OpenGLTest --target OpenGLTest -- -j 6
Scanning dependencies of target OpenGLTest
[ 50%] Building CXX object CMakeFiles/OpenGLTest.dir/main.cpp.o
[100%] Linking CXX executable OpenGLTest
Undefined symbols for architecture arm64:
  "_glfwCreateWindow", 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
  "_glfwSetKeyCallback", referenced from:
      _main in main.cpp.o
  "_glfwSetWindowShouldClose", referenced from:
      __Z12key_callbackP10GLFWwindowiiii in main.cpp.o
  "_glfwSwapBuffers", referenced from:
      _main in main.cpp.o
  "_glfwTerminate", referenced from:
      _main in main.cpp.o
  "_glfwWindowShouldClose", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[3]: *** [OpenGLTest] Error 1
make[2]: *** [CMakeFiles/OpenGLTest.dir/all] Error 2
make[1]: *** [CMakeFiles/OpenGLTest.dir/rule] Error 2
make: *** [OpenGLTest] Error 2

user438383
  • 5,716
  • 8
  • 28
  • 43
  • What is in your CMakeLists.txt? – Yun Aug 21 '21 at 09:11
  • Well, after rechecking the Cmakelist, it seems like I've solved the problem. I made a mistake in the code—a VERY STUPID MISTAKE! – Baix3XiaoRuo Aug 21 '21 at 14:24
  • I want to post my setting here. It may help some of the people who may still struggle in mud. – Baix3XiaoRuo Aug 21 '21 at 14:26
  • ` set(CMAKE_CXX_STANDARD 14) set(GLEW_H /opt/homebrew/Cellar/glew/2.2.0_1/include/GL) set(GLFW_H /opt/homebrew/Cellar/glfw/3.3.4/include/GLFW) include_directories(${GLEW_H} ${GLFW_H}) set(GLEW_LINK /opt/homebrew/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.dylib) set(GLFW_LINK /opt/homebrew/Cellar/glfw/3.3.4/lib/libglfw.3.dylib) link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK}) add_executable(OpenGLTest main.cpp) set(SOURCE_FILES main.cpp)` if (APPLE) target_link_libraries(OpenGLTest "-framework OpenGL") target_link_libraries(OpenGLTest "-framework GLUT") endif()` – Baix3XiaoRuo Aug 21 '21 at 14:29
  • Thanks, but... could you please submit that as an answer and explain what the problem was? – Yun Aug 21 '21 at 14:32

1 Answers1

1

I'm using MacBook Air (M1, 2020) (Monterey) - with XCode updated.

I got things to work by using this CMakeLists.txt file:

cmake_minimum_required(VERSION 3.22)
project(Project)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")

find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

add_executable(Project main.cpp MainWindow.cpp MainWindow.h)
target_link_libraries(Project glfw OpenGL::GL GLEW::GLEW)

I have installed all the libraries using brew:

brew install glfw3
brew install glew

Then in your code, e.g. in the main function, you need to specify GLFW version and compatibility:

#ifdef __APPLE__
// Silence deprecation warnings on macOS
#define GL_SILENCE_DEPRECATION
#endif

//Include GLEW
#include <GL/glew.h>
//Include GLFW
#include <GLFW/glfw3.h>

if (!glfwInit()) {
    std::cerr << "ERROR: could not start GLFW3" << std::endl;
    exit(EXIT_FAILURE);
}

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

When I try to log the versions:

printf("------------\n");
printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
printf("Using GLEW %s\n", glewGetString(GLEW_VERSION));
printf("Vendor %s\n", glGetString(GL_VENDOR));
printf("Renderer %s\n", glGetString(GL_RENDERER));
printf("GLSL %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
printf("Using GLFW %i.%i.%i\n", major, minor, revision);
printf("------------\n\n");

the output is:

------------
OpenGL Version: 4.1 Metal - 76.3
Using GLEW 2.2.0
Vendor Apple
Renderer Apple M1
GLSL 4.10
Using GLFW 3.3.8
------------

I have stumbled upon many problems making this work, like the setting the glfwWindowHint flags should be called after glfwInit call, otherwise the version settings won't make a difference.

I'm no C++ expert, feel free to correct me if I'm wrong.

mtbno
  • 561
  • 7
  • 12
  • **Thanks @mtbno!!** I had been wondering in the wilderness for a couple of days before finding your answer which fixed my problem. The key seems to be using `glfw` instead of `GLFW::glfw` in `target_link_libraries(Project...)`. It looks to me like `find_package(OpenGL REQUIRED)` is **not** required, nor is the `OpenGL::GL` in `target_link_libraries(Project...)`. I assume that is all taken care of by GLEW as part of its extension wrangling. – Craig Reynolds Feb 17 '23 at 02:10