Skip to content

How to solve undefined reference error? #425

@AumDhabalia

Description

@AumDhabalia

This is the CMakeLists.txt file for generating build for simple hello_window program of opengl.
`
cmake_minimum_required(VERSION 4.0.0)
project(hello_world VERSION 1.0.0)

cmake_policy(SET CMP0072 NEW)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

add_executable(hello_world src/main.cpp src/glad.c)

target_include_directories(hello_world PRIVATE dependencies)

target_link_libraries(hello_world ${GLFW3_LIBRARY} OpenGL::GL)
`

Whenever I try to build and run I receive error
[main] Building folder: d:/cpp_programs/test_opengl_project/build [build] Starting build [proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/cpp_programs/test_opengl_project/build --config Debug --target all -j 12 -- [build] [ 33%] Building CXX object CMakeFiles/hello_world.dir/src/main.cpp.obj [build] [ 66%] Building C object CMakeFiles/hello_world.dir/src/glad.c.obj [build] [100%] Linking CXX executable hello_world.exe [build] CMakeFiles\hello_world.dir/objects.a(main.cpp.obj): In functionmain':
[build] D:/cpp_programs/test_opengl_project/src/main.cpp:9: undefined reference to glfwInit' [build] D:/cpp_programs/test_opengl_project/src/main.cpp:16: undefined reference to glfwCreateWindow'
[build] D:/cpp_programs/test_opengl_project/src/main.cpp:19: undefined reference to glfwMakeContextCurrent' [build] D:/cpp_programs/test_opengl_project/src/main.cpp:22: undefined reference to glfwGetProcAddress'
[build] D:/cpp_programs/test_opengl_project/src/main.cpp:24: undefined reference to glfwTerminate' [build] D:/cpp_programs/test_opengl_project/src/main.cpp:31: undefined reference to glfwWindowShouldClose'
[build] D:/cpp_programs/test_opengl_project/src/main.cpp:33: undefined reference to glfwPollEvents' [build] D:/cpp_programs/test_opengl_project/src/main.cpp:35: undefined reference to glfwSwapBuffers'
[build] D:/cpp_programs/test_opengl_project/src/main.cpp:38: undefined reference to glfwTerminate' [build] collect2.exe: error: ld returned 1 exit status [build] mingw32-make.exe[2]: *** [hello_world.exe] Error 1 [build] CMakeFiles\hello_world.dir\build.make:119: recipe for target 'hello_world.exe' failed [build] mingw32-make.exe[1]: *** [CMakeFiles/hello_world.dir/all] Error 2 [build] CMakeFiles\Makefile2:85: recipe for target 'CMakeFiles/hello_world.dir/all' failed [build] Makefile:89: recipe for target 'all' failed [build] mingw32-make.exe: *** [all] Error 2 [proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/cpp_programs/test_opengl_project/build --config Debug --target all -j 12 -- exited with code: 2 [driver] Build completed: 00:00:01.295 [build] Build finished with exit code 2
I am using vscode as editor, mingw for c/c++ libraries and windows os as platform. I have referred to this video solution but no luck.

Need solution for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions