使用 extern“C”的链接器错误在 Objective-C 代码中
我正在尝试创建一些可以从 iPhone 应用程序中的 Objective-C 和 C++ 代码调用的实用函数。我有无法编译为 ObjectiveC++ (.mm) 的第三方 C++ 类。我有一个头文件声明我的函数,然后有一个定义它们的 .c 文件。我已经三次检查拼写错误,但由于某种原因,我的链接器无法找到任何函数的定义。
这是 C 辅助函数的标头:
#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 文件:
#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];
}
我尝试过使用和不使用 extern“C”进行编译,但具有相同的链接器错误结果。
任何人都可以阐明我做错了什么吗?
链接器错误:
Undefined symbols for architecture i386:
"loadMeshFromFile(char const*, void*)", referenced from:
更新:我似乎通过将源文件编译为 Objective C 来解决问题,即使没有实际的 ObjectiveC 代码,谁能告诉我为什么上面的源文件不会编译为 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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下构造来指定 c 和 c++ 翻译的 c 链接:
此声明将与 c、objc、c++、objc++ 兼容。
use the following construct to specify c linkage for c and c++ translations:
this declaration will be compatible with c, objc, c++, objc++.
仔细检查您的体系结构 - 链接器的问题是它没有编译您的 .c 文件,而不是它无法找出链接。所以这就是发生的事情......
编译器通过,找不到正确编译 .c 文件的方法
链接器看到外部 C 链接,说“好吧,让我们看看是否能找到它”找不到它,因为 .c 文件没有编译成目标代码,哦,糟糕,中止。
您需要检查您的构建阶段 - 所以转到您的目标,选择任何活动的目标,看看编译阶段发生了什么 - 我的猜测是,无论您想要什么目标(发布或调试)都添加了此文件作为 i386 目标而不是 arm,这就是为什么它很混乱并且找不到编译它的规则(您不是以 i386 为目标,所以它没有编译它的规则)。
如果一切都失败了,请尝试为arm创建一个全新的目标,看看是否可以这样使用它,或者将目标从发布切换到调试等。
如果你向我展示你正在编译的任何目标的快照,我可能可以会更有帮助:)。祝你好运!
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!