-2

I'm a beginner with cmake and I have a problem when I try to link the GLFW static library to my project. My goal is to make the project as portable as possible. In other words, the code should be editable and executable on Windows and Linux if possible. I'm using the pre-compiled windows 64-bits binaries and a Windows 10 PC. I find no answer for my problem and my project configuration on stackoverflow :/

When I try to build my project with my run.sh script, the terminal returns these errors :

my terminal

The errors seem to indicate that the compiler cannot find the definitions of GLFW functions, but I don't see my mistake :(

So here is the tree structure of my project :

enter image description here

My main.cpp, which is just a simple program displaying a window :

#include <GLFW/glfw3.h>

int main() {
    // init GLFW
    if (!glfwInit()) {
        return -1;
    }

    // window creation
    GLFWwindow* window = glfwCreateWindow(800, 600, "project", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    // main loop
    while (!glfwWindowShouldClose(window)) {
        // render code

        // swap buffers
        glfwSwapBuffers(window);

        // event checking
        glfwPollEvents();
    }

    // close GLFW
    glfwTerminate();
    return 0;
}

Here is my CMakeLists.txt :

cmake_minimum_required(VERSION 3.7)
project(project)

set(CMAKE_CXX_STANDARD 23)

set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/libs/glfw/include)

include_directories(${GLFW_INCLUDE_DIR})
link_directories(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/libs/glfw/lib-static-ucrt)

set(SOURCES 
    ${CMAKE_SOURCE_DIR}/src/main.cpp)

add_executable(${PROJECT_NAME} ${SOURCES})

target_link_libraries(${PROJECT_NAME} PRIVATE glfw3dll)

And a little script run.sh to build and run the project :

#!/bin/bash

#Create a build directory and navigate into it
rm -rf cmake-build-debug
mkdir cmake-build-debug
cd cmake-build-debug

#Generate the Makefile using CMake
cmake -G "Unix Makefiles" ..

#build the project using CMake
make

#Check if the compilation was successful
if [ $? -eq 0 ]; then
    echo "Compilation successful. Running the project..."
    #Execute the project binary
    ./project
else
    echo "Compilation failed. Please check the error messages."
fi
Paul
  • 1
  • 1
  • You need to define `GLFW_DLL` https://www.glfw.org/docs/3.3/build_guide.html#build_macros – Alan Birtles Aug 16 '23 at 22:02
  • Thanks, I hadn't noticed that line in the documentation! But the error is still there – Paul Aug 16 '23 at 22:06
  • I didn't noticed but the error has changed slightly, the undefined functions now have _ imp __ in front of them – Paul Aug 16 '23 at 22:21
  • Stack Overflow discourages using **images** for represent *text*. Instead, paste the error message and structure of your project in **textual** form. See [ask] and [that question](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) on meta. – Tsyvarev Aug 16 '23 at 22:30
  • #1 Your MinGW is very old. The current version has gcc-13.2.0. #2 I think your lib and dll are form msvc and not MinGW. Both of these could be solved by using msys2 to install MinGW + GLFW – drescherjm Aug 16 '23 at 22:51
  • How can I update my MinGW ? For the lib, I took it from the GLFW's website, I don't know how to have it from MinGW and not from MSVC. And what I want is a project as portable as possible. With as many libraries as possible in the project and not in the machine's environment variables, and the CMaleLists makes all the links between the libraries and the project. Is that possible or is this the wrong way to think ? – Paul Aug 16 '23 at 23:07
  • Your `CMakeLists.txt` looks okay. I generally would use `find_package( GLFW REQUIRED)` instead of putting the libs in the project folder but that is a style thing. – drescherjm Aug 16 '23 at 23:18

0 Answers0