构建 Linux C++ API 作为 Windows DLL
我正在尝试将 C++ Avro API 构建为DLL,以便我可以在 Visual Studio 项目中使用 Avro 生成的头文件。我可以使用 Cygwin 在 Windows 中构建 Avro,因此下一步是将 Avro API 转换为 DLL,并将其与我的项目中的 cygwin1.dll 一起使用(不完全清楚如何使用)这也可以)。
我一直在查看各种关于如何使用 cmake 构建 DLL(由 VTK 提供) 的在线指南(因为 cmake 通常用于构建 Avro)并且我想在花费大量时间尝试之前确保我尝试做的事情实际上是可行的。我还注意到 cygwin 指南 表明 gcc 编译器已经可以构建DLL:
gcc -shared -o mydll.dll mydll.o
通常在 Windows 中,我们必须在适当的情况下使用 __declspec(dllexport) 属性来指定需要导出哪些函数/类。任何人都可以澄清一下它们应该如何组合在一起:
- 为了使用 Cygwin 构建我的 DLL,我是否必须使用 VTK 指南中所示的导出属性来标记所有函数/类,或者我可以做一些事情来告诉gcc 编译器在不使用导出属性的情况下输出 DLL(即更改
CMakeList.txt
文件中的 cmake 选项)? - 一旦我从 cygwin 生成了 DLL,那么在我的 Visual Studio 项目中我必须同时引用
my.dll
和cygwin1.dll
?是“那么简单”还是我错过了什么?
如果我必须使用导出属性标记 Avro 中的所有函数/类,那么我可以想象这将是一项更加复杂的任务,因为 Avro 有相当数量的类。如果我不使用导出属性标记函数/类,并且使用 GCC 输出 DLL,那么是否会生成必要的导出符号以便在 VS 项目中使用它们?
I'm attempting to build the C++ Avro API as a DLL so I can use the Avro generated header files in a Visual Studio project. I'm able to build Avro in Windows using Cygwin, so the next step is to convert the Avro API into a DLL and use it along with the cygwin1.dll
in my project (not entirely clear on how this would work either).
I've been looking at various online guides on how to build the DLL using cmake (by VTK) (since cmake is generally used to build Avro) and I wanted to make sure that what I'm attempting to do is actually feasible before spending a ton of time trying to do it. I've also noticed that the cygwin guide indicates that the gcc compiler can already build DLLs:
gcc -shared -o mydll.dll mydll.o
Generally in windows we have to specify which functions/classes needs to be exported by using the __declspec(dllexport)
attribute where appropriate. Could anybody please clarify how it's all supposed to come together:
- In order to build my DLL with Cygwin, do I have to mark all of the functions/classes with the export attribute as shown in the VTK guide or can I just do something to tell the gcc compiler to output a DLL without using the export attributes (i.e. change the cmake options in the
CMakeList.txt
file)? - Once I generate a DLL from cygwin, then in my Visual Studio project I must reference both
my.dll
and thecygwin1.dll
? Is it "that simple" or am I missing something?
If I have to mark all of the functions/classes in Avro with the export attribute, then I can imagine it will be a significantly more involved task since Avro has a decent amount of classes. If I don't mark the functions/classes with the export attribute and I use GCC to output a DLL, then will the necessary export symbols be generated so they can be used in a VS project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我制作了一个尝试生成 DLL 的示例项目,这就是我的
CMakeLists.txt
中的内容:生成的输出是 DLL (.dll) 和共享库 (.a)。但是,我无法成功链接到 DLL,尽管我应该能够根据 cygwin FAQ 上的答案:http://cygwin.com/faq/faq.programming.html#faq.programming.msvs-mingw
上面的链接还提供了说明关于如何生成 .def 和 .lib(有关为什么需要它们的更多信息,请参阅链接)。
I made a sample project that attempts to generate the DLL and this is what I had in my
CMakeLists.txt
:The resulting output is a DLL (.dll) and a shared library (.a). However, I wasn't able to successfully link with the DLL, although I should be able to according to an answer on the cygwin FAQ: http://cygwin.com/faq/faq.programming.html#faq.programming.msvs-mingw
The above link also provides instructions on how to generate the .def and the .lib (see the links for more info on why they're needed).