0

I'm trying to call a method implemented in Obj-C from C code as follows:

// MainViewController.m

- (void)Test
{
    [self outputLine:@"Called from MyCode.c"];
}
.
.
.
cFunc(id param);
.
.
.

// MyCode.c

void cFunc(id param)
{
    [param Test]; // compilation error: Parse issue: Expected expression
}

I guess it happens since the MainViewController is not declared in MyCode.c, but when I #include the MainViewController.h I get tons of other errors that suggests I'm totally wrong... How should I handle it correctly?

TIA

3
  • 1
    possible duplicate of Call Objective-C function from C function Commented Jul 11, 2013 at 13:47
  • @OneManCrew: That question covers a different issue, despite its title. I've edited it to make the title match the subject. I would be surprised, however, if there wasn't a duplicate of this somewhere. Commented Jul 11, 2013 at 20:55
  • although it really sounds trivial, even for an Obj-C newbie like me I was quite surprised not to find a similar answer, but maybe it's just my searching skills... Commented Jul 13, 2013 at 3:52

3 Answers 3

4

You should compile the MyCode.c file as Objective-C. Objective-C is a superset of C, but it's not true the other way around. You can't have Objective-C code interspersed with your "pure C" code if you are intending to compile it as C.

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

Comments

0

Check: using objc_msgSend to call a Objective C function with named arguments

void cFunc(id param) {
    objc_msgSend(param, sel_getUid("Test"));
}

But, as per the link above, this is dangerous for a few reasons, if your arguments don't fit in registers (i.e. floats, structs, blah blah).

The accepted way of doing this is to cast objc_msgSend:

void cFunc(id param) {
    // Now let's pretend that you want to send someFloat to your method

    void (*objc_msgSendTyped)(id self, SEL _cmd, float bar) = (void*)objc_msgSend;

    float someFloat = 42.f;
    objc_msgSendTyped(param, sel_getUid("Test"), someFloat);
}

4 Comments

Since you have no parameters this is an easy way to do it. Just don't use this if you have to pass parameters to the message, check @Ken's answer to the question I linked to.
Never call objc_msgSend() without casting it to the correct function pointer type.
That is true... but in this case it is fairly trivial because the OP's function was void anyway. I'll edit to reflect your suggestion though and to reemphasize the method in the link I listed.
+1 because now it's edited, showing the proper way, and anyways I don't like "it's impossible"-style answers in general, which now mine apparently is.
0

Just change your myCode.c to myCode.m :P

Don't be afraid to put C code in an Objective-C file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.