How to build program with glfw compiled locally?

Recently I want to use Cmake to build a cross-platform OpenGL program with glfw supporting. I felt confused when I try to build it on Ubuntu 16.04 or Mac OS.

All I found in google suggested you to compile and install glfw first manually and then link it properly. However, I want to build the dependence locally and get the executable file easily using the follow command:

mkdir build
cd build
cmake ..
make

My CMakeLists.txt is simple:

cmake_minimum_required (VERSION 3.2)

# Set file path
# SET(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/bin/)

project(MineCube CXX)

set(CMAKE_CXX_STANDARD 11)

# OS config
if (WIN32)
    option(CMAKE_USE_WIN32_THREADS_INIT "using WIN32 threads" ON)
endif()
if(NOT WIN32)
    set(CMAKE_C_COMPILE_OBJECT g++)
    find_package(OpenGL REQUIRED)
endif()

# config glfw
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_TESTS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_DOCS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_BUILD_INSTALL OFF CACHE BOOL  "GLFW lib only" )

add_subdirectory(3rd_party/glfw)
include_directories(3rd_party/glfw/include)
LIST(APPEND LIBS glfw ${GLFW_LIBRARIES})

# config glad
set(GLAD_SRC 3rd_party/glad/src/glad.c)
if (NOT WIN32)
    LIST(APPEND LIBS dl)
endif()
include_directories(3rd_party/glad/include)

# config glm
include_directories(3rd_party/glm)

# config imgui
include_directories(3rd_party/imgui)
include_directories(3rd_party/imgui/examples/opengl3_example)
aux_source_directory(3rd_party/imgui IMGUI_SRC)
set(IMGUI_IMPL_GLFW_GL3_SRC 3rd_party/imgui/examples/opengl3_example/imgui_impl_glfw_gl3.cpp)

# include our headers
include_directories(include)

# get MineCube src files
aux_source_directory(./src MINECUBE_SRC)

set(SOURCE_FILES ${MINECUBE_SRC} ${IMGUI_SRC} ${IMGUI_IMPL_GLFW_GL3_SRC} ${GLAD_SRC})

# Compile
add_executable(MineCube ${SOURCE_FILES})

# Link
target_link_libraries(MineCube ${LIBS})

It can work on Windows 10 (Visual Studio 2015). However, on Ubuntu, it can build files rightly while meet error when I use make command:

Scanning dependencies of target glfw
[  3%] Building C object 3rd_party/glfw/src/CMakeFiles/glfw.dir/context.c.o
g++: fatal error: no input files
compilation terminated.
3rd_party/glfw/src/CMakeFiles/glfw.dir/build.make:62: recipe for target '3rd_party/glfw/src/CMakeFiles/glfw.dir/context.c.o' failed
make[2]: *** [3rd_party/glfw/src/CMakeFiles/glfw.dir/context.c.o] Error 1
CMakeFiles/Makefile2:173: recipe for target '3rd_party/glfw/src/CMakeFiles/glfw.dir/all' failed
make[1]: *** [3rd_party/glfw/src/CMakeFiles/glfw.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

It seems like it can’t find context.c.o. Do I need to modify CMakeLists.txt in glfw’s directory? Or is there sth wrong in my CMakeLists.txt?
Thx in advance.

Can you build GLFW with cmake directly on your Ubuntu system to check what that does?

You don’t need to modify the GLFW CMakeLists.txt source, I use it in a similar fashion to you, and this works on Ubuntu (see https://github.com/dougbinks/enkiTSExamples/blob/master/CMakeLists.txt ).

I’m not good enough with cmake to tell directly what your error here is, but if you start out with a simple example and work your way up you should find it.

3 Likes

I am so appreciate for your help! I will try in the next few days.