【问题标题】:GLFW3 library in CLion with CMake causes undefined symbols on MacOS带有 CMake 的 CLion 中的 GLFW3 库会导致 MacOS 上出现未定义的符号
【发布时间】:2017-12-08 16:15:55
【问题描述】:

我在 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)

【问题讨论】:

    标签: c++ macos cmake glfw


    【解决方案1】:

    我将此添加到我的 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)
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 2020-12-10
      • 2014-03-19
      • 2016-08-21
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多