2015-03-31 63 views
-1

我一直在試圖找出爲什麼我得到這個具體的錯誤時,GCC嘗試連接到目標文件一起:得到一個鏈接錯誤的C程序在GCC

Undefined      first referenced 
symbol        in file 
main        /usr/local/gcc_4.7.1/lib/gcc/sparc-sun-solaris2.10/4.7.1/crt1.o 
ld: fatal: symbol referencing errors. No output written to complex 
collect2: error: ld returned 1 exit status 

叫你正在查看的代碼被給予使用。目的是爲了能夠理解makefile並將所有這些文件鏈接在gcc上。我相信問題來自makefile或main.c。請幫助我一直試圖糾正這個小時。我一直在使用的命令是:

make makefile 
gcc complex.c -o complex -lm 

我已經徹底地搜索了網絡,找到是什麼原因造成的,但它們都沒有任何意義。下面我將把我想要鏈接在一起的三個文件放在一起:complex.c,main.c complex.h和我的makefile。

complex.c裏:

// Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers 
    /* 
    * Operators to process complex numbers 
    */ 

    /* User-defined complex number type */ 
    #include <stdio.h> 
    #include <math.h> 
    #include "complex.h" 


    /* 
    * Complex number input function returns standard scanning error code 
    * 1 => valid scan, 0 => error, negative EOF value => end of file 
    */ 
    int 
    scan_complex(complex_t *c) /* output - address of complex variable to 
      fill  */ 
    { 
      int status; 

      status = scanf("%lf%lf", &c->real, &c->imag); 
      if (status == 2) 
       status = 1; 
      else if (status != EOF) 
       status = 0; 

      return (status); 
    } 

    /* 
    * Complex output function displays value as (a + bi) or (a - bi), 
    * dropping a or b if they round to 0 unless both round to 0 
    */ 
    void 
    print_complex(complex_t c) /* input - complex number to display */ 
    { 
      double a, b; 
      char sign; 

      a = c.real; 
      b = c.imag; 

      printf("("); 

      if (fabs(a) < .005 && fabs(b) < .005) { 
       printf("%.2f", 0.0); 
      } else if (fabs(b) < .005) { 
       printf("%.2f", a); 
      } else if (fabs(a) < .005) { 
       printf("%.2fi", b); 
      } else { 
       if (b < 0) 
         sign = '-'; 
       else 
         sign = '+'; 
       printf("%.2f %c %.2fi", a, sign, fabs(b)); 
      } 

      printf(")"); 
    } 

    /* 
    * Returns sum of complex values c1 and c2 
    */ 
    complex_t 
    add_complex(complex_t c1, complex_t c2) /* input - values to add */ 
    { 
      complex_t csum; 

      csum.real = c1.real + c2.real; 
      csum.imag = c1.imag + c2.imag; 

      return (csum); 
    } 

    /* 
    * Returns difference c1 - c2 
    */ 
    complex_t 
    subtract_complex(complex_t c1, complex_t c2) /* input parameters */ 
    { 
      complex_t cdiff; 
      cdiff.real = c1.real - c2.real; 
      cdiff.imag = c1.imag - c2.imag; 

      return (cdiff); 
    } 

    /* ** Stub ** 
    * Returns product of complex values c1 and c2 
    */ 
    complex_t 
    multiply_complex(complex_t c1, complex_t c2) /* input parameters */ 
    { 
      complex_t cmul; 
      double a, b, c, d; 
      a = c1.real; 
      b = c1.imag; 
      c = c2.real; 
      d = c2.imag; 

      if ((b > 0 && d < 0) || (b < 0 && d > 0)) 
      { 
       cmul.real - (a*c) + (fabs(b)*fabs(d)); 
       cmul.imag = (a*d) + (b*c); 
      } 
      else if ((b>0 && d>0) || (b<0 && d<0)) 
      { 
      cmul.real = (a*c) - (b*d); 
      cmul.imag = (a*d) + (b*c); 
     } 
      return (cmul); 
    } 

    /* ** Stub ** 
    * Returns quotient of complex values (c1/c2) 
    */ 
    complex_t 
    divide_complex(complex_t c1, complex_t c2) /* input parameters  */ 
    { 
      complex_t cdiv; 
      double a, b, c, d; 
      a = c1.real; 
      b = c1.imag; 
      c = c2.real; 
      d = c2.imag; 

      if (b > 0 && d < 0) 
      { 
       cdiv.real = (a*c) - (fabs(b)*fabs(d))/((c*c) + (d*d)); 
       cdiv.imag = (a*d) + (b*c)/((c*c) + (d*d)); 
      } 
      else if (b>0 && d>0) 
      { 
       cdiv.real = (a*c) - (fabs(b)*fabs(d))/((c*c) + (d*d)); 
       cdiv.imag = ((-1*a*d) + (b*c))/((c*c) + (d*d)); 
     } 
      else if (b<0 && d<0) 
     { 
      cdiv.real = (a*c) + (fabs(b)*fabs(d))/((c*c) + (d*d)); 
      cdiv.imag = ((-1*a*d) + (b*c))/((c*c) + (d*d)); 
     } 
     else if (b<0 && d<0) 
     { 
      cdiv.real = (a*c) + (fabs(b)*fabs(d))/((c*c) + (d*d)); 
      cdiv.imag = ((a*fabs(d)) + (b*c))/((c*c) + (d*d)); 
     } 
      return (cdiv); 
    } 
    /* 
    * Returns absolute value of complex number c 
    */ 
    complex_t 
    abs_complex(complex_t c) /* input parameter      */ 
    { 
      complex_t cabs; 

      cabs.real = sqrt(c.real * c.real + c.imag * c.imag); 
      cabs.imag = 0; 

      return (cabs); 
    } 

的main.c

#include <stdio.h> 
#include "complex.h" 

int main (void) 
{ 
     complex_t com1, com2; 

     /* Gets two complex numbers */ 
     printf("Enter the real and imaginary parts of a complex number\n"); 
     printf("separated by a space> "); 
     scan_complex(&com1); 
     printf("Enter a second complex number> "); 
     scan_complex(&com2); 

     /* Forms and displays the sum */ 
     printf("\n"); 
     print_complex(com1); 
     printf(" + "); 
     print_complex(com2); 
     printf(" = "); 
     print_complex(add_complex(com1, com2)); 

     /* Forms and displays the difference     */ 
     printf("\n\n"); 
     print_complex(com1); 
     printf(" - "); 
     print_complex(com2); 
     printf(" = "); 
     print_complex(subtract_complex(com1, com2)); 

     /* Forms and displays the multiplication */ 
     printf("\n"); 
     print_complex(com1); 
     printf(" * "); 
     print_complex(com2); 
     printf(" = "); 
     print_complex(multiply_complex(com1, com2)); 

     /* Forms and displays the division */ 
     printf("\n"); 
     print_complex(com1); 
     printf("/"); 
     print_complex(com2); 
     printf(" = "); 
     print_complex(divide_complex(com1, com2)); 

     /* Forms and displays the absolute value of the first number  */ 
     printf("\n\n|"); 
     print_complex(com1); 
     printf("| = "); 
     print_complex(abs_complex(com1)); 
     printf("\n"); 

     return (0); 
} 

complex.h:

typedef struct { 
     double real, imag; 
} complex_t; 

int scan_complex(complex_t *c); 
void print_complex(complex_t c); 
complex_t add_complex(complex_t c1, complex_t c2); 
complex_t subtract_complex(complex_t c1, complex_t c2); 
complex_t multiply_complex(complex_t c1, complex_t c2); 
complex_t divide_complex(complex_t c1, complex_t c2); 
complex_t abs_complex(complex_t c); 

生成文件:

complex: main.o complex.o 
    gcc -o complex main.o complex.o -lm 
main.o: complex.h 
    gcc -c main.c 
complex.o: complex.h 
    gcc -c complex.c 
+0

你可能會調用'make'(即當將只是缺少'-f'選項來指定輸入文件。make'make -f makefile',或者簡單的'make'使用默認的'makefile'文件) – SleuthEye 2015-03-31 01:05:09

回答

2

只需鍵入make

更傳統的是拼寫主make文件「Makefile」(大寫M)。

如果你想命名makefile文件,比如Makefile.common,例如你需要明確地使用-f選項:例如make -f Makefile.com

我沒有看到make文件本身有什麼問題。

+1

'make'尋找'Makefile'和'makefile'。 – Barmar 2015-03-31 01:07:04

+0

從GNU Make手冊:默認情況下,當make查找makefile時,會按照以下順序嘗試以下名稱:GNUmakefile,makefile和Makefile。 – Barmar 2015-03-31 01:08:06

2

你不需要運行gcc命令,你只需要make。即

$ make 
$ ./complex 

有了您的gcc命令你想單獨編譯complex.c成二進制可執行文件,這當然是行不通的,因爲它不具備的主要功能。

如果您想直接使用gcc編譯二進制文件,只是包括源文件:

$ gcc -o complex main.c complex.c -lm 
+0

謝謝!我得到它的工作! – DeadWalker01 2015-03-31 01:24:24