【发布时间】:2018-07-08 01:59:43
【问题描述】:
我一直在尝试在 Windows 10 上使用 CMake 和 MinGW 构建 GLFW 示例(来自 http://www.glfw.org/documentation.html)。
但是我一直遇到以下链接器错误:
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'
目前我正在尝试动态链接,但我尝试使用静态库进行链接并遇到类似问题。
这是我用来构建它的 CMakeFile.txt:
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)
目录结构如下:
.
| CMakeLists.txt
| glfw3.dll
|
+---build
+---include
| \---GLFW
| glfw3.h
| glfw3native.h
|
+---lib
| libglfw3.a
| libglfw3dll.a
|
\---src
main.cpp
我已经尝试了大部分我能找到的解决方案,但似乎没有人遇到同样的问题,因为他们的解决方案通常是我已经做过的事情。我正在使用 mingw-32 的预编译库。我希望未定义的引用在 libglfw3dll.a 中,但也许我错了。链接期间是否需要实际的 DLL?
我对 CMake 不是很有经验,所以它也可能是 CMakeFile 中的初学者错误。
我正在尝试构建的示例代码:
#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;
}
【问题讨论】:
-
哦,愚蠢的我!我对 CMake 很不满意。这确实是重复的,谢谢。