Brian Yeh
Brian Yeh

Reputation: 3267

How to compile glfw on OSX with cmake

I'm using CLion IDE from jetbrains to compile a small hello world application.

This is the full log of the commands and errors I get when attempting to compile:

/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

I realize that to prevent these errors I have to add flags to the compiler. Other stackoverflow questions refrence this as the correct command:

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

How would I do this through Cmake? How can I get this to work under CLion.

Below is my CmakeLists.txt for reference:

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)

Upvotes: 2

Views: 2271

Answers (1)

dragn
dragn

Reputation: 1050

Add -framework flags to linker flags, not compile flags.

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

Upvotes: 3

Related Questions