我有一台装有 Ventura 13.5.1(intel proc)的 Mac。我需要在 mac 中使用 usc gcc 而不是 clang,我该怎么做?
现在我的 mac 上有这样的东西:
λ ~/ gcc -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
λ /usr/bin/ gcc -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
这是代码,可以正常编译。但在带有 gcc 的 Linux 上,它会引发浮点转换错误。
#include <stdio.h>
#include <math.h>
#define ERROR_OK 0
#define ERROR_IO 1
#define ERROR_RANGE 2
#define EPSILON 0.00001
int are_points_collinear(float x1, float y1, float x2, float y2, float x3, float y3)
{
return fabs((y3 - y2) * (x2 - x1) - (y2 - y1) * (x3 - x2)) < EPSILON;
}
float area(float x_a, float y_a, float x_b, float y_b, float x_c, float y_c)
{
double s;
s = 0.5 * fabs((x_b - x_a) * (y_c - y_a) - (x_c - x_a) * (y_b - y_a));
return s;
}
int main(void)
{
double x_a, y_a, x_b, y_b, x_c, y_c;
double s;
printf("Enter coordinates of point A (x, y): ");
if (scanf("%lf %lf", &x_a, &y_a) != 2)
return ERROR_IO;
printf("Enter coordinates of point B (x, y): ");
if (scanf("%lf %lf", &x_b, &y_b) != 2)
return ERROR_IO;
printf("Enter coordinates of point C (x, y): ");
if (scanf("%lf %lf", &x_c, &y_c) != 2)
return ERROR_IO;
if (are_points_collinear(x_a, y_a, x_b, y_b, x_c, y_c))
return ERROR_RANGE;
s = area(x_a, y_a, x_b, y_b, x_c, y_c);
printf("S = %lf\n", s);
return ERROR_OK;
}
我用
编译gcc -std=c99 -Wall -Werror -Wpedantic -Wfloat-equal -Wfloat-conversion -Wextra -o app.exe main.c -lm
如果由于可能丢失精度而将
-Wfloat-conversion
转换为 double
,则 float
选项会发出警告,并且 -Werror
会将所有警告转换为错误。触发此警告/错误是因为您的函数接受 float
参数,但传入 double
参数。
通常不应使用
float
代替 double
,除非您有特定原因这样做,因此将所有 float
声明更改为 double
。
int are_points_collinear(double x1, double y1, double x2, double y2, double x3, double y3)
{
return fabs((y3 - y2) * (x2 - x1) - (y2 - y1) * (x3 - x2)) < EPSILON;
}
double area(double x_a, double y_a, double x_b, double y_b, double x_c, double y_c)
{
double s;
s = 0.5 * fabs((x_b - x_a) * (y_c - y_a) - (x_c - x_a) * (y_b - y_a));
return s;
}