ysimhony
ysimhony

Reputation: 35

extern function in header file vs source file

I have this C compilation issue.

It took me while to understand the root cause for the issue. Although, now that i have intercepted the issue, I am not sure I fully understated the full C compilation flow logic.

I have two directoriess: inc and src

In the inc directories I have one file: test.h In the src directories I have two file: test.c and main.c

The test.c file implements a function, let call it test(), and the main.c file call this test() function.

I declared this function in the test.h file

Now, I included this test.h file only in the main.c file, in order to let the main.c file to "see" the declaration of the test() function.

when compiling:

g++ -o test.o -c test.c
g++ -o main.o -c main.c
g++ -o test test.o main.o

In the last command I get an error of "undefined reference to 'test'

After debugging this issue, I found out that the missing include of the test.h file in the test.c file resolves the issue.

In other words, I need to includes the test.h file in both source files - main.c and test.c

My question is, why? Is it not enough to include the header file, test.h file, only in the main.c file, in order to let it "see" the function declaration, and in linkage phase the compiler would "know" to associate the test() function used in the main.c file to its implementation in the test.c file?

I thought that the declaration of the test() function in the test.h file makes it "extern" declaration, and therefore it will signal the compiler to find the function implementation in linkage phase

Upvotes: 0

Views: 544

Answers (2)

Milind Dumbare
Milind Dumbare

Reputation: 3244

I don't think there should be any problem in just adding test.h in main.c and not in test.c. Please check your setup again.

You can either declare the function in test.h and include test.h in main.c

or declare the function as extern in main.c itself. In both ways linker has to do the job of finding definition of test()

Include files are usually to separate interfaces from implementations.

Upvotes: 0

Valeri Atamaniouk
Valeri Atamaniouk

Reputation: 5163

The issue is because your prototype doesn't match implementation.

If you would be using C compiler, your would get interesting run-time errors due to wrong parameter and return value handling, because the generated symbol typically doesn't include parameter information.

With C++ the compiler generates a method signature that depends on parameter types. So two functions with the same name but different arguments will produce two distinct symbols, so linker would not confuse them.

So fix your prototype and it would work...

Upvotes: 1

Related Questions