如何编译包含多个文件的 C 项目?"解析器"的多重定义 首先在这里定义



这是我的练习。我必须写一个函数";解析器(双a、双b、双c、双*px1、双px2("它确定方程ax^2+bx2+c=0是否为类型1、2、3或4(分别是两个解都是实的,解是虚的,解不存在,并且只有一个解(。这个名为";ec2g.c";做它的工作:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ec2g.h"
int resolver(double a, double b, double c, double* px1, double *px2){
double disc = b*b - 4*a*c; /*discriminant helps us know the particular case*/
int res = 0; /*determines the case we're facing*/
if (disc >= 0 && a != 0){
*px1 = (-b + sqrt(disc))/(2*a);
*px2 = (-b - sqrt(disc))/(2*a);
res = 1;
}
else if (disc < 0){
*px1 = -b/(2*a);
*px2 = sqrt(-(disc))/(2*a);
res = 2;
}
else if (a == 0 && b == 0 && c != 0){
*px1 = 0;
*px2 = 0;
res = 3;
}
else if (a == 0 && b != 0){
*px1 = -c/b;
*px2 = sqrt(-1.0);
res = 4;
}
return res;
}

现在,我需要另一个名为";mainEc2g.c";它从一个名为"的文件中读取输出;ecuaciones.txt";。。。


1.0 -2.0 1.0
-1.0 -2.0 1.0
3.0 -2.0 -8.0
-3.0 -2.0 -8.0
2.0 3.0 5.0
0.0 0.0 4.0
2.0 0.0 6.0
0.0 4.0 11.0

并用命令"产生该输出/mainEc2g ecuaciones.txt";。。。


Caso 1: 1.000000, 1.000000
Caso 1: -2.414214, 0.414214
Caso 1: 2.000000, -1.333333
Caso 2: -0.333333, 1.598611
Caso 2: -0.750000, 1.391941
Caso 3: 0.000000, 0.000000
Caso 2: -0.000000, 1.732051
Caso 4: -2.750000, -nan

My";mainEc2g.c";文件是:

#include <stdio.h>
#include <stdlib.h>
#include "ec2g.c"
int main(int argc, char* argv[]){
FILE *fp = fopen(argv[1], "r");
double a, b, c = 0.0; /*Equation coefficients*/
double px1, px2; /*Equation results*/
/* double aux; Helps us store the double we're reading*/
char  buf[100]; /*Line's maximum size*/
/*char *token;*/
int count; /*helps us to get the coefficients*/
int size;
int caso; /*helps us determine the case we're facing*/
if (fp == NULL || argc > 2){
printf("No such file or many argumentsn");
return 1;
}
while (fgets(buf, 100, fp)){
count++;
}
printf("The file's got %i linesn", count);
size = count;
/*Now, reset count to 0 because we'll need it to get a, b, c and then make use of "resolver"*/
count = 0;
fclose(fp);
fp = fopen(argv[1], "r");
fscanf(fp, "%lf", &a);
fscanf(fp, "%lf", &b);
fscanf(fp, "%lf", &c);
/*printf("a, b, c = %f, %f, %f n", a, b, c);*/
caso = resolver(a, b, c, &px1, &px2);
/*printf("This is case %in", caso);*/
printf("Caso %i: %f, %fn", caso, px1, px2);
count++;
while(count < size){
fscanf(fp, "%lf", &a);
fscanf(fp, "%lf", &b);
fscanf(fp, "%lf", &c);
/*printf("a, b, c = %f, %f, %f n", a, b, c);*/
caso = resolver(a, b, c, &px1, &px2);
printf("Caso %i: %f, %fn", caso, px1, px2);
count++;
}


/*  while (fscanf(fp, "%f", &aux) != EOF){
if (count == 0) {
a = aux;
printf("El valor de a es: %in", a);
count++;
}
else if (count == 1){
b = aux;
printf("El valor de b es: %in", b);
count++;
}
else if (count == 2){
c = aux;
printf("El valor de c es: %in", c);
count++;
}
else{
count = 0;
caso = resolver(a, b, c, &px1, &px2);
printf("Caso %i: %f, %fn", caso, px1, px2);
}
}*/
return 0;
}

然而,当我使用命令gcc -ansi -pedantic -Wall -Wextra -Werror mainEc2g.c ec2g.c -o mainEc2g -lm进行编译时,会弹出以下错误:

/usr/bin/ld: /tmp/ccePNwsS.o: in function `resolver':
ec2g.c:(.text+0x0): multiple definition of `resolver'; /tmp/ccO6EnFU.o:mainEc2g.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

我该如何解决?顺便说一下,我确实有一个名为";ec2g.h";包含以下内容:

#ifndef _EC2G_H_
#define _EC2G_H_
int resolver(double a, double b, double c, double* px1, double *px2);
#endif

我什么都试过了。。。但似乎做不好。我希望我把自己解释清楚

您应该意识到#include的作用:它实际上用您指定的文件替换自己。

在您的情况下,您将完整的ec2g.c放在您的mainEc2g.c中,然后编译它。现在,前者的所有文本都在后者中,正如链接器稍后告诉你的那样,这是重复的,因为你还单独编译ec2g.c。

所以,永远不要在另一个文件中包含.c文件。包括.h,它只定义函数优先级,而不是实际的函数代码。将这两个文件编译为.o文件(gcc-c(,然后将它们组合为一个可执行文件。

相关内容

最新更新