由各种编译器编译的链接库 [英] Link libraries compiled by various compilers

查看:216
本文介绍了由各种编译器编译的链接库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更详细地询问我最近在这里得到的答案(第三个):
编译语言的基础知识



如果我写C和MinGW,并链接到VC编译的C ++库,它会工作吗?我如何提前知道?



换句话说,如果我能够创建没有警告的.exe链接到该C ++ .dll,我能够运行(只是运行,没有进一步测试).exe,是否意味着它工作?在某一时刻不能核心转储?



要完全确定,我需要自己重新编译库源码和它的链接吗?



我知道链接C ++和C代码时可能会出现问题,但是如何知道它的工作原理?



PS:是的,我看到了
使用库编译...
我只是想我的问题稍有不同。

解决方案


如果我在C和MinGW中写入并链接到VC编译的C ++库,它会工作吗?


这取决于编译器(和我不知道MinGW)和具体的C ++库。



它可能不链接的原因,或者可能崩溃如果它链接:


  1. C ++库导出C ++类和方法,使用mangled名称,但MinGW的C ++名称调整可能(我不知道)不同于VC的(和不存在时你用C而不是C ++编码)


  2. VC代码不使用与MinGW相同的C运行时库, API是这样的,通过VC代码在堆上分配内存,然后应该被MinGW代码释放。


  3. VC的代码不是二进制的,可以与MinGW的代码(不使用相同的参数传递约定,不以相同的方式实现异常)


另一方面,它可能工作的一些原因:


  1. C ++库用C风格的接口


  2. 与1不同。


  3. MinGW编译器的厂商使它与VC二进制兼容





我如何提前知道?


我不知道。如果你发布从DLL导出的函数的名称和/或声明其公共/导出API的头文件,这将给我(或其他人)一个非常强的暗示是否导出(可能不兼容)C ++



否则你有两个阶段的问题:


  1. 需要什么(例如C而不是C ++,例如不假设他们使用相同的堆,例如指定参数传递约定)调用VC代码的代码?


  2. 是否将您的VC库写入了这一点?


有人使用这两个编译器可能回答第一个问题(我没有使用MinGW)。我不知道谁能回答第二个;谁写的VC库?


I would like to ask in more detail about a answer I recently got here (3rd one): Compiled languages basics

If I write in C and MinGW and I link to C++ library compiled by VC - will it work? How do I know in advance?

In other words, if I'm able to create without warnings an .exe which links to that C++ .dll, and I'm able to run (just run, no further testing) that .exe, does it mean it worked? Wont it core-dump at some point?

To be totally sure, do I need to re-compile the library sources by myself and the link with it?

I understand that there might be a problem sometimes with linking C++ and C code but how to know when it works and worked?

PS: Yes, I saw Use libraries compiled... I just thought my question is slightly different.

解决方案

If I write in C and MinGW and I link to C++ library compiled by VC - will it work?

It depends on the compilers (and I don't know MinGW) and on the specific C++ library.

Reasons why it might not link, or might crash if it does link:

  1. The C++ library is exporting C++ classes and methods, using "mangled" names, but MinGW's C++ name mangling may (I don't know) be different than VC's (and non-existent when you're coding in C instead of C++)

  2. VC code doesn't use the same C run-time library as MinGW, which will bite you if the API is such that memory is allocated on the heap by VC code and is then supposed to be freed by the MinGW code.

  3. VC's code isn't binary-comptible with MinGW's code (doesn't use the same parameter-passing conventions, doesn't implement exceptions in the same way)

On the other hand, some reasons why it might work:

  1. The C++ library is written with C-style interface, by developers who intended it to be called from a different compiler

  2. Same as 1.

  3. The makers of the MinGW compiler made it binary-compatible with VC

How do I know in advance?

I don't know. If you post the names of the functions exported from the DLL, and/or the header file which declares its public/exported API, that would give me (or someone else) a very strong hint about whether it's exporting (possibly-incompatible) C++-style methods or exporting (more-likely-to-be-comptible) C-style functions.

Otherwise you have a two-stage question:

  1. What does it take (e.g. C instead of C++, e.g. not assuming they use the same heap, e.g. specifiying the parameter-passing convention) for MinGW code to invoke VC code?

  2. Has your VC library be written with that in mind?

Someone who has used both compilers could probably answer the first question (I haven't used MinGW). I don't know who could answer the second; who wrote that VC library?

这篇关于由各种编译器编译的链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆