# C: Undefined reference error only in some parts of same compilation unit

#### Sigdl

Joined Feb 20, 2022
11
What could be the possible causes for a call to some function to generate an undefined reference in one place but not other of *same file*?

Say I have the following code:

1 int functionA()
2 {
3 /*functionX();*/
4 }
5
6 int functionB()
7 {
8 functionX();
9 }

If I uncomment line 3, I get

file.c:3:undefined reference to functionX'

but line 8 never generates an error

Any ideas?

Grr

#### ApacheKid

Joined Jan 12, 2015
1,118
What could be the possible causes for a call to some function to generate an undefined reference in one place but not other of *same file*?

Say I have the following code:

1 int functionA()
2 {
3 /*functionX();*/
4 }
5
6 int functionB()
7 {
8 functionX();
9 }

If I uncomment line 3, I get

file.c:3:undefined reference to functionX'

but line 8 never generates an error

Any ideas?

Grr
How source errors are reported is not part of any standard, the compiler writers can decide how to do that. I suspect it is a simple matter of "before reporting error XYZ have we already reported XYZ?". So rather than flood the output with repetitive messages each error is reported just once - this is my suspicion anyway. Also in this case if you fix line 3 you will automatically fix line 8 so bringing line 8 to your attention explicitly is not much use.

#### MrChips

Joined Oct 2, 2009
28,136
Declare a function prototype ahead of all calls to the function.

int functionX();

This is usually placed at the top of the file.

#### Sigdl

Joined Feb 20, 2022
11
Thanks for the responses

> Also in this case if you fix line 3 you will automatically fix line 8 so bringing line 8 to your attention explicitly is not much use.

Line 8 is *not* commented and it doesn't generate any error

> Declare a function prototype ahead of all calls to the function

Prototype is defined. That's why line 8 doesn't generate any error