c++ - 带有 CMake 的 CLion 中的 GLFW3 库导致 MacOS 上出现 undefined symbol

标签 c++ macos cmake glfw

我在 MacOS 中使用 cmake 从源代码构建了 GLFW,一切顺利,示例正常运行。但是当我尝试在 clion 中使用 cmake 编译程序时,出现以下错误:

Scanning dependencies of target triangle
[ 50%] Building CXX object CMakeFiles/triangle.dir/main.cpp.o
[100%] Linking CXX executable triangle
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)
... (There are a huge bunch of errors like this, let me know if you want to read all of them)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [triangle] Error 1
make[2]: *** [CMakeFiles/triangle.dir/all] Error 2
make[1]: *** [CMakeFiles/triangle.dir/rule] Error 2
make: *** [triangle] Error 2

我浏览了互联网和文档,发现我应该使用框架参数进行编译,如下所示:

cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo

我在命令行上试过了,它起作用了。但是,如果我想在 CLion(或 CMake?)中完成工作,或者我如何在 CMake 中使用框架呢?

我的 CMakeLists.txt 以防你想知道:

cmake_minimum_required(VERSION 3.8)
project(triangle)

set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(triangle ${SOURCE_FILES})
target_link_libraries(triangle glfw3)

最佳答案

我将其添加到我的 CMakeLists.txt 中,现在可以使用了:

set(CMAKE_CXX_FLAGS "-framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo")

所以我的 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 3.8)
project(triangle)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo")
find_package(glfw3 REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(triangle ${SOURCE_FILES})

target_link_libraries(triangle glfw3)

我希望有一种更“优雅”的方式,但这现在解决了我的问题。

关于c++ - 带有 CMake 的 CLion 中的 GLFW3 库导致 MacOS 上出现 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718196/

相关文章:

c++ - 如何使用 STL 算法找到分隔字符串中两个不同字母的最短星号序列?

php - 当我在我的 Mac (OS X 10.6 Snow Leopard) 上使用本地服务器时,我在哪里保存我的 HTML 和 PHP 文件

linux - Linux 和 Android 上的 CoreFoundation Lite

cmake - pyarrow 的构建轮失败

c++ - 使特定于 Unix 的 CMake 过程跨平台

c++ - 如何检测 C++ 中的溢出?

c++ - "The procedure entry point.... could not be located"错误

CMake 与重新定位的库的链接问题

c++ - OpenCV 皮肤检测

linux - 将多个文件符号链接(symbolic link)到现有文件夹