开发者

Linker error using extern "C" in Objective-C code

I'm trying to create some utility functions that can be called from both Objective-C and C++ code in an iPhone application. I have third party C++ classes that cannot be compiled as ObjectiveC++ (.mm). I hav开发者_C百科e a header file declaring my functions, then a .c file defining them. I've tripled checked for spelling errors, but for some reason my linker is NOT able to find the definition of any of the functions.

Here is the header for the C helper functions:

#ifndef FILE_LOADER_H
#define FILE_LOADER_H

#if __cplusplus
extern "C" {
#endif

void * loadDataFromFile(const char * szFilename, bool bDocument);
void * loadImageFromFile(const char * szFilename, bool bDocument);
void loadMeshFromFile(const char *szFilename, void* pMesh);

#if __cplusplus
}   // Extern C
#endif

#endif

And here is the .c file:

#include "FileLoader.h"
#include <objc/objc.h>
#include <objc/message.h>
#include <Foundation/Foundation.h>


void * loadDataFromFile(const char * szFilename, bool bDocument)
{
    Class loaderClass = NSClassFromString(@"Loader");
    void * pReturnVal = objc_msgSend(loaderClass,   NSSelectorFromString(@"loadDataFromFile:document"), szFilename, bDocument);
    return pReturnVal;
    //return [Loader loadDataFromFile:szFilename document:bDocument];
}
void * loadImageFromFile(const char * szFilename, bool bDocument)
{
    Class loaderClass = NSClassFromString(@"Loader");
    void * pReturnVal = objc_msgSend(loaderClass, NSSelectorFromString(@"loadImageFromFile:document"), szFilename, bDocument);
    return pReturnVal;
    //return (void*)[Loader loadImageFromFile:szFilename document:bDocument];
}
void loadMeshFromFile(const char *szFilename, void* pMesh)
{
    Class loaderClass = NSClassFromString(@"Loader");
    objc_msgSend(loaderClass, NSSelectorFromString(@"loadMeshFromFile:mesh"), szFilename, pMesh);
    //[Loader loadMeshFromFile:szFilename mesh:pMesh];
}

I've tried compiling with and without the extern "C", but with the same linker error result.

Can anyone shed some light on what I'm doing wrong?

Linker Error:

Undefined symbols for architecture i386:
"loadMeshFromFile(char const*, void*)", referenced from:

UPDATE: I've seemed to solve the problem by compiling my source file as objective C, even though there was no actual ObjectiveC code, can anyone tell me why the above source file won't compile as C code?


use the following construct to specify c linkage for c and c++ translations:

#if !defined(__cplusplus)
#define MONExternC extern
#else
#define MONExternC extern "C"
#endif
// declare loadMeshFromFile
MONExternC void loadMeshFromFile(char const*, void*);

this declaration will be compatible with c, objc, c++, objc++.


Double check your architecture - the issue from the linker is that it isn't compiling your .c file, not that it can't figure out the linkage. So here's what happens...

Compiler goes through, can't find a way to compile the .c file properly Linker sees external C linkage, says "okay let's see if we can find it" can't find it because the .c file isn't compiled into object code, oh crap, abort.

You need to check your build phase - so go to your targets, select whatever target is active, see what is happening in the compile phase - my guess is that somehow whatever target you're aiming for (release or debug) got this file added as a i386 target instead of arm, which is why it's confused and can't find a rule to compile it (you aren't targeting i386 so it doesn't have rules to compile it).

If all else fails, try making a brand new target for arm and see if you can use it that way, or switch targets from release to debug, etc.

If you show me a snapshot of whatever target you're compiling for I can probably be a lot more helpful :). Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜