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 :
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 :
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