简体   繁体   中英

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在此处输入图片说明

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

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

and lastly, my Cmake preferences

在此处输入图片说明

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

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

I had exactly the same problem.
I'm using local (not system-wide) glfw with glad .
Here is my cmake file I've ended up with which worked for me.

cmake_minimum_required(VERSION 3.17)
project(project_name)

set(CMAKE_CXX_STANDARD 14)
if (APPLE)
    set(CMAKE_CXX_FLAGS "-framework Cocoa -framework IOKit")
    # -framework OpenGL -framework CoreVideo
    add_compile_definitions(GL_SILENCE_DEPRECATION)
endif ()

include_directories(<your include dir here>)
add_executable(project_name <your source files here>)

target_link_libraries(project_name <path to libglfw3.a>)

The line which actually helped me was set(CMAKE_CXX_FLAGS "-framework Cocoa -framework IOKit")

GL_SILENCE_DEPRECATION part seems to have no impact, so probably can be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM