I use FetchContent to get the library (GLFW) and try to use dependencies inside the “deps” folder.
Here my file structure:
<code>├── CMakeLists.txt
├── main.cpp
├── build
├── cmake
│ └── AddGLFW.cmake
</code>
<code>├── CMakeLists.txt
├── main.cpp
├── build
├── cmake
│ └── AddGLFW.cmake
</code>
├── CMakeLists.txt
├── main.cpp
├── build
├── cmake
│ └── AddGLFW.cmake
CMakeLists.txt
<code>cmake_minimum_required(VERSION 3.20)
project(Test1)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(AddGLFW)
include_directories("${GLFW_SOURCE_DIR}/deps")
set(GLAD_GL "${GLFW_SOURCE_DIR}/deps/glad/gl.h")
add_executable(main WIN32 main.cpp ${GLAD_GL})
target_link_libraries(main glfw)
</code>
<code>cmake_minimum_required(VERSION 3.20)
project(Test1)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(AddGLFW)
include_directories("${GLFW_SOURCE_DIR}/deps")
set(GLAD_GL "${GLFW_SOURCE_DIR}/deps/glad/gl.h")
add_executable(main WIN32 main.cpp ${GLAD_GL})
target_link_libraries(main glfw)
</code>
cmake_minimum_required(VERSION 3.20)
project(Test1)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(AddGLFW)
include_directories("${GLFW_SOURCE_DIR}/deps")
set(GLAD_GL "${GLFW_SOURCE_DIR}/deps/glad/gl.h")
add_executable(main WIN32 main.cpp ${GLAD_GL})
target_link_libraries(main glfw)
AddGLFW.cmake
<code>include(FetchContent)
FetchContent_Declare (
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.4
)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
set(GLFW_INSTALL OFF CACHE BOOL "")
FetchContent_MakeAvailable(glfw)
</code>
<code>include(FetchContent)
FetchContent_Declare (
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.4
)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
set(GLFW_INSTALL OFF CACHE BOOL "")
FetchContent_MakeAvailable(glfw)
</code>
include(FetchContent)
FetchContent_Declare (
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.4
)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
set(GLFW_INSTALL OFF CACHE BOOL "")
FetchContent_MakeAvailable(glfw)
main.cpp
<code>#include "glad/gl.h"
#include "GLFW/glfw3.h"
#include <iostream>
int main() {
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
int version = gladLoadGL(glfwGetProcAddress);
if (version == 0) {
printf("Failed to initialize OpenGL contextn");
return -1;
}
// Successfully loaded OpenGL
printf("Loaded OpenGL %d.%dn", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
}
</code>
<code>#include "glad/gl.h"
#include "GLFW/glfw3.h"
#include <iostream>
int main() {
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
int version = gladLoadGL(glfwGetProcAddress);
if (version == 0) {
printf("Failed to initialize OpenGL contextn");
return -1;
}
// Successfully loaded OpenGL
printf("Loaded OpenGL %d.%dn", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
}
</code>
#include "glad/gl.h"
#include "GLFW/glfw3.h"
#include <iostream>
int main() {
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
int version = gladLoadGL(glfwGetProcAddress);
if (version == 0) {
printf("Failed to initialize OpenGL contextn");
return -1;
}
// Successfully loaded OpenGL
printf("Loaded OpenGL %d.%dn", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
}
When I build my project with cmake, there’s a error “undefined reference to gladLoadGL”, I don’t know what go wrong hear, please help.
New contributor
Iaoh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.