我一直在尝试在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;
}
答案 0 :(得分:1)
我尝试仅从github克隆glfw,并使用main.cpp。我最终得到了一个更简单的东西:
/
CMakeLists.txt (see below)
glfw/ (cloned from github)
src/main.cpp (containing your main.cpp)
cmake_minimum_required(VERSION 3.0)
project(opengl-test)
find_package(OpenGL REQUIRED)
add_subdirectory(glfw)
add_executable(gltest
src/main.cpp
)
target_link_libraries(gltest
glfw
OpenGL::GL
)
这至少在Mac上可以正常工作。
对于glfw的构建方式,我并没有付出太多努力,只是在使用“ add_subdirectory”添加它时将其默认设置为默认值。
但是您也可以轻松地转到glfw文件夹,并对其进行配置以构建所需的样式,然后执行cmake --build。 --target安装
然后将脚本中的行从add_subdirectory更改为find_package(需要glfw)
好,再次使用与上述相同的设置,但是添加目录“安装”以放置预构建的库。
然后我转到glfw目录并运行以下命令:
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS:BOOL=On -DCMAKE_INSTALL_PREFIX=`pwd`/../../install -DGLFW_BUILD_DOCS=OFF -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF ..
cmake --build . --config Release
cmake --build . --target install
现在我有
.
./CMakeLists.txt
./glfw/... # Actually, this could be outside of the tree, or deleted.
./install/include/GLFW/glfw3.h
./install/include/GLFW/glfw3native.h
./install/lib/cmake/glfw3/glfw3Config.cmake
./install/lib/cmake/glfw3/glfw3ConfigVersion.cmake
./install/lib/cmake/glfw3/glfw3Targets-noconfig.cmake
./install/lib/cmake/glfw3/glfw3Targets.cmake
./install/lib/libglfw.3.3.dylib
./install/lib/libglfw.3.dylib
./install/lib/libglfw.dylib
./install/lib/pkgconfig/glfw3.pc
./src/main.cpp
与您的产品更接近,但是那里也有一个不错的cmake安装脚本,可以很好地将其导入。
cmake_minimum_required(VERSION 3.0)
project(opengl-test)
find_package(OpenGL REQUIRED)
set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/install)
find_package(glfw3 REQUIRED)
add_executable(gltest
src/main.cpp
)
target_link_libraries(gltest
glfw
OpenGL::GL
)
这个版本可以建立,我对dylib的定位确实有点问题,因为它使用某种奇怪的相对路径名与它链接,在那里它在相对lib /文件夹中查找dll。我从安装目录运行进行测试,并且运行良好。