Apple Mach-O 链接器错误,由于文件扩展名

发布于 2025-01-04 22:18:45 字数 427 浏览 0 评论 0原文

可怕的典型链接器错误..

ld: 找不到架构 armv6 的符号 collect2: ld returned 1 exit status

但是,这是由文件名引起的?我使用 C++/Objective-C,所以我所有的 Obj-C 文件都是 .mm,但我永远不能使用任何 .c 文件。例如,我在我的项目中包含了 SFMT 算法,这给了我这些错误,但只需将单个 .c 文件更改为 .cpp 就可以让它消失,并且代码工作得很好!我只包含标题,所以我不确定为什么这会产生任何影响。

现在的问题是我试图包含 Freetype2,给了我同样的问题(很确定这是因为它是 .c),但这太大了,无法重命名每个文件,而且我还使用链接的二进制文件,所以除非我用新文件名重新编译它,我无法更改它。所以现在是时候找出这背后的真正原因了。

知道为什么会发生这种情况吗?如何阻止 .c 文件的链接器错误?

The dreaded typical linker error..

ld: symbol(s) not found for architecture armv6
collect2: ld returned 1 exit status

However, it is caused by filename? I use C++/Objective-C, so all of my Obj-C files are .mm, but I can never use any .c files. For example I've included the SFMT algorithm in my project, which was giving me these errors, but just changing the single .c file to .cpp made it go away and the code works just fine! I am only including the headers, so I'm not sure why this makes any difference.

The problem now is I'm trying to include Freetype2, giving me the same issue (pretty sure it's because it's .c), but that is far too large to rename every file, and I'm also using a linked binary, so unless I recompile it with new filenames, I can't change that. So now it's time to find the real reason behind this.

Any idea why this would happen? How can I stop linker errors for .c files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2025-01-11 22:18:45

将 Freetype 包含在 extern "C" 指令中:

// Non-C includes
#include <iostream>

extern "C"
{
    #include <freetype/freetype.h>
    // ... Other freetype includes
}

您可以在 extern " 中使用 #import 而不是 #include C" 指令。我从未尝试过,但我不明白为什么它不起作用。

Wrap your Freetype includes within an extern "C" directive:

// Non-C includes
#include <iostream>

extern "C"
{
    #include <freetype/freetype.h>
    // ... Other freetype includes
}

You can probably use #import instead of #include within an extern "C" directive. I've never tried, but I don't see why it wouldn't work.

不…忘初心 2025-01-11 22:18:45

用这个包围你的 c 头文件。这也可以包含:

#ifdef __cplusplus   
extern "C" {         
#endif     

// function declarations etc if this is your own header.
// OR you can use this in the .mm file to surround your include.
//...

#ifdef __cplusplus       
};                       
#endif   

这指定了 c 函数的外部链接。
如果在包含 c .h 文件时不这样做,C++ 编译器将以与 C 编译器不同的方式进行破坏,并导致链接器出现问题。
通过使用 extern "C",您可以告诉 C++ 编译器使用 C 风格的函数重整。

Surround your c header file with this. This can also surround the include:

#ifdef __cplusplus   
extern "C" {         
#endif     

// function declarations etc if this is your own header.
// OR you can use this in the .mm file to surround your include.
//...

#ifdef __cplusplus       
};                       
#endif   

This specifies external linkage for your c functions.
If you don't do this when you include your c .h files the C++ compiler will mangle in a different way from the C compiler, and cause problems for the linker.
By using extern "C", you are telling your C++ compiler to use C-style mangling of the functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文