0

I'm trying to run the example code in GLFW. I'm trying to build the project using CMake. I've gone through lots of posts in stackoverflow. But I'm unable to add the GLFW library, correctly to the project. Could you please help me configure CMakeLists.txt file?

Project structure

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(cmake_testapp)

set(CMAKE_CXX_STANDARD 14)

include_directories(includes/GLFW)

LINK_DIRECTORIES(lib)
add_executable(cmake_testapp main.cpp)
TARGET_LINK_LIBRARIES(cmake_testapp glfw3)

main.cpp

#include <glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Error when building:

====================[ Build | cmake_testapp | Debug ]===========================
"C:\Program Files\JetBrains\CLion 2020.2.4\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProjects\cmake_testapp\cmake-build-debug --target cmake_testapp -- -j 6
[ 50%] Linking CXX executable cmake_testapp.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\cmake_testapp.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/User/CLionProjects/cmake_testapp/main.cpp:26: undefined reference to `_imp__glClear@4'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cmake_testapp.dir\build.make:105: recipe for target 'cmake_testapp.exe' failed
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/cmake_testapp.dir/all' failed
mingw32-make.exe[3]: *** [cmake_testapp.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/cmake_testapp.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/cmake_testapp.dir/rule] Error 2
mingw32-make.exe: *** [cmake_testapp] Error 2
CMakeFiles\Makefile2:101: recipe for target 'CMakeFiles/cmake_testapp.dir/rule' failed
Makefile:137: recipe for target 'cmake_testapp' failed
root
  • 157
  • 1
  • 3
  • 16
  • 1
    Don't know anything about your specific situation but have you tried `TARGET_LINK_LIBRARIES(cmake_testapp glfw3)`? It's normal in Linux environments to remove the `lib` prefix and the `.a` suffix. – john Dec 26 '20 at 07:30
  • Tried like u said. It did change the error. Changed the question. Also I'm in windows. – root Dec 26 '20 at 07:40
  • 1
    Well the new error means it is now finding the library so that is progress. I know you are on Windows but MinGW is a Linux derived compiler, so Linux conventions apply. – john Dec 26 '20 at 07:43
  • 1
    The reference to glClear is a reference to the loader library. So you must be missing that when you build. Since there are several to choose from I can't tell you what to do. – john Dec 26 '20 at 07:46
  • Yeah, thanks so much for help. I think the issue now is it's missing opengl library – root Dec 26 '20 at 07:46
  • This might help https://www.glfw.org/docs/3.3/build_guide.html#build_link_cmake_package – john Dec 26 '20 at 07:49
  • added `TARGET_LINK_LIBRARIES(cmake_testapp opengl32)` to CMakeLists.txt file and issue was fixed. Thanks @john for help :) – root Dec 26 '20 at 10:40

0 Answers0