9

I have a simple program as demo_use.c

#include "libhello.h"

int main(void) {
 hello();
 return 0;
}

libhello.h

void hello(void);

libhello.c

#include <stdio.h>

void hello(void) {
  printf("Hello, library world.\n");
}

I have used the command in terminal as

gcc demo_use.c -o test

error Undefined symbols for architecture x86_64: "_hello",

referenced from: _main in ccZdSQP3.o

ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

user3545251
  • 445
  • 3
  • 6
  • 15
  • 2
    Use: `gcc demo_use.c libhello.c -o test` , you need both files. Note that [make](http://www.gnu.org/software/make/) is a better option for multiple files. – David Ranieri Mar 16 '15 at 10:02

1 Answers1

13

You need to compile both the source files together to generate the binary. use

gcc demo_use.c libhello.c -o test

Otherwise, the definition of hello() function will be absent. So, at linking time, linker will throw undefined symbol error.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261