Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function name scoping in c

How does function name scoping work across multiple C files?

I'm porting a standard gnu toolchain project to iPhone OS, and using Xcode to do it.

The code builds through make, but not through xcode. when building through xcode, the linker complains that the same symbol (function) is defined in two objects. the code has two distinct source files that #include a common file between them. While... odd (to me at least), it seems to work for the standard toolchain. any ideas if this is something that's somehow handled differently through a standard makefile?

like image 235
kolosy Avatar asked Feb 24 '26 09:02

kolosy


1 Answers

All functions not marked static have global scope (they are dumped in a single namespace). Functions marked static are limited to the translation unit they are defined in.

You have a One Definition Rule violation.

One of your headers probably has a definition for a variable. E.g: in common.h you have

int foo = 42;

change it to:

extern int foo; // a declaration

and then create a common.c file where you put a definition:

int foo = 42;
like image 121
dirkgently Avatar answered Feb 25 '26 22:02

dirkgently



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!