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

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

问题描述

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



如果我写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 ++风格的方法或出口(更可能合并的)C风格的功能。



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


  1. 需要什么(例如C代替C ++,例如不假定他们使用相同的堆,例如指定参数传递约定)MinGW代码调用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天全站免登陆