【问题标题】:Undefined references when attempting to build GLFW example with MinGW/CMake [duplicate]尝试使用 MinGW/CMake 构建 GLFW 示例时未定义的引用 [重复]
【发布时间】: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 很不满意。这确实是重复的,谢谢。

标签: c++ cmake mingw glfw


【解决方案1】:

简单的方法

我尝试简单地从 github 克隆 glfw,并使用您的 main.cpp。我最终得到了一些更简单的东西,例如:

/
  CMakeLists.txt   (see below)
  glfw/            (cloned from github)
  src/main.cpp     (containing your main.cpp)

CMakeScripts.txt

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 。 --目标安装

然后将脚本中的行从 add_subdirectory 更改为 find_package(glfw REQUIRED)

使用安装

好的,再次使用与上述相同的设置,但添加一个目录,“安装”以放置预构建的库。

然后我转到 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 安装脚本,可以很好地导入。

新建 CMakeLists.txt

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 时确实遇到了一些问题,因为它使用某种奇怪的相对路径 soname 与它链接,它在相对 lib/ 文件夹中查找 dll。我从安装目录跑到了测试,但效果很好。

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2016-11-05
    • 2022-01-04
    • 2017-06-05
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多