在Linux上的C代码编译错误(同样的代码编译上OSX)(Compilation Error of

2019-09-21 15:53发布

我试图编译在Linux上,我知道编译OSX上的一些代码,但我得到的一些问题。

所有的文件都拥有一个名为.H头,并且所有的文件都在同一目录下。 我编译如下:

gcc *.c -std=c99 -lpthread

虽然这个代码不会对OSX编译,我得到我的Ubuntu一堆奇怪的链接错误的安装。 我缺少一些编译器选项? 这是一个默认的Ubuntu的服务器安装与其他软件包gccbuild-essential安装。

In file included from errorLogger.h:24:0,
                 from configParser.h:17,
                 from configParser.c:9:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
configParser.c: In function ‘parseConfigFile’:
configParser.c:114:5: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
In file included from errorLogger.h:24:0,
                 from global.h:18,
                 from connection.h:19,
                 from connection.c:10:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
connection.c: In function ‘createConnectionQueue’:
connection.c:189:28: warning: assignment makes integer from pointer without a cast [enabled by default]
In file included from errorLogger.h:24:0,
                 from database.h:16,
                 from database.c:9:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
In file included from errorLogger.h:24:0,
                 from errorLogger.c:10:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
errorLogger.c: In function ‘reportError’:
errorLogger.c:63:5: warning: implicit declaration of function ‘strerror_r’ [-Wimplicit-function-declaration]
errorLogger.c: In function ‘logMessage’:
errorLogger.c:87:5: warning: implicit declaration of function ‘localtime_r’ [-Wimplicit-function-declaration]
errorLogger.c: In function ‘processErrorQueue’:
errorLogger.c:131:17: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
errorLogger.c:131:57: error: ‘O_APPEND’ undeclared (first use in this function)
errorLogger.c:131:57: note: each undeclared identifier is reported only once for each function it appears in
errorLogger.c:131:68: error: ‘O_CREAT’ undeclared (first use in this function)
errorLogger.c:131:78: error: ‘O_WRONLY’ undeclared (first use in this function)
errorLogger.c:131:88: error: ‘S_IWRITE’ undeclared (first use in this function)
errorLogger.c:131:99: error: ‘S_IREAD’ undeclared (first use in this function)
errorLogger.c:146:13: warning: implicit declaration of function ‘fsync’ [-Wimplicit-function-declaration]
errorLogger.c: In function ‘startErrorLogger’:
errorLogger.c:167:36: error: ‘O_APPEND’ undeclared (first use in this function)
errorLogger.c:167:47: error: ‘O_CREAT’ undeclared (first use in this function)
errorLogger.c:167:57: error: ‘O_WRONLY’ undeclared (first use in this function)
errorLogger.c:167:67: error: ‘S_IWRITE’ undeclared (first use in this function)
errorLogger.c:167:78: error: ‘S_IREAD’ undeclared (first use in this function)
errorLogger.c:214:57: error: ‘O_EXCL’ undeclared (first use in this function)
errorLogger.c:231:27: warning: assignment makes integer from pointer without a cast [enabled by default]
errorLogger.c: In function ‘closeErrorLogger’:
errorLogger.c:246:9: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
In file included from errorLogger.h:24:0,
                 from global.h:18,
                 from global.c:9:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
In file included from main.c:23:0:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
main.c: In function ‘main’:
main.c:53:5: warning: implicit declaration of function ‘blockSignals’ [-Wimplicit-function-declaration]
main.c:61:45: error: invalid application of ‘sizeof’ to incomplete type ‘struct addrinfo’ 
main.c:62:29: error: invalid application of ‘sizeof’ to incomplete type ‘struct addrinfo’ 
main.c:64:10: error: dereferencing pointer to incomplete type
main.c:65:10: error: dereferencing pointer to incomplete type
main.c:66:10: error: dereferencing pointer to incomplete type
main.c:66:23: error: ‘AI_PASSIVE’ undeclared (first use in this function)
main.c:66:23: note: each undeclared identifier is reported only once for each function it appears in
main.c:69:5: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
main.c:73:9: warning: implicit declaration of function ‘gai_strerror’ [-Wimplicit-function-declaration]
main.c:73:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat]
main.c:73:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat]
main.c:81:41: error: dereferencing pointer to incomplete type
main.c:83:30: error: dereferencing pointer to incomplete type
main.c:83:46: error: dereferencing pointer to incomplete type
main.c:83:64: error: dereferencing pointer to incomplete type
main.c:96:30: error: dereferencing pointer to incomplete type
main.c:96:44: error: dereferencing pointer to incomplete type
main.c:112:5: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
main.c:138:9: error: unknown type name ‘fd_set’
main.c:142:9: warning: implicit declaration of function ‘FD_ZERO’ [-Wimplicit-function-declaration]
main.c:143:9: warning: implicit declaration of function ‘FD_SET’ [-Wimplicit-function-declaration]
main.c:145:9: warning: implicit declaration of function ‘pselect’ [-Wimplicit-function-declaration]
In file included from signalHandling.c:10:0:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
signalHandling.c:12:18: error: unknown type name ‘sigset_t’
signalHandling.c: In function ‘setHandler’:
signalHandling.c:51:53: error: invalid application of ‘sizeof’ to incomplete type ‘struct sigaction’ 
signalHandling.c:52:36: error: invalid application of ‘sizeof’ to incomplete type ‘struct sigaction’ 
signalHandling.c:54:5: warning: implicit declaration of function ‘sigemptyset’ [-Wimplicit-function-declaration]
signalHandling.c:54:30: error: dereferencing pointer to incomplete type
signalHandling.c:60:9: warning: implicit declaration of function ‘sigaddset’ [-Wimplicit-function-declaration]
signalHandling.c:60:35: error: dereferencing pointer to incomplete type
signalHandling.c:67:17: error: dereferencing pointer to incomplete type
signalHandling.c:72:9: warning: implicit declaration of function ‘sigaction’ [-Wimplicit-function-declaration]

Answer 1:

你可能缺少一些#include它们带来了隐含在OS X S,但不能在Linux上。

从错误信息来看,你可能缺少包括至少:

  • <signal.h> (对于sigset_t和其他)
  • <fcntl.h> (对于O_*
  • <unistd.h> (为一堆东西)
  • <netdb.h> (关于各种网络功能和常量)
  • <stdio.h> (对于getline

您可能还需要定义一些特征宏(例如, _POSIX_C_SOURCE )来获得某些依赖于系统的功能,包括strerror_rpselect



Answer 2:

我假设的直接目标是让代码在所有的编译,而一旦这样做了,你会回去修改源,以便它的作品了两个平台上的方块。 这意味着,黑客是可接受的非常短期的; 他们会为您获得更多的便携性问题是什么的知识来正确固定。 (如果它的任何安慰,第一替代系统到一个地方最初开发的软件通常最难的是,在这之后,它通常变得更容易。)

尝试的第一件事是:

gcc *.c -std=gnu99 -lpthread

这告诉系统头文件定义许多大于符号-std=c99 。 (有一个关于这个话题,这是确定一些异议至少,如果添加。 -pedantic-std=c99编译,然后通过POSIX定义的标准的C头符号不被暴露,除非你还要求POSIX的支持-见下文既然你没有-pedantic ,这可能不是在编译的一个因素,在这种情况下悄悄移动到下一个建议,就是为将来移植到POSIX系统的基础。)

如果这是不足以让你回到正轨,那么你可能需要使用类似:

gcc *.c -std=gnu99 -D_XOPEN_SOURCE=700 -lpthread

这是说“与POSIX和X /打开功能相当于2008年POSIX提供给我。” 你可以尝试600和500的旧版本,但你可能不会需要在Linux上这样做。 在适当的时候,你可能会设置_XOPEN_SOURCE自动,或者通过配置头或者通过配置工具。 当你把事情在所有编译,在命令行上指定它是确定。 在适当的时候,你将使用一个makefile或等同于控制编制,而不是键入gcc在shell命令行。)

sigset_t中定义<signal.h> POSIX下。 因此,请求支持POSIX明确应该得到的东西再正确编译。 如果你仍然得到的类型,如sigset_t未申报的,那么就必须在Mac OS X中的标头,包括标准的头文件,如<signal.h> ,但它确实在Linux上的一些不相关的任务(因此不包括<signal.h> )。 这将需要的源代码审查。 然而,它是相对不太可能是必要的。



Answer 3:

您需要包括额外的头文件,因为不同的系统头包括其他不同的系统头。

此外,例如海湾合作委员会一直在努力,不包括报头,它不应该。

signalHandling.h包括#include <signal.h>其中sigset_t的定义?

编辑

与OP交谈后,似乎问题是一个编译/链接问题。 首先编译源到目标文件,然后将它们连接起来后,似乎已经解决了他们的问题。



文章来源: Compilation Error of C code on Linux (Same code compiles on OSX)