In my Xcode project I made a standalone header file with the following C utility function:
#ifndef FOO
#define FOO
CGFloat DistanceBetweenTwoPoints(CGPoint p1, CGPoint p2)
{
CGFloat dx = p2.x - p1.x;
CGFloat dy = p2.y - p1.y;
return sqrt(dx*dx + dy*dy);
};
#endif
Even with the preprocessor directives, if I try to import or include that header file in multiple locations, I receive the following error complaining about duplicate symbols:
linker command failed with exit code 1
Is there a different way I can achieve this effect? This question is out of curiosity more than anything else.
Thanks
Put your function body in a
.cfile and the function declaration (aka prototype) in the.hwith those#ifndef, etc. Then useincludeto import the header file where you need the function.Remember to check the target membership of the
.cfile, otherwise it won’t be compiled.For a small function like that you can declare it
inlineand just use the header file:The compiler will replace your function calls with the function code without actually building and linking a new object file. No more duplicated symbols.