【发布时间】:2020-03-05 01:04:54
【问题描述】:
我正在尝试在 MacOS Catalina 上使用 GCC-7 编译一些代码。
GCC-7 是使用自制软件安装的 brew install gcc@7
代码如下:
#include <stdlib.h>
#include <math.h>
double distance(double *a, double *b, int d) {
double dist = 0;
for(int i = 0; i < d; i++) {
dist += pow(a[i]-b[i],2);
}
return sqrt(dist);
}
double *computeDistances (double *X, int n, int d) {
double *dist = malloc( (n-1) * sizeof(double) );
double *vp = X + (n-1)*d;
for(int i = 0; i < n-1; i++) {
dist[i] = distance(&(X[IDX(d,i,0)]), vp, d);
}
return dist;
}
我正在使用gcc-7 -Iinc/ -o lib/test.o -c src/test.c进行编译
输出是:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/wait.h:110:0,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:66,
from src/test.c:1:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/resource.h: In function 'getiopolicy_np':
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/resource.h:443:34: error: expected declaration specifiers before '__OSX_AVAILABLE_STARTING'
int getiopolicy_np(int, int) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
^~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/resource.h:449:39: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__OSX_AVAILABLE_STARTING'
int setiopolicy_np(int, int, int) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libkern/_OSByteOrder.h:66:0,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_endian.h:130,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/i386/endian.h:99,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/endian.h:35,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/wait.h:186,
from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:66,
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/resource.h:443:1: error: parameter name omitted
src/test.c:21:1: error: expected '{' at end of input
}
^
在我不包含 stdlib.h 的地方它可以工作。我认为头文件有问题。
【问题讨论】:
-
代码怎么调用?如果 not 包含必要的头文件可以解决问题,那么问题肯定是代码中的错误。
-
@Lundin 它没有解决问题,它给出了这些警告
src/test.c:15:19: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration] -
在
#include <stdlib.h>之前添加#include <stdio.h>— 或几乎任何其他标准标题。仅当<stdlib.h>是第一个系统标头时,您才会收到该错误。我还没弄清楚为什么。我在关于无法在 Catalina 上编译 C 程序的 Q&A 评论中注意到了这一点。 -
@JonathanLeffler 如果不是在其他地方值得回答。
-
在 gcc 7,4.0 / Mint-19 中不会发生
标签: c macos gcc macos-catalina gcc7