Eric Brotto
Eric Brotto

Reputation: 54281

Calling a C function from within a function in a .mm file

This is one of those problems that is a bit vague and so difficult to pinpoint the cause. I'll try to keep it simple.

I've created a C class with a header like this:

C_Class.h

void do_something(void);

C_Class.c

void do_something(void)
{
}

Then I have a .mm file that was working perfectly fine until within one of its functions I added my C sub_routine which I created earlier:

  #import "C_Class.h" // included in the .h file 


 // then in the .mm file

 - (void)working_function{
  .... some working code ......
  do_something();

  }

The problem is I get this error:

 -[MyDotMMfile working_function] in MyDotMMfile.o
 Symbol(s) not found
 Collect2:ld returned 1 exit status

This questioned has been answered elsewhere, but the response doesn't seem relevant to my situation. I'm working in XCode and have stopped running my app, rebuilt it and still get the same error. Since I'm new to programming I have a feeling it has to do with how I'm calling my C functions.

Any help?


EDIT 1

If anyone is feeling so altruistic they may download the actually files here.

Upvotes: 2

Views: 1607

Answers (2)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

The .mm file is looking to call a mangled version of the function name. You need either __BEGIN_DECLS and __END_DECLS around the C function declarations seen by the C++-compiled file, or you need to do the equivalent yourself. The idea is to mark those function declarations as extern "C" when seen by an (Obj-)C++ compiler, but not when seen by any other sort of compiler.

Upvotes: 7

Eric Brotto
Eric Brotto

Reputation: 54281

Somehow I managed to figure out the problem. It had to do with a missing file. The solution I used I found here.

Upvotes: 0

Related Questions