Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined symbol for architecture x86_64 in compiling C program [duplicate]

Tags:

c

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

like image 944
user3545251 Avatar asked Mar 16 '15 10:03

user3545251


1 Answers

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.

like image 68
Sourav Ghosh Avatar answered Oct 27 '22 11:10

Sourav Ghosh