在Windows下编译程序会给出一堆"error: template with C linkage"报告

Compiling a program under Windows gives a bunch of "error: template with C linkage" reports

本文关键字:error template 一堆 with 报告 linkage 编译程序 Windows      更新时间:2023-10-16

我做了一个OpenGL项目,可以用GCC(版本4.7.3及更高版本)编译,并且可以在Linux上运行。当尝试使用安装了GCC 4.9.2的MSYS2在Windows下编译相同的代码时,我收到大量错误报告:

g++ -g --std=c++11 src/*.cpp -Iinclude -Isrc -lIL -lILU -lILUT -lGL -lGLU -lglut -lm -DWIN32 -I/mingw64/include windows/src/*.cpp -o "Achtung, die Kurve 3D!"
In file included from /usr/lib/gcc/x86_64-pc-msys/4.9.2/include/c++/bits/stringfwd.h:40:0,
                 from /usr/lib/gcc/x86_64-pc-msys/4.9.2/include/c++/string:39,
                 from include/windows.h:1,
                 from /usr/include/w32api/GL/gl.h:13,
                 from /mingw64/include/GL/freeglut_std.h:143,
                 from /mingw64/include/GL/freeglut.h:17,
                 from src/controls.cpp:1:
/usr/lib/gcc/x86_64-pc-msys/4.9.2/include/c++/bits/memoryfwd.h:63:3: error: template with C linkage
   template<typename>
   ^
/usr/lib/gcc/x86_64-pc-msys/4.9.2/include/c++/bits/memoryfwd.h:66:3: error: template specialization with C linkage
   template<>
   ^
/usr/lib/gcc/x86_64-pc-msys/4.9.2/include/c++/bits/memoryfwd.h:70:3: error: template with C linkage
   template<typename, typename>
   ^
...
In file included from /mingw64/include/GL/freeglut_std.h:143:0,
                 from /mingw64/include/GL/freeglut.h:17,
                 from src/controls.cpp:1:
/usr/include/w32api/GL/gl.h:684:1: error: ‘WINGDIAPI’ does not name a type
 WINGDIAPI void APIENTRY glAccum(GLenum op,GLfloat value);
...
/usr/include/w32api/GL/gl.h:1037:24: error: expected ‘)’ before ‘*’ token
 typedef void (APIENTRY *PFNGLGETCOLORTABLEPARAMETERFVEXTPROC)(GLenum target,GLenum pname,GLfloat *params);
                        ^
In file included from /mingw64/include/GL/freeglut_std.h:144:0,
                 from /mingw64/include/GL/freeglut.h:17,
                 from src/controls.cpp:1:
/usr/include/w32api/GL/glu.h:24:25: error: expected initializer before ‘gluErrorString’
 const GLubyte *APIENTRY gluErrorString(GLenum errCode);
                         ^
/usr/include/w32api/GL/glu.h:25:25: error: expected initializer before ‘gluErrorUnicodeStringEXT’
 const wchar_t *APIENTRY gluErrorUnicodeStringEXT(GLenum errCode);
                         ^
...

这仅取自由 4500 行错误组成的部分日志。这些是重复多次的最常见的。

以前认为问题出在旧的 MSYS/MinGW(不是 MSYS2 端口)上,我首先尝试了相同的结果。但是,MSYS2 并没有解决问题,这使我完全无能为力,因为我的代码是用 C++ 编写的,它只需要标准的 C 和 C++ 头文件以及一些 GL 头文件。我不使用任何外部"C"重整。

您在这里使用的是 msys2 GCC,而不是 mingw-w64 GCC。

请阅读MSYS2 wiki [1],其中解释了所有这些内容,但简要说明:

运行mingw64_shell.bat,而不是msys2_shell.bat,安装 mingw-w64 GCC 工具链包。此命令使用 bash 大括号扩展功能安装 32 位和 64 位:

pacman -S mingw-w64-{x86_64,i686}-toolchain

.. 然后看到gcc -dumpmachine报告x86_64-w64-mingw32而不是x86_64-pc-msys

[1] https://sourceforge.net/p/msys2/wiki/Home/

这个问题与

OpenGL无关,也与MSYS(2)/MinGW(-w64)或MS Visual Studio 2013无关。

对于可能面临与我相同问题的任何人,绝大多数错误报告都是生成的,因为我的项目包含路径中存在一个自定义windows.h头文件。具有相同名称的标头存在于标准 Windows 标头库中,对于其他库的正常运行至关重要。

经验教训:切勿尝试在操作系统之后使用系统特定的代码命名文件。