gcc 如何编译包含多个文件的C项目?'resolver'的多个定义

zed5wv10  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(157)

这是我的练习。我必须写一个函数“解析器(双a,双b,双c,双 * px 1,双 * px 2);“,它确定方程ax^2+bx*2+c=0的类型是1、2、3还是4(分别是两个解都是eal,解都是虚数,解不存在,只有一个解)。这个名为“ec 2g. 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

并使用命令“./mainEc 2g 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

我的“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 arguments\n");
    return 1;
  }

  while (fgets(buf, 100, fp)){
    count++;
  }
  printf("The file's got %i lines\n", 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 %i\n", caso);*/
  printf("Caso %i: %f, %f\n", 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, %f\n", caso, px1, px2);
    count++;
  }



/*  while (fscanf(fp, "%f", &aux) != EOF){
    if (count == 0) {
      a = aux;
      printf("El valor de a es: %i\n", a);
      count++;
    }
    else if (count == 1){
      b = aux;
      printf("El valor de b es: %i\n", b);
      count++;
    }
    else if (count == 2){
      c = aux;
      printf("El valor de c es: %i\n", c);
      count++;
    }
    else{
      count = 0;
      caso = resolver(a, b, c, &px1, &px2);
      printf("Caso %i: %f, %f\n", 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

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

jaql4c8m

jaql4c8m1#

您应该了解#include的作用:它会将其自身替换为您指定的文件。
在你的例子中,你把完整的ec 2g. c放在mainEc 2g. c中,然后编译它。现在,来自前者的所有文本都在后者中,正如你的链接器稍后告诉你的那样,这是重复的--因为你也单独编译ec 2g. c。
因此,永远不要在另一个文件中包含一个.c文件。在两个文件中都包含.h文件,它只定义了函数的优先级,而不是实际的函数代码。将这两个文件编译为.o文件(gcc -c),然后将它们组合成一个可执行文件。

相关问题