C++链接到 C++ 的库使用 g++ 的 dll

发布于 2024-10-18 00:51:24 字数 1143 浏览 0 评论 0原文

我见过很多关于使用 C 库链接到 C++ 或 C++ 库链接到 C 的人的问题和答案...但是,我有一个链接到 C++ 的 C++ 库,并且得到了与人们将 C 和 C++ 混合在一起相同的症状(链接期间未定义的引用)。

这是我的 g++ 行:

g++ -L C:/MyLibraries mycode.cpp -shared -o mycode.dll -lopengl32 -lglu32 -lgdi32 -lMyLibrary

到目前为止,我遇到的每个答案都谈到了如何包装我的标头:

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

但是,我的库是纯 C++,100% 类,没有全局函数,没有外部“C”可以渗透的内容(不能在类中的方法上使用 extern“C”,并且用 extern“C”包装整个类集不会执行任何操作...链接时仍然未定义引用)。

MyLibrary.lib 是在 Visual Studio 2010 中编写的。而且,我使用 Visual Studio 2005 和 2008 编写了许多库。没有一个库需要添加到其中任何一个库中以将它们静态链接到其他基于 Visual Studio 的项目。 C++ 库链接到其他 C++ 项目,从来没有任何问题。

这是 Visual Studio 负责而 g++ 不负责的事情吗?我可以将大量我自己的 C++ 自定义静态库静态链接到其他 C++ 项目中,并且它可以正常工作。 Visual Studio 是否足够聪明,可以破译它自己的 C++ 方法名称修改,所以直到现在我才遇到过这种情况?但 g++ 不知道该怎么做,强制需要 C 命名约定(即使我在所有方面都是 100% C++)?

或者问题是,即使我使用 g++,它仍然执行一些标准 C 规则?我的 g++ 命令行有问题吗?

即使在 MSDN 上寻找导出 dll 函数的答案,也看起来更像是我的 g++ 行出了问题,因为它们的链接具有诸如“将 C 函数导出到 C++ 可执行文件”和“将 C++ 函数导出到 C”等主题可执行文件”,没有任何地方提到“将 C++ 函数导出到 C++ 可执行文件”的链接...我在我的函数上尝试了 __declspec(dllexport),它已编译,但仍然与 g++ 未定义链接。有什么想法吗?

谢谢。

I've seen numerous questions and answers to people with C libraries linking to C++, or C++ libraries linking to C... I however have a C++ library linking to C++, and am getting the same symptoms as people mixing C and C++ together (undefined reference during linking).

This is my g++ line:

g++ -L C:/MyLibraries mycode.cpp -shared -o mycode.dll -lopengl32 -lglu32 -lgdi32 -lMyLibrary

Every answer I've come across so far talks about having the following wrapping my header:

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

However, my library is pure C++, 100% classes, no global functions, nothing that an extern "C" can penitrate (can't extern "C" on the methods in the classes, and wrapping the entire set of classes with extern "C" does nothing... still undefined references when linking).

MyLibrary.lib was writen in Visual Studio 2010. And, I have written many libs using Visual Studio 2005 and 2008. Not a single one ever needed extern "C" added to any of them to statically link them into other Visual Studio based projects. C++ libraries linked into other C++ project, with no problems, ever.

Is this something that Visual Studio takes care of that g++ does not? I can staticly link a large number of my own C++ custom static libs together into other C++ projects, and it just works. Is Visual Studio just smart enough to decipher it's own C++ method name mangling, so I've never run into this until now? But g++ doesn't know what to do, forcing C naming conventions to be required (even though I'm 100% C++ on all ends)?

Or is the problem that even though I'm using g++, its still enforcing some standard C rules? Something wrong with my g++ command line?

Even looking for an answer at MSDN for exporting dll functions makes it look more like I've just got something wrong with my g++ line, because their links have subjects such as "Export C functions into C++ executables" and "Export C++ functions into C executables", with no link for "Export C++ functions into C++ executables" mentioned anywhere... I tried __declspec(dllexport) on my functions, which compiled, but still undefined linking with g++ still. Any ideas?

Thank you.

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

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

发布评论

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

评论(1

面犯桃花 2024-10-25 00:51:24

Visual Studio 和 g++ 对于名称修改、vtable 布局等使用非常不同的约定,因此将用它们构建的库链接在一起是不安全的(除非它们仅通过 C 接口进行通信)。有关 MinGW 的更多信息,请访问 http://www.mingw.org/wiki/MishingCompilers地点。

Visual Studio and g++ use very different conventions for things like name mangling, vtable layout, and such so that it is not safe to link libraries built with them together (other than when they only communicate through C interfaces). There is more information at http://www.mingw.org/wiki/MixingCompilers on the MinGW site.

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