Linux 头文件中的代码似乎会导致编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7114820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:45:20  来源:igfitidea点击:

Code in header files seems to cause errors on compile

clinuxcompiler-errorsheader-files

提问by M_M

I have a CLI program consisting of a single vt.c file that compiles and runs under Windows (cmd.exe) using Open Watcom. I can also compile it for Linux while running Open Watcom on Windows (and the resulting build runs under linux).

我有一个 CLI 程序,它由一个单独的 vt.c 文件组成,该文件使用 Open Watcom 在 Windows (cmd.exe) 下编译和运行。我也可以在 Windows 上运行 Open Watcom 时为 Linux 编译它(并且生成的构建在 linux 下运行)。

When I try to compile it under linux however (using make, which calls cc), I get 375 lines of error messages. Here are some of them, where many subsequent errors were similar, I have only pasted the first few:

然而,当我尝试在 linux 下编译它时(使用调用 cc 的 make),我收到 375 行错误消息。以下是其中一些,其中许多后续错误都相似,我只粘贴了前几个:

In file included from vt.c:4:0:
process.h: In function ‘__declspec':
process.h:45:22: error: storage class specified for parameter ‘execl'
process.h:46:1: error: expected declaration specifiers before ‘__declspec'
process.h:47:1: error: expected declaration specifiers before ‘__declspec'
...
In file included from vt.c:5:0:
ctype.h:48:1: warning: empty declaration
ctype.h:81:37: error: storage class specified for parameter ‘__ctype_b_loc'
ctype.h:82:6: warning: ‘__nothrow__' attribute ignored
ctype.h:83:28: error: storage class specified for parameter ‘__ctype_tolower_loc'
ctype.h:84:6: warning: ‘__nothrow__' attribute ignored
ctype.h:85:28: error: storage class specified for parameter ‘__ctype_toupper_loc'
ctype.h:86:6: warning: ‘__nothrow__' attribute ignored
...
In file included from vt.c:6:0:
string.h:44:14: error: storage class specified for parameter ‘memcpy'
string.h:46:6: warning: ‘__nothrow__' attribute ignored
string.h:49:14: error: storage class specified for parameter ‘memmove'
string.h:50:6: warning: ‘__nothrow__' attribute ignored
string.h:57:14: error: storage class specified for parameter ‘memccpy'
string.h:59:6: warning: ‘__nothrow__' attribute ignored
...
vt.c:28:1: warning: empty declaration
vt.c:41:1: warning: empty declaration
vt.c:50:1: error: parameter ‘maxtextlength' is initialized
vt.c:70:1: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘{' token
vt.c:123:1: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘{' token
vt.c:158:1: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘{' token
...
vt.c:67:6: error: declaration for parameter ‘clearinputbuffer' but no such parameter
vt.c:66:6: error: declaration for parameter ‘clrscr' but no such parameter
vt.c:65:6: error: declaration for parameter ‘testrandom' but no such parameter
...
string.h:579:14: error: declaration for parameter ‘stpncpy' but no such parameter
...
ctype.h:268:12: error: declaration for parameter ‘toupper_l' but no such parameter
...
process.h:45:22: error: declaration for parameter ‘execl' but no such parameter
vt.c:608:1: error: expected ‘{' at end of input
make: *** [vt] Error 1

My problem is, as (I'm sure) many have had before: I just want it to compile.

我的问题是,因为(我敢肯定)很多人以前都有过:我只是想让它编译。

Question 3641178 seems to suggest that the order of the include files is important, but the headers that are giving errors come AFTER all other included files in vt.c.

问题 3641178 似乎表明包含文件的顺序很重要,但在 vt.c 中的所有其他包含文件之后出现错误的标题。

I have tried installing Open Watcom on Linux, and the provided header files are the same. As far as I know, these are standard header files, and I can think of no reason why they should cause compile errors.

我已经尝试在 Linux 上安装 Open Watcom,并且提供的头文件是相同的。据我所知,这些都是标准的头文件,我想不出它们为什么会导致编译错误。

If anyone can shed any light on this, I'd be very grateful. The entire directory can be accessed in my git repo at [email protected]:megamasha/Vocab-Tester.git (https://github.com/megamasha/Vocab-Tester)

如果有人能对此有所了解,我将不胜感激。整个目录可以在我的 git 仓库中访问 [email protected]:megamasha/Vocab-Tester.git ( https://github.com/megamasha/Vocab-Tester)

采纳答案by Matteo Italia

Your process.h, string.h, ... come from a specific compiler (watcom) and contain compiler-specific keywords (e.g. __declspec) that are not supported by gcc(the compiler usually used on Linux).

您的process.h, string.h, ... 来自特定的编译器 (watcom) 并包含(通常在 Linux 上使用的编译器__declspec)不支持的特定于编译器的关键字(例如gcc)。

As far as string.hand ctype.hare concerned, you should remove them completely from your directory and #includethem with angular brackets (<...>): they are standard headers and each compiler provide its versions (that are compatible with what the standard says).

string.hctype.h而言,您应该将它们从目录中完全删除,#include并使用尖括号 ( <...>)将它们删除:它们是标准头文件,并且每个编译器都提供其版本(与标准所说的兼容)。

With process.h, the situation is a bit more difficult, since it's a nonstandard header. Still, as far as I see, process.hseems to contain just some functions that are used to spawn processes, and, from a quick look, it seems that your application doesn't need it; if so, just remove process.hand the relative #include. Otherwise, tell me in the comment, probably there's a quick standard (or OS-specific, but not compiler-specific) replacement.

使用process.h,情况有点困难,因为它是一个非标准的标题。尽管如此,据我所知,process.h似乎只包含一些用于生成进程的函数,而且,从快速浏览来看,您的应用程序似乎不需要它;如果是这样,只需删除process.h和相关的#include。否则,请在评论中告诉我,可能有一个快速标准(或特定于操作系统,但不是特定于编译器)的替代品。