如何使用 cmake 在 OSX 上编译 glfw

How to compile glfw on OSX with cmake

我正在使用 jetbrains 的 CLion IDE 来编译一个小型的 hello world 应用程序。

这是我在尝试编译时遇到的命令和错误的完整日志:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/brian/Library/Caches/CLion12/cmake/generated/ac340afb/ac340afb/Debug --target Dunjun -- -j 4
[ 50%] Building CXX object CMakeFiles/Dunjun.dir/src/main.cpp.o
clang: warning: -framework Cocoa: 'linker' input unused
clang: warning: -framework OpenGL: 'linker' input unused
clang: warning: -framework IOKit: 'linker' input unused
[100%] Linking CXX executable Dunjun
Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      _main in main.cpp.o
  "_glfwInit", referenced from:
      _main in main.cpp.o
  "_glfwMakeContextCurrent", referenced from:
      _main in main.cpp.o
  "_glfwPollEvents", referenced from:
      _main in main.cpp.o
  "_glfwSwapBuffers", referenced from:
      _main in main.cpp.o
  "_glfwTerminate", referenced from:
      _main in main.cpp.o
  "_glfwWindowShouldClose", referenced from:
      _main in main.cpp.o
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]: *** [Dunjun] Error 1
make[2]: *** [CMakeFiles/Dunjun.dir/all] Error 2
make[1]: *** [CMakeFiles/Dunjun.dir/rule] Error 2
make: *** [Dunjun] Error 2

我意识到要防止这些错误,我必须向编译器添加标志。其他 Whosebug 问题将此引用为正确的命令:

gcc -Iglfw3/include/ -Lglfw3/lib/ -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo main.c

我如何通过 Cmake 做到这一点?我怎样才能让它在 CLion 下工作。

下面是我的CmakeLists.txt供参考:

cmake_minimum_required(VERSION 3.3)
project(Dunjun)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -framework Cocoa -framework OpenGL -framework IOKit")
SET( CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS}" )

set(SOURCE_FILES
    bin/Debug/Dunjun_game
    include/Dunjun/Common.hpp
    src/main.cpp
    CMakeLists.txt
    README.md)

add_executable(Dunjun ${SOURCE_FILES})
include_directories(include)

-framework 标志添加到链接器标志,而不是编译标志。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")