我正在尝试在linux上编译一些我知道在OSX上编译的代码,但是我遇到了一些问题。
所有文件都有名为.h的标题,所有文件都在同一目录中。我正在编译:
gcc *.c -std=c99 -lpthread
虽然这段代码在OSX上编译,但我在Ubuntu安装上遇到了一堆奇怪的链接器错误。我错过了一些编译器选项吗?这是默认的Ubuntu服务器安装,其中安装了其他软件包gcc
和build-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]
答案 0 :(得分:11)
您可能遗漏了一些在OS X上隐式引入的#include
,但在Linux上却没有。
从错误消息判断,您可能至少缺少包括:
<signal.h>
(适用于sigset_t
及其他人)<fcntl.h>
(适用于O_*
)<unistd.h>
(对于一堆东西)<netdb.h>
(针对各种网络功能和常量)<stdio.h>
(适用于getline
)您可能还需要定义一些功能宏(例如_POSIX_C_SOURCE
)以获取某些与系统相关的功能,包括strerror_r
和pselect
。
答案 1 :(得分:5)
我假设最直接的目标是让代码完全编译,并且一旦完成,你将返回并修改源代码,以便在两个平台上开箱即用。这意味着短时间内可以接受黑客攻击;当您了解可移植性问题时,它们将得到妥善修复。 (如果有任何安慰,那么最初开发软件的第一个替代系统通常是最难的;之后,通常会变得更容易。)
首先要尝试的是:
gcc *.c -std=gnu99 -lpthread
这告诉系统头文件定义比-std=c99
更多的符号。 (对此主题有一些不同意见,这是可以的。至少,如果将-pedantic
添加到-std=c99
编译中,除非您还请求POSIX,否则不会公开POSIX定义的标准C标头中的符号。支持 - 见下文。由于你没有-pedantic
,这可能不是编辑中的一个因素,在这种情况下,悄悄地转向下一个建议,这是未来POSIX系统可移植性的基础。)
如果这不足以让你重回正轨,那么你可能需要使用类似的东西:
gcc *.c -std=gnu99 -D_XOPEN_SOURCE=700 -lpthread
这说“向我提供与POSIX 2008相对应的POSIX和X / Open功能”。对于旧版本,您可以尝试600和500,但您可能不需要在Linux上执行此操作。在适当的时候,您可能会通过配置标头或配置工具自动设置_XOPEN_SOURCE
。在你完成编译的过程中,在命令行中指定它是可以的。在适当的时候,您将使用makefile
或等效项来控制编译,而不是在shell中键入gcc
命令行。)
sigset_t
在POSIX下的<signal.h>
中定义。因此,明确请求POSIX支持应该让事情再次正确编译。如果你仍然得到诸如sigset_t
未声明的类型,那么Mac OS X上必须有一个包含标准标题的标题,例如<signal.h>
,但它在Linux上执行一些不相关的任务(因此不包括<signal.h>
)。这将需要源代码审查。但是,它不太可能是必要的。
答案 2 :(得分:2)
您需要包含其他头文件,因为不同的系统头包含其他不同的系统头。
另外,例如gcc一直在努力不包括它不应该包含的标题。
signalHandling.h
是否包含#include <signal.h>
,其中定义了sigset_t?
修改强>
与OP交谈后,问题似乎是编译/链接问题。首先将源代码编译成目标文件,然后将它们链接起来似乎已经解决了它们的问题。