Reputation: 3
I use FetchContent to get the library (GLFW) and try to use dependencies inside the "deps" folder.
Here my file structure:
├── CMakeLists.txt
├── main.cpp
├── build
├── cmake
│ └── AddGLFW.cmake
CMakeLists.txt
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
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
#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 context\n");
return -1;
}
// Successfully loaded OpenGL
printf("Loaded OpenGL %d.%d\n", 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.
Upvotes: 0
Views: 310
Reputation: 66118
When use header glad/gl.h
shipped with GLFW, you need to define the macro GLAD_GL_IMPLEMENTATION
for obtain definition of functions in that header:
#define GLAD_GL_IMPLEMENTATION
#include "glad/gl.h"
(If you use that header from several sources of your application, then only one source should define that macro.)
Upvotes: 0