使用的extern&QUOT链接错误; C"在Objective-C code [英] Linker error using extern "C" in Objective-C code

查看:109
本文介绍了使用的extern&QUOT链接错误; C"在Objective-C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以从两个Objective-C和C ++中的iPhone应用程序code调用一些实用功能。我有一个不能被编译成ObjectiveC ++(.mm)第三方C ++类。我有一个头文件中声明我的函数,则.c文件中定义它们。我了两倍检查拼写错误,但由于某些原因,我的链接器无法找到任何的函数的定义。

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 have 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.

下面是用于C辅助函数头:

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

这里是.c文件:

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];
}

我试过有和没有外部的C编译,但具有相同的链接器错误的结果。

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?

链接器错误:

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

更新:我已经似乎对我的编译源文件为目标C到解决问题,即使没有实际ObjectiveC code,谁能告诉我为什么上面的源文件将不能编译为C code吗?

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?

推荐答案

使用下面的构造来指定C链接的C和C ++的翻译:

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*);

这个声明将与C,objc,C ++,objc ++。

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

这篇关于使用的extern&QUOT链接错误; C&QUOT;在Objective-C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆