“未定义的符号首先在文件中引用”链接错误

我正在用C语言编写我的第一个程序; 我已经设法解决了大部分语法错误,但是当gcc尝试将目标文件链接在一起时,我遇到了一个奇怪的错误。 它打印完全如下:

gcc -o proj04.support.o proj04.driver.o Undefined first referenced symbol in file convert proj04.driver.o 

我环顾四周寻找一些答案,但没有一个对我有意义。 我将发布我正在使用的文件来制作下面的程序,如果你有答案,我会非常感谢你的帮助。 这似乎是一个非常基本的错误,所以它可能是我没有做的傻事。

Makefile (首先发布,因为我怀疑问题在这里)

 # Comments # Comments proj04: proj04.support.o proj04.driver.o gcc -o proj04.support.o proj04.driver.o proj04.support.o: proj04.support.c gcc -Wall -c proj04.support.c proj04.driver.o: proj04.driver.c gcc -Wall -c proj04.driver.c 

头文件 (由教授提供,不可更改,一行长):

 int convert( int, unsigned, char[], int ) 

实施文件

 #include  #include "/user/cse320/Projects/project04.support.h" #include  void formatdisplay( char[], int ); int convert( int I, unsigned base, char result[], int display ) { int quotient, dividend, remainder; const int divisor = base; int count = 0; char ending[] = " base "; dividend = I; remainder = 0; quotient = 1; while (quotient != 0) { if (count = 0 ) { result[0] = '+'; } if ( I < 0 ) { result[0] = '-'; } printf( "%s" , strcat (result, ending)); } void formatdisplay ( char str[], int disp ) { if ( disp < 0 ) { unsigned i = 0; for ( i; i = 0 ) { unsigned i = 0; for ( i; i < strlen(str)-1; i++) { if ( str[i] = '\0') { str[i] = ' '; } } } } 

驱动程序文件 (尚未真正实现)

 #include  #include "/user/cse320/Projects/project04.support.h" int main () { char Result1[32]; int T = convert(10, 2, Result1, 1); } 

是的,问题可能在Makefile中:

 proj04: proj04.support.o proj04.driver.o gcc -o proj04.support.o proj04.driver.o 

gcc-o选项接受一个参数,即输出文件名。 所以这是要求gcc链接文件proj04.driver.o ,生成proj04.driver.o的输出文件。

gcc -o proj04 proj04.support.o proj04.driver.o应该更好用。

我不是专家,但作为Andreas Joensson(根据函数名称判断我编写完全相同的程序)上面说的这个问题似乎发生在声明和使用函数之间存在一些不匹配时。

您的函数convert被声明为返回一个int,但我找不到返回值。 这可能是问题所在。

我有几乎相同的问题。

 Undefined first referenced symbol in file isThreeOfAKind /var/tmp//ccIQWbaj.o 

我的问题是我错过了我的一个函数中的一封信,所以声明和函数错位了。 例如:

  void isThreeOfAKind (void); void isThreeOfAkind {} 

错过了大写字母K,而是写了小写字母k。

在我将k更改为大写K之后,它编译得很好。

我不知道它是否有帮助,但它可能就像那样简单。