如何使用cmake在OSX上编译glfw

时间:2016-01-24 18:37:50

标签: c++ macos cmake glfw clion

我正在使用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

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

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)

1 个答案:

答案 0 :(得分:2)

-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")