首页 > 解决方案 > 如何使用 g++ (MSYS2/MinGW) 在 Windows 上根据 Google 的灯丝库编译 C++ 项目?

问题描述

我对在尝试编译我的 C++ 项目时遇到的问题有点一头雾水,该项目使用Google 的 Filament 库,在 Windows 上使用g++ (MSYS2/MinGW)。它只是向我吐出大量错误消息(见下面的摘录)。我的猜测是它缺少 MSVCRT,但我不确定。如果是这种情况,我是否应该能够告诉 g++ 这个库将在运行时通过动态提供,msvcrt.dll所以它现在不关心引用?

main.cpp

#include <filament/Engine.h>
#include <filament/SwapChain.h>
#include <filament/Renderer.h>
#include <filament/View.h>
#include <utils/EntityManager.h>

int main()
{}

编译命令:

g++ \
    -std=c++14 -pthread \
    main.cpp \
    -IE:/libraries/filament/1.8.1/include \
    -IE:/libraries/sdl/2.0.12/x86_64-w64-mingw32/include \
    -LE:/libraries/filament/1.8.1/lib/x86_64/mt \
    `E:/libraries/sdl/2.0.12/x86_64-w64-mingw32/bin/sdl2-config --libs` \
    -lfilament -lbackend -lbluegl -lbluevk -lfilabridge -lfilaflat -lutils -lgeometry -lsmol-v -libl \
    -lpthread

它吐出 1233 KB 的消息。以下是一些摘录:

一开始有一些is-too-small-to-hold-all-values -errors:

In file included from E:/libraries/filament/1.8.1/include/backend/Platform.h:22,
                 from E:/libraries/filament/1.8.1/include/filament/Engine.h:20,
                 from main.cpp:2:
E:/libraries/filament/1.8.1/include/backend/DriverEnums.h:631:30: warning: 'filament::backend::SamplerParams::<unnamed union>::<unnamed struct>::filterMag' is too small to hold all values of 'enum class filament::backend::SamplerMagFilter'
  631 |             SamplerMagFilter filterMag      : 1;    //!< magnification filter (NEAREST)
      |                              ^~~~~~~~~
E:/libraries/filament/1.8.1/include/backend/DriverEnums.h:632:30: warning: 'filament::backend::SamplerParams::<unnamed union>::<unnamed struct>::filterMin' is too small to hold all values of 'enum class filament::backend::SamplerMinFilter'
  632 |             SamplerMinFilter filterMin      : 3;    //!< minification filter  (NEAREST)
      |                              ^~~~~~~~~
(some more cut out)

接下来是 40 行告诉我:

Warning: corrupt .drectve at end of def file

然后是很多神秘的未定义引用:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/libraries/filament/1.8.1/lib/x86_64/mt/bluegl.lib(bluegl.dir/Release/BlueGL.obj):(.text$x+0x8): undefined reference to `??1sentry@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAA@XZ'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/libraries/filament/1.8.1/lib/x86_64/mt/bluegl.lib(bluegl.dir/Release/BlueGL.obj):(.text$x+0x8): undefined reference to `??1_Sentry_base@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAA@XZ'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/libraries/filament/1.8.1/lib/x86_64/mt/bluegl.lib(bluegl.dir/Release/BlueGL.obj):(.text$x+0x8): undefined reference to `??1locale@std@@QEAA@XZ'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/libraries/filament/1.8.1/lib/x86_64/mt/bluegl.lib(bluegl.dir/Release/BlueGL.obj):(.text$x+0x8): undefined reference to `??1_Lockit@std@@QEAA@XZ'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/libraries/filament/1.8.1/lib/x86_64/mt/bluegl.lib(bluegl.dir/Release/BlueGL.obj):(.text$x+0x8): undefined reference to `??1?$unique_ptr@V_Facet_base@std@@U?$default_delete@V_Facet_base@std@@@2@@std@@QEAA@XZ'
(many many many more)

他们中有一些:

undefined reference to `__CxxFrameHandler4'

这让我想到了我的 MSVCRT 理论。

我的 g++ 版本:

$ g++ --version
g++.exe (Rev2, Built by MSYS2 project) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Windows 的 Binary Filament 附带不同的 lib 版本:mt、md、mtd 和 mdd,在 Microsoft 网站上进行了说明。我认为 mt 版本意味着没有到 MSVCRT 的静态链接,因此在编译期间没有参考问题。也许我在这里误解了一些事情?

标签: c++gccmingwmsys2

解决方案


推荐阅读