I'm pretty new to CMake, and have just gotten into setting it up. I've gone ahead and implemented a simple opengl boilerplate, whose tree is like this
CMakeLists.txt
include
glad
KHR
src
glad
glad.c
CMakeLists.txt
main.cpp
CMakeLists.txt
lib
.gitignore
.gitmodules
CMakePresets.json
README.md
My Root CMakeLists is this:
cmake_minimum_required (VERSION 3.8)
project ("GameBro")
set(APP_NAME "GameBro")
# Opengl stuff
find_package( OpenGL REQUIRED )
# GLFW stuff
set(BUILD_SHARED_LIBS OFF CACHE BOOL "")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "")
set(GLFW_BUILD_TESTS OFF CACHE BOOL "")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
set(GLFW_INSTALL OFF CACHE BOOL "")
# MSVC related stuff
if( MSVC )
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup" )
endif()
# Macro to add sources from subdirs
macro (add_sources)
file (RELATIVE_PATH _relPath "${PROJECT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
foreach (_src ${ARGN})
if (_relPath)
list (APPEND sources "${_relPath}/${_src}")
else()
list (APPEND sources "${_src}")
endif()
endforeach()
if (_relPath)
# propagate SRCS to parent directory
set (sources ${sources} PARENT_SCOPE)
endif()
endmacro()
# Library subdir
add_subdirectory( lib )
# Add sources
add_subdirectory( src )
# Add executable
include_directories( ${OPENGL_INCLUDE_DIRS} lib/glfw/include lib/glfw/deps include )
add_executable ( ${APP_NAME} ${sources} )
target_link_libraries (${APP_NAME} glfw ${GLFW_LIBRARIES})
My src CMakeLists is like this
add_sources( main.cpp )
#add_sources( glad/glad.c )
add_subdirectory (glad)
My src/glad CMakeLists is like this
add_sources( glad.c )
The problem is, when i try to compile - i get errors like undefined reference to glad_glCreateShader etc.... But, when i put glad.c in the same dir as main.cpp and modify CMakeLists accordingly, it works properly and compiles, and displays a rectangle. WHat am I doing wrong here?