I've been trying to build the GLFW example (from http://www.glfw.org/documentation.html) using CMake and MinGW on Windows 10.
However I keep having the following linker errors:
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x17): undefined reference to `_imp__glfwInit'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x58): undefined reference to `_imp__glfwCreateWindow'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x68): undefined reference to `_imp__glfwTerminate'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x7c): undefined reference to `_imp__glfwMakeContextCurrent'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x89): undefined reference to `_imp__glfwWindowShouldClose'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xa0): undefined reference to `_imp__glClear@4'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xb0): undefined reference to `_imp__glfwSwapBuffers'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xb7): undefined reference to `_imp__glfwPollEvents'
CMakeFiles\opengl-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xc0): undefined reference to `_imp__glfwTerminate'
Currently I'm trying to link dynamically but I have tried to do it with the static libraries and encountered a similar problem.
Here's the CMakeFile.txt that I'm using to build it:
cmake_minimum_required (VERSION 3.0)
project (opengl-test)
set (GCC_COMPILE_FLAGS "-Wall -Wextra")
set (GCC_LINK_FLAGS "-Llib -lglfw3dll -lopengl32 -lglu32")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS}")
file (GLOB_RECURSE SRCS src/*.cpp src/*.cxx src/**/*.cpp src/**/*.cxx)
file (GLOB_RECURSE HEADERS src/*.h src/**/*.h)
MESSAGE("${SRCS}")
MESSAGE("${HEADERS}")
add_definitions(-DGLFW_DLL)
set (EXECUTABLE_OUTPUT_PATH ..)
include_directories ("${PROJECT_BINARY_DIR}")
link_directories ("${CMAKE_SOURCE_DIR}/lib")
add_executable (opengl-test ${SRCS} ${HEADERS})
target_include_directories(opengl-test PRIVATE include)
set_target_properties(opengl-test PROPERTIES LINKER_LANGUAGE CXX)
Here's the directory structure:
.
| CMakeLists.txt
| glfw3.dll
|
+---build
+---include
| \---GLFW
| glfw3.h
| glfw3native.h
|
+---lib
| libglfw3.a
| libglfw3dll.a
|
\---src
main.cpp
I've already tried most of the solutions I could find but no one seems to have the same problem as their solutions often are things that I have already done. I'm using the precompiled libraries for mingw-32. I'd expect the undefined references to be in libglfw3dll.a but maybe I'm wrong. Is the actual DLL needed during linking?
I'm not very experienced with CMake so it could also be a beginner mistake in the CMakeFile.
Example code I'm trying to build:
#include "GLFW/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;
}