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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
Tricky
  • 7,025
  • 5
  • 33
  • 43
  • SOrry, I should have said this is for an iOS project using the XCode 4.5 IDE – Tricky Oct 18 '12 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. – Dietrich Epp Oct 18 '12 at 17:58

3 Answers3

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

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

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46
-4

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

Daniel
  • 577
  • 2
  • 12