在Windows下编译程序会给出一堆"error: template with C linkage"报告
Compiling a program under Windows gives a bunch of "error: template with C linkage" reports
我做了一个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 标头库中,对于其他库的正常运行至关重要。
经验教训:切勿尝试在操作系统之后使用系统特定的代码命名文件。
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 传递给std::function template的template参数究竟代表什么
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 在template中使用std::variant的template函数
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- C++ template for QList
- 对象声明中的模板"error: expected expression"
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 在Windows下编译程序会给出一堆"error: template with C linkage"报告
- Clang VS VC++: "error: declaration of 'T' shadows template parameter"