Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C compiling - "undefined reference to"?

I am making a reliable data transfer protocol and have the function prototype

void tolayer5(int, char data[]);

With the structs

struct msg {
  char data[20];
};

struct pkt {
   int seqnum;
   int acknum;
   int checksum;
   char payload[20];
};

And when I call the function in this format:

tolayer5(A, packet.payload);

Where A is an int and packet.payload is a struct pkt, I get the error "undefined reference to 'tolayer5(int, char*)'. Can you help me see what I'm missing here?

void tolayer5(int AorB, char data[])
{
  int i;
  if (TRACE>2)
  {
     printf("TOLAYER5: data received:");
     for (i=0; i<20; i++)
        printf("%c",data[i]);
     printf("\n");
  }
}

Thank you all for helping with the original issue! :) When trying to fix that one, however, I ran into an infinite loop that I think has something to do with me addressing characters in an array incorrectly (it's been awhile since I've done C like this. Can you help me to find where I'm creating an infinite loop?

I have updated the above code to what I'm now working with. Notice the main changes have been to my function:

void tolayer5(int AorB, char data[])

And this line inside the function: printf("%c",msgReceived.data[i]); since now it's just:

printf("%c",data[i]);
like image 272
Programmer Avatar asked Oct 16 '13 01:10

Programmer


People also ask

How do you fix a undefined reference in C ++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do I fix linker error in C++?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What is undefined reference to printf?

This error is often generated because you have typed the name of a function or variable incorrectly. For example, the following code: #include <stdio.h> void print_hello() { printf ("Hello!\n"); } /* To shorten example, not using argp */ int main() { Print_hello(); return 0; }


2 Answers

seems you need to link with the obj file that implements tolayer5()

Update: your function declaration doesn't match the implementation:

      void tolayer5(int AorB, struct msg msgReceived)
      void tolayer5(int, char data[])

So compiler would treat them as two different functions (you are using c++). and it cannot find the implementation for the one you called in main().

like image 115
tristan Avatar answered Sep 24 '22 10:09

tristan


Make sure your declare the tolayer5 function as a prototype, or define the full function definition, earlier in the file where you use it.

like image 31
Phillip Ngan Avatar answered Sep 21 '22 10:09

Phillip Ngan