1
% gcc -std=c89 -o main main.c
/tmp/cccuTfCk.o: In function `main':
main_c11e5.c:(.text+0x3c): undefined reference to `swap'
collect2: error: ld returned 1 exit status

Here is my function file

#include <stdio.h>



void swap(int *p, int *q){

int a;
int b;

*p = a;
*q = b;

*p = b;
*q = a;

return p ,q;
}

this is my main file

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

int main(){

int i;
int j;

i = 9;
j = 100;

printf("i value: %i\n j value: %i\n", i, j);

swap (&i,&j);

printf("i value: %i\n",i);
printf("j value: %i\n",j);

return 0;
}

and I created

// swap.h

void swap(int *p, int *q);

I just have no idea why I keep getting a error like that.

I thought I completed pointing each others.

is compiling weird or my coding is unclear?

Thank you.

Jace Cho
  • 27
  • 4
  • 4
    I think you forgot to compile `swap.c` – Paul Ogilvie Oct 22 '18 at 16:50
  • 2
    Turn warnings on: swap can't return a value (and `return p ,q;` will only return `q`). – Paul Ogilvie Oct 22 '18 at 16:50
  • 3
    What is `return p ,q;` supposed to do? In a `void` function, no less. – Christian Gibbons Oct 22 '18 at 16:50
  • your swap is set to have a void return, and you return p and q- which is unnecessary and impossible. – H.cohen Oct 22 '18 at 16:51
  • You need to compile both source files and link the corresponding object files, or specify both source files in the linking command line. Your function file (presumably `swap.c`) needs to include the header `#include "swap.h"` so that you get the proper cross-checking. Headers are the glue that hold everything together and provide the cross-checking necessary to ensure that the definition of the function matches what the users of the function have been told to expect. – Jonathan Leffler Oct 22 '18 at 16:53
  • 2
    also `-std=c89`... explicitly choosing 29-year-old obsolete standard revision – Antti Haapala -- Слава Україні Oct 22 '18 at 16:55
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?: Failure to link against appropriate libraries/object files or compile implementation files](https://stackoverflow.com/a/12574400). – Raymond Chen Oct 22 '18 at 16:57
  • You can't return two values in c – suvojit_007 Oct 22 '18 at 18:53

2 Answers2

3

There are multiple errors.

1) You need to include swap.h in second file where you have defined swap function.

2) You are compiling only main.c file but swap function is defined in another file. This is the reason, you are getting undefined reference to swap function. You need to compile both the files (gcc -std=c89 -o main main.c name_of_second_file)

3) You declared swap() as void. That means you shouldn't return any value from the function and there is no need to return also as you are passing addresses of i and j to swap function.

kadina
  • 5,042
  • 4
  • 42
  • 83
1
void swap(int *p, int *q){

    int tmp;

    tmp = *p;
    *p = *q;
    *q = tmp;

}

You cant return anything, because you declared as a void return function

aizhan_maksatbek
  • 69
  • 1
  • 1
  • 9