Simple tutorial for building an opengl window with glfw, and glad on linux command line.
Well, it took me 2 weeks to figure out how to compile a goddamn window that's why.
Most of the tutorials, books (at least the popular ones) I have seen, show you
either how to do it by using an ide, or come with a repository and a complex
CMakeLists.txt file that magically builds examples.
If you are new into C/C++ world and the build/compile/run operations like myself, you will soon find yourself trying IDEs advertised in the tutorial X.
I did that, and it didn't work out well in the end. I realized that I was getting more and more hesitant about build/compile process, which made me question whether I wanted to really pursue learning OpenGL, whether I should look for alternatives, etc.
But in the end, I noticed that at its base, it is a very simple process.
So I prepared this tutorial for you.
Just follow the steps and you shall have your first window. Make sure to actually read every step instead of copy pasting mindlessly. I lost a lot of time looking for magical lines.
Here is my layout. It should work for simple applications, but might fail to deal with complexities of a large application.
Here is the logic behind the layout:
- root: project's root dir. Contains the
CMakeLists.txt/bin: contains the final executable/libs: contains dynamic and static libs that will be linked to your project/include: contains the header files, like*.h , *.hpp/build: contains build files that would be generated bycmake/src: contains source files that implements/uses those that were specified in your header files
This is by far the only challenging aspect of building the application for a beginner.
Here is the contents of the projects CMakeLists.txt, I shall explain
what they do in the comments:
cmake_minimum_required (VERSION 2.8.3) # this line specifies
# the minimum version of cmake that is required to build this project
project("Hello Window OpenGL")
# specifies the name of the project
# if your name includes spaces it should be in quotes
find_package(OpenGL REQUIRED)
# If you don't know where your opengl library is
# then this would find the location of your opengl library
include_directories(
# my headers
"./include/"
)
# this includes the header files in your directory to your projects
# meaning that this helps you to write with statements like
# #include <GLFW/glfw3.h>
# #include <glad/glad.h>
# assuming that ./include directory also contains
# other subdirectories like GLFW/ and glad/
set( GLFW_SHARED_LIB
"${PROJECT_SOURCE_DIR}/libs/glfw/src/libglfw.so")
# this sets a variable to the path of the dynamic library of glfw
# Meaning that the functions of GLFW would be available at runtime
# not at compile time.
# If you have undefined reference errors try compiling
# GLFW with static library option.
# Notice that libs/ contain a sub directory glfw/
# I simply copy pasted the src/ directory that resulted from
# the building process of glfw to project_root/lib/glfw/
set ( ALL_LIBS
${OpenGL}
${GLFW_SHARED_LIB}
"-ldl"
)
# This line simply assings a new variable to the group of
# libraries that we need to link to
# -ldl is necessary I don't know why.
#
add_executable(myWin
"src/glad.c"
"src/window.cpp"
)
# This line generates the executable, the access point to your program
# basically it creates a file called myWin, that can be run as ./myWin
# Notice that in order to create the executable we need to add the glad.c
# as well
target_link_libraries(myWin ${ALL_LIBS})
# this line links the dependency libraries to our executable
# With out this line we won't be able to use <glad/glad.h> <GLFW/glfw3.h>
# etc
install(TARGETS myWin DESTINATION "${PROJECT_SOURCE_DIR}/bin/")
# this line gives an install location for the executable
# if the make command is invoked as make install
The above file should be easy to read and understand.
Let's try it:
~/projectRoot$ rm -rf build/: Deletes the build directory~/projectRoot$ mkdir build: Creates a build directorycd build: Enters into the build directorycmake ..: Builds the contents ofCMakeLists.txtof the parent directory to the current directorymake install: Creates the filemyWinand puts it into theprojectRoot/bindirectorycd projectRoot/bin: Enters into the bin directory./myWin: Launches the executable
If everything went alright, you should see a black window with Learn OpenGL as title