Search code examples
c++macoscmakeclionglfw

How to correctly setup cmake for glfw on clion on MacOS


I've recently bought a Macbook and I want to start writing some OpenGL code for a project.

SPECIFICATIONS

  • MacOS
  • Macbook pro 16inch 2019
  • Clion
  • glfw3 (precompiled binaries 64 bit)

The CMake doesn't complain, but as soon as I start compiling I get the following error

    /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/arnesix/Documents/dev/c++/graphics/cmake-build-debug --target graphics -- -j 12
[ 50%] Linking CXX executable graphics
Undefined symbols for architecture x86_64:
  "_CFArrayAppendValue", referenced from:
      __glfwInitJoysticksNS in libglfw3.a(cocoa_joystick.m.o)
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayCreateMutable", referenced from:
      __glfwInitJoysticksNS in libglfw3.a(cocoa_joystick.m.o)
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayGetCount", referenced from:
      __glfwSetVideoModeNS in libglfw3.a(cocoa_monitor.m.o)
      __glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
      _closeJoystick in libglfw3.a(cocoa_joystick.m.o)
      __glfwPlatformPollJoystick in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayGetValueAtIndex", referenced from:
      __glfwSetVideoModeNS in libglfw3.a(cocoa_monitor.m.o)
      __glfwPlatformGetVideoModes in libglfw3.a(cocoa_monitor.m.o)
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
      _closeJoystick in libglfw3.a(cocoa_joystick.m.o)
      __glfwPlatformPollJoystick in libglfw3.a(cocoa_joystick.m.o)
  "_CFArraySortValues", referenced from:
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)

The binaries I have downloaded from https://www.glfw.org/download.html only contain the 64 bit binaries.

I am using the following cmake code and folder structure enter image description here

#include <iostream>
#include <glfw3.h>

int main() {
    std::cout << glfwInit() << std::endl;
    return 0;
}

and lastly, my Cmake preferences

enter image description here

I would love to know why this keeps happening and how I should resolve this issue.


Solution

  • Did you read the documentation? You're missing three frameworks if you compile like that:

    target_link_libraries(graphics glfw3 -framework Cocoa -framework OpenGL -framework IOKit)
    

    Better to just use the find_package approach