1

Simple question:

I have created a bunch of C helper routines and am trying to include them in a project. I had assumed something like the following would be ok.

MyFuncs.h

typedef struct { float n; } MyStruct; float Operation(MyStruct ms); 

MyFuncs.m

#import "MyFuncs.h" float Operation(MyStruct ms) { return ms.n * ms.n; } 

However, I'm getting a linker error "Undefined symbols for architecture i386" and "Operation(MyStruct) referenced from x"

Is there some other way that header/implemenation C files need to be set-up to work?

Note: This is for an iOS project using Xcode 4.5.

2
  • SOrry, I should have said this is for an iOS project using the XCode 4.5 IDE Commented Oct 18, 2012 at 17:44
  • I've taken the liberty of editing your comment into the question -- you're free to edit your own questions if you realize you left something out. Commented Oct 18, 2012 at 17:58

3 Answers 3

3

No, there is no problem with using pure C. Usually, the source files will be named *.c instead of *.m, but renaming *.c to *.m shouldn't cause errors.

However, there's a clue:

Operation(MyStruct) referenced from x

If the linker knows the type of the function parameters, it's because you're calling the function from C++ code. You will have to put extern "C" { ... } in the header as follows:

#ifdef __cplusplus extern "C" { #endif typedef struct { float n; } MyStruct; float Operation(MyStruct ms); #ifdef __cplusplus } #endif 

If you don't have any C++ in your project (including Objective-C++ *.mm files), then this isn't your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Make sure also to put extern "C" inside an #ifdef __cplusplus
@asveikau: Righto. I've been doing it all automatically for so long it's easy to forget...
Great spot! Yes, i'm migrating a couple of C++ classes to C to simplify my project a little.
0

If it's pure c then "MyFuncs.c" instead of "MyFuncs.m" will be better.

Comments

-4

Call the implementation file with extension .mm like MyFuncs.mm.

1 Comment

Objective-C++ (.mm) and C (.c) are very different languages.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.