如何使用 __cdecl 调用约定编译 boost?
我有一个使用 __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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用即可。
只需在构建 boost 库时
Just use
while building boost libraries.
据我所知,没有办法让 C++ 使用 cdecl 调用约定(请参阅 MSDN调用约定)。 C++ 方法调用与 C 不同。必须使用 C 调用约定之一的唯一机会是函数,其中包括 C++ 中的类静态函数。如果您知道是这种情况,您可以尝试通过在构建期间添加选项来强制构建该选项:
bjam cxxflags=/Gd ...
(请参阅 BBv2 内置功能)
或者使其“永久”使用编译器设置 user-config.jam并将其添加到所有 BBv2 msvc 构建的构建选项中(请参阅 BBv2 配置 和相关文档)。至于您的其他问题:
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:
我在共享属性文件之一中找到了问题的原因: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.