开发者

"Undefined symbol <function> first referenced in file <file>" link error

开发者 https://www.devze.com 2023-02-11 00:20 出处:网络
I\'m writing my first program in C for a class; I\'ve managed to even out most of the syntax errors, but I\'m getting a strange error when gcc tries to link the object files together. It prints exactl

I'm writing my first program in C for a class; I've managed to even out most of the syntax errors, but I'm getting a strange error when gcc tries to link the object files together. It prints exactly like below:

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

I've looked around for a few answers, but none really make sense to me. I'll post the files I'm using to make the program below, and if you've got the answer I would really appreciate the help. It seems to be a pretty basic error, so it's probably something silly I didn't do.

Makefile (posting开发者_JAVA技巧 this first because I suspect the issue is here)

# 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

Header file (provided by the professor, unchangeable, one line long):

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

Implementation file

#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] = ' '; }
    }
  }   
} 

Driver file (not really implemented yet)

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

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


Yes, the problem is probably in the Makefile:

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

The -o option to gcc takes an argument, the output filename. So this is asking gcc to link the file proj04.driver.o, producing an output file of proj04.support.o.

gcc -o proj04 proj04.support.o proj04.driver.o should work better.


I had the almost the same problem.

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

My problem was that I had missed on a letter in one of my functions, so the declaration and the function misaligned. Ex:

 void isThreeOfAKind (void);
 void isThreeOfAkind {}

Missed the uppercase K, and wrote lowercase k instead.

After I changed the k to a uppercase K it compiled fine.

I dont know if it helps, but it could be something as easy as that.


I am no expert but as Andreas Joensson (who, judging by function name is writing the exact same program I am) says above this problem seems to occur when there's some mismatch between declaration and use of a function.

Your function convert is declared as returning an int but I find no return value. That might be the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消