c++cmakelnk2001glad

How to link GLAD using CMake?


I'm trying to setup GLAD in my project. But I keep getting multiple linking error like this one:

error LNK2001: unresolved external symbol glad_glClear

here are the part of the CMake file that are related to GLAD:

cmake_minimum_required(VERSION 3.10)

project(AR)

...

# Set Glad library
set(GLAD_DIR C:/tools/CPP/glad)

...

add_library(glad ${GLAD_DIR}/src/glad.c)

target_include_directories(glad PUBLIC ${GLAD_DIR}/include)

...

What am I doing wrong ?

I tried moving the glad.c in multiple folder, but it keeps giving me the same error.


Solution

  • Point include_directories() at the GLAD headers and toss glad.c into the add_executable() source file list. target_link_libraries()'s library list doesn't need to change.

    All together:

    ...
    include_directories( SYSTEM "external/glad/include" )
    
    add_executable( app-name "src/main.cpp" "external/glad/src/glad.c" )
    
    target_link_libraries( app-name )
    ...