Snowman
Snowman

Reputation: 32071

Importing header with C functions into Xcode proj?

I have a .h file with a single C style function:

void myFunc(NSArray *array) {
    ...
}

I want this function to be available in every file, so I #import "Functions.h" into my App-Prefix.pch file.

However, this gives me a compilation error ld: 38 duplicate symbols for architecture i386. What's the proper way to do this?

Upvotes: 0

Views: 155

Answers (1)

cutsoy
cutsoy

Reputation: 10251

You should not implement the actual C function body in a header file, just like you shouldn't implement Objective-C methods in header files.

myfunc.h

    void myFunc(NSArray *array);

myfunc.m

    void myFunc(NSArray *array) {
        ...
    }

Obviously, you should also add a #ifndef at the top of your header and if you decide not to use any Objective-C in myfunc.m, you can just as well rename it to myfunc.c.

Update:
The reason for this is that header files are collected from within multiple entry points in your program (multiple files). Then your compiler gets a little dizzy and starts wondering what function body you actually want to call (since there are multiple). A more obvious example would be declaring the same function name twice in a single C file. At the end, you should use header files to "describe" your function (add documentation and all that) and then use your main (.m) or C file (.c) to instruct the compiler what to do when that function gets called.

Upvotes: 2

Related Questions