简体   繁体   English

如何在 MacOS 上的 clion 上为 glfw 正确设置 cmake

[英]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.我最近买了一台 Macbook,我想开始为一个项目编写一些 OpenGL 代码。

SPECIFICATIONS规格

  • MacOS苹果系统
  • Macbook pro 16inch 2019 Macbook pro 16 英寸 2019
  • Clion克里昂
  • glfw3 (precompiled binaries 64 bit) glfw3(预编译二进制文件 64 位)

The CMake doesn't complain, but as soon as I start compiling I get the following error CMake 没有抱怨,但是一旦我开始编译,我就会收到以下错误

    /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.我从https://www.glfw.org/download.html下载的二进制文件只包含 64 位二进制文​​件。

I am using the following cmake code and folder structure我正在使用以下 cmake 代码和文件夹结构在此处输入图片说明

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

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

and lastly, my Cmake preferences最后,我的 Cmake 偏好

在此处输入图片说明

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最好只使用find_package 方法

I had exactly the same problem.我遇到了完全相同的问题。
I'm using local (not system-wide) glfw with glad .我使用的是本地(不是系统范围内) glfwglad
Here is my cmake file I've ended up with which worked for me.这是我最终得到的 cmake 文件,它对我有用。

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") set(CMAKE_CXX_FLAGS "-framework Cocoa -framework IOKit")实际帮助我的行set(CMAKE_CXX_FLAGS "-framework Cocoa -framework IOKit")

GL_SILENCE_DEPRECATION part seems to have no impact, so probably can be ignored. GL_SILENCE_DEPRECATION部分似乎没有影响,所以可能可以忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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