【问题标题】:"Undefined symbol <function> first referenced in file <file>" link error“未定义的符号 <function> 首先在文件 <file> 中引用”链接错误
【发布时间】:2011-02-18 00:13:28
【问题描述】:

我正在用 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 <stdio.h>
#include "/user/cse320/Projects/project04.support.h"
#include <string.h>

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 <= strlen(result))
    {
      quotient = (dividend / divisor);
      remainder = (dividend % divisor);
      //convert to ascii char
      result[count] = remainder;
      count++;
    }
  }

  formatdisplay ( result, display );

  strrev(result);

  if ( I >= 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 < strlen(str)-1; i++)
    {
      if ( str[i] = '\0') { str[i] = '0'; }
    }
  }
  if ( disp >= 0 )
  { 
    unsigned i = 0;
    for ( i; i < strlen(str)-1; i++)
    { 
      if ( str[i] = '\0') { str[i] = ' '; }
    }
  }   
} 

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

#include <stdio.h>
#include "/user/cse320/Projects/project04.support.h"

int main () {
  char Result1[32];
  int T = convert(10, 2, Result1, 1);
}

【问题讨论】:

  • 您确定您正确引用了project04.support.h 文件吗?函数声明后好像少了分号。

标签: c gcc


【解决方案1】:

是的,问题可能出在 Makefile 中:

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

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

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

【讨论】:

  • 这修复了错误,但它现在给了我另一个函数 strrev 的相同错误。而不是“未定义的符号转换”,我有“未定义的符号 strrev”。 Strrev 在 中声明,我已将其包含在内。不过,我可能在 proj04.support.c 文件中错误地声明了 strrev。
  • 您使用的是什么平台? C 和 POSIX 都没有将 strrev() 定义为库函数。如果您确定它已在您的&lt;string.h&gt; 中声明,但您遇到链接时间错误,那么您需要与其他一些库链接。 (Linux 上的 GNU C 库当然没有实现它或在头文件中声明它。)
【解决方案2】:

我遇到了几乎同样的问题。

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

我的问题是我在一个函数中遗漏了一个字母,因此声明和函数未对齐。例如:

 void isThreeOfAKind (void);
 void isThreeOfAkind {}

漏掉了大写的K,改写了小写的k。

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

我不知道它是否有帮助,但它可能就这么简单。

【讨论】:

    【解决方案3】:

    我不是专家,但正如 Andreas Joensson(根据函数名称判断,他正在编写与我完全相同的程序)所说的,当函数的声明和使用之间存在一些不匹配时,似乎会出现此问题。

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

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多