链接静态 C++ 时 Objective-C 中的损坏符号表图书馆

发布于 2024-09-19 09:33:28 字数 942 浏览 0 评论 0原文

我有一个用 C++ 编写的名为 options 的类,这是标题信息:

class Options {
public:
string filename;
string chunkDir;
string outFilename;
string inFilename;
BOOL   compress;
BOOL   extract;
BOOL   print;
BOOL   reconstruct;
int    bits;

Options(string inFilename);
Options(int argc, char** argv);
void unsupported(string s);
void setOptionsFromArguments(int argc, char** argv);
void validateOptionCombination();
int getBits() {
    return bits
};

};

在 Objective-C 部分中,我像这样初始化选项:

Options *opts=new Options([fileName cStringUsingEncoding:NSUTF8StringEncoding]);

现在发生的情况是,如果我将指针传递给另一个 C++ 方法,它可以正常工作,但如果我实际上尝试使用例如

opts- 访问 Objective C 端中的任何内容, >位 或者 opts->getBits()

它总是返回 print 的整数值

看起来符号表在 Objective-C 和 C++ 端之间被破坏了,但我不知道我可以做什么来导致这种情况。

如果我为 Mac 进行编译,该代码甚至可以与 Objective-C++ 一起使用,并且只要 C++ 调用 C++,它似乎就可以工作,所以它不像内存被损坏,它只是看起来像符号表问题。

任何见解将不胜感激。

I have a class called options written in c++, here is the header info:

class Options {
public:
string filename;
string chunkDir;
string outFilename;
string inFilename;
BOOL   compress;
BOOL   extract;
BOOL   print;
BOOL   reconstruct;
int    bits;

Options(string inFilename);
Options(int argc, char** argv);
void unsupported(string s);
void setOptionsFromArguments(int argc, char** argv);
void validateOptionCombination();
int getBits() {
    return bits
};

};

In the objective-c section, I initialize Options like this:

Options *opts=new Options([fileName cStringUsingEncoding:NSUTF8StringEncoding]);

Now what happens is that if I pass the pointer to another C++ method it works fine, but if I actually try to access anything in the objective c side using, for example

opts->bits
or
opts->getBits()

It always returns the integer value for print

It looks like somehow the symbol table is getting mangled between the objective-c and C++ side, but I have no idea what I could have done to do cause this.

The code works even with Objective-C++ if I compile for the mac, and as long as C++ is calling C++ it seems to work so it's not like the memory is getting corrupted, it just looks like a symbol table issue.

Any insight would be much appreciated.

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

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

发布评论

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

评论(1

爱*していゐ 2024-09-26 09:33:28

在 Objective-C 中,BOOLsigned char 的类型定义。编译纯 C++ 时使用什么定义?如果不同,您会遇到各种奇怪的情况,因为 C++ 代码和 Objective-C++ 代码在成员变量的大小或布局上不一致。

In Objective-C, BOOL is a typedef to signed char. What definition are you using when compiling pure C++? If it's different, you'll get all sorts of weirdness, because the C++ code and the Objective-C++ code won't agree on the size or layout of the member variables.

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