Reputation: 252
Before posting this question I've read hundreds of Google search result pages, searching solution of this problem in vain. My program can't be more simple — these are the only 3 files:
test.h
#ifndef SYMBOL
#define SYMBOL
#include <stdio.h>
void output(void);
#endif
test.c
#include "test.h"
void output(void)
{
printf("%s\n", "Hello world");
}
untitled.c
#include <stdio.h>
#include "test.h"
void main()
{
output();
return;
}
I use terminal and enter the following command to compile:
gcc -o aaa untitled.c
Output should print the "Hello world", but instead it keeps throwing this error:
Undefined symbols for architecture x86_64:
"_output", referenced from:
_main in untitled-00f904.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I spent the whole day searching for solution, tried all the suggestions as possible but none helped.
Something notable: If I change the file included in untitled.c (#include "test.h"
to #include "test.c"
), compiler goes without problem.
Can someone please explain to me why, and how to resolve it?
Upvotes: 3
Views: 4605
Reputation: 122391
You have to compile and link both source files into the executable; the linker is telling you it cannot find the output()
function defined in test.c
:
$ gcc -o aaa untitled.c test.c
Upvotes: 4