0

I have 3 directories, src, lib, and include. In include I have header file header3.h. Its code is as follows:

// header3.h
extern void change(int *a);

In lib I have file change4.c, which contains:

// change4.c
#include <stdlib.h>
#include "header3.h"

void change(int *a){
    int y=100;
    a=y;
}

In src I have file manipulate5.c, which contains:

// manipulate5.c
#include <stdio.h>
#include "header3.h"

int main(void){
     int x=10;
     printf("x is %d\n", x );
     change(&x);
     printf("x is now %d\n", x );
}

When I attempt to compile manipulate5.c with following command:

gcc -I ../include manipulate5.c`

when in directory src, I get the following error:

In function main:
manipulate5.c:(.text+0x2b): undefined reference to change

So how do I get manipulate5.c to properly work?

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Try `gcc -I ../include change4.c manipulate5.c`. You need to compile and link the function implementation as well. – Eugene Sh. Jun 08 '18 at 15:15
  • That did not work. Produces error that change4.c does not exist. So is the linking the solution? If so, how do I do that? –  Jun 08 '18 at 15:33
  • Does not exist? Is it in the same directory as manipulate5.c? – Eugene Sh. Jun 08 '18 at 15:35
  • No, and that is my question. Each file is in its own directory, and I am supposed to keep it that way. –  Jun 08 '18 at 15:36
  • 1
    Then either give it a path to it, or compile each file into `.o` by itself and then link them. – Eugene Sh. Jun 08 '18 at 15:37
  • I do not remember the syntax for compiling into .o, is it gcc -I ../include manipulate5.o manipulate5.c? –  Jun 08 '18 at 15:43
  • Refer to the docs. If you give `gcc` the `-c` option, it will only perform compilation without linking. – Eugene Sh. Jun 08 '18 at 15:43
  • How do I link .o files? –  Jun 08 '18 at 15:46
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – melpomene Jun 08 '18 at 16:36

0 Answers0