如何使用 __cdecl 调用约定编译 boost?

发布于 2024-08-29 23:03:39 字数 571 浏览 8 评论 0原文

我有一个使用 __cdecl 调用约定 (msvc2010) 编译的项目,并使用默认设置使用相同的编译器编译了 boost。

该项目与 boost 链接,但我在运行时收到如下断言消息: 文件:...\boost\boost\program_options\detail\parsers.hpp 行:79

运行时检查失败#0 - ESP 的值未在函数调用期间正确保存。这通常是用一种调用约定声明的函数和用另一种调用约定声明的函数指针调用的结果。

有以下问题:

  • 在 Windows (msvc2010) 上默认使用什么调用约定构建 boost
  • 如何使用 __cdecl 调用约定编译 boost
  • 为什么 boost 无法阻止与具有不同调用约定的代码链接?我知道 boost 有非常智能的库自动包含代码。

更新 #1

看起来 boost 确实使用正确的调用约定进行编译和链接,但在运行时我仍然遇到上述问题。我使用相同的代码做了一个示例应用程序,它可以工作,但在我的应用程序中它失败了。唯一的区别可能来自项目配置或includes/stdafx.h

I have a project compiled using __cdecl calling convention (msvc2010) and I compiled boost using the same compiler using the default settings.

The project linked with boost but I at runtime I got an assert message like this:
File: ...\boost\boost\program_options\detail\parsers.hpp
Line: 79

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

There are the following questions:

  • what calling convention does boost build with by default on Windows (msvc2010)
  • how to I compile boost with __cdecl calling convention
  • why boost wasn't able to prevent linking with code with different calling conventions? I understood that boost has really smart library auto-inclusion code.

Update #1

It looks that boost does compile and link with proper calling convention, still at runtime I get the above problem. I did a sample application using the same code and it works but in my application it fails. The only difference could be from project configuration or includes/stdafx.h

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

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

发布评论

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

评论(3

梦归所梦 2024-09-05 23:03:39

使用即可。

bjam ... **cxxflags=/Zp4**

只需在构建 boost 库时

Just use

bjam ... **cxxflags=/Zp4**

while building boost libraries.

孤星 2024-09-05 23:03:39

据我所知,没有办法让 C++ 使用 cdecl 调用约定(请参阅 MSDN调用约定)。 C++ 方法调用与 C 不同。必须使用 C 调用约定之一的唯一机会是函数,其中包括 C++ 中的类静态函数。如果您知道是这种情况,您可以尝试通过在构建期间添加选项来强制构建该选项:

bjam cxxflags=/Gd ...

(请参阅 BBv2 内置功能

或者使其“永久”使用编译器设置 user-config.jam并将其添加到所有 BBv2 msvc 构建的构建选项中(请参阅 BBv2 配置 和相关文档)。至于您的其他问题:

  1. Boost 使用 MSVC 使用的默认调用约定,除非它在代码级别覆盖它。我不知道它们在哪里,因为它们是特定于图书馆的。因此,您必须在代码中搜索“__*”代码装饰器。
  2. 请参阅上面的部分答案。
  3. 检测;原因有两个:我们可以合理检测到构建的不同选项的数量是有限的,因为它是不同可能变化的指数增长,因此我们将其限制为最重要的情况。在调用约定的情况下,这实际上是不可能的,因为它可以在每个函数的基础上进行更改。

As far as I know there's not way to make C++ use cdecl calling conventions (see MSDN Calling Convention). The C++ method calling is just different from C. The only opportunity that you have to use one of the C calling conventions is for functions, which include class static functions in C++. If you know that's the case you can try forcing the option when building by adding the option during the build:

bjam cxxflags=/Gd ...

(see BBv2 Builtin features)

Or to make it "permanent" set up a user-config.jam with your compiler and add it to the build options for all BBv2 msvc builds (see BBv2 Configuration and related docs). As for you other questions:

  1. Boost uses the default calling convention MSVC uses, except for cases where it overrides it at the code level. I don't know where those are as they are library specific. So you'd have to search the code for the "__*" code decorators.
  2. See above for partial answer.
  3. Detection; there are two reasons: There is a limit to how many different options we can reasonably detect for for building as it's an exponential growth of different possible variations so we limit it to the most important cases. And in the case of calling convention, it's not actually possible since it's something that can be changed on a per function basis.
梅倚清风 2024-09-05 23:03:39

我在共享属性文件之一中找到了问题的原因: 4Bytes

如果我将其删除,代码将正常工作。不过,我不确定为什么会发生这种情况,以及如何在不删除上述代码(另一个库需要)的情况下解决它。

我添加了另一个关于提升和结构成员对齐的问题。

I found the cause of the problem inside one of the shared property files: <StructMemberAlignment>4Bytes</StructMemberAlignment>

If I remove it the code will work. Still, I'm not sure why this is happening and how could I solve it without removing the above code (that was required by another library).

I added another question regarding boost and structure member alignment.

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