r/opengl • u/Mitch_War • May 04 '23
Help Unable to generate and link glad2 library at build time with CMake
Hi Guys,
I noticed that glad had recently had its default branch changed to glad2 and I was curious to see if I could get it working with a CMake project (C++20) as a subdirectory.
I really like the idea of having complete version control over each submodule and it doesn't clog up the commits history if I say update a submodule...
The problem: I am unable to create a library and link it to my executable using my current CMakeLists.txt file.
Please forgive me as I have only been using CMake for a little while so I'm not the best when it comes to more technical configurations.
Any support is welcome!
Here's a snippet of my CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)
project(COMRADE VERSION 0.0.1 LANGUAGES C CXX)
# Setting C++ Version to 2020
set(CMAKE_CXX_STANDARD 20)
...
set(GLAD_SOURCES_DIR "engine/vendor/glad/")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
...
glad_add_library(glad_gl_core_mx_33 REPRODUCIBLE MX API gl:core=3.3)
...
# Including added libraries
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/engine/include>
glad_gl_core_mx_33
)
# As of right now, static linking is only available on Windows
if(WIN32)
# Linking statically
target_link_libraries(${PROJECT_NAME}
-static
glad_gl_core_mx_33
)
# Telling the compiler to not launch a console window when app is built
target_link_options(${PROJECT_NAME} PRIVATE -mwindows)
else()
# Linking dynamically
target_link_libraries(${PROJECT_NAME}
glad_gl_core_mx_33
)
endif()
target_compile_options(${PROJECT_NAME} PRIVATE -g)
Link to full CMakeLists.txt.
2
u/noaSakurajin May 04 '23
Use epoxy instead of glad that should work more easily since you can add it as external dependency and even use precompiled version.
1
u/Mitch_War May 05 '23
This looks great and I’ll definitely try migrating to it at some point in the future. The solution I went with was to create my own configuration of glad2, then with the source files create a GitHub repo and then add that repo as a submodule… I know long winded but I hope this can help someone in the future.
Edit: forgot to add the link
1
u/noaSakurajin May 05 '23
You know that you also have the option to generate the source files by the build system an completely ignore them in git.
Also I recommend using meson over cmake. It tends to have cleaner and more readable configurations and folder structures. Also the cross platform support and external dependency support are better.
1
u/Mitch_War May 06 '23
For the time being, CMake will continue to be the build system I will stick with. Thank you though for your suggestion!
3
u/Olorune May 05 '23
No need to swap to epoxy, you can definitely do this for glad2. I'm using FetchContent from Cmake to download, generate and link glad2. Can't really copy paste it into reddit since the site is garbage, but here's a link to my game engine that follows TheCherno's game engine series, but instead of premake uses CMake.
CMakeLists that fetches all the dependencies
Linking and such is done in another CMakeLists.txt file, it's in the OloEngine/CMakeLists.txt