构建 Linux C++ API 作为 Windows DLL

发布于 2024-12-02 21:35:32 字数 1221 浏览 1 评论 0原文

我正在尝试将 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) 属性来指定需要导出哪些函数/类。任何人都可以澄清一下它们应该如何组合在一起:

  1. 为了使用 Cygwin 构建我的 DLL,我是否必须使用 VTK 指南中所示的导出属性来标记所有函数/类,或者我可以做一些事情来告诉gcc 编译器在不使用导出属性的情况下输出 DLL(即更改 CMakeList.txt 文件中的 cmake 选项)?
  2. 一旦我从 cygwin 生成了 DLL,那么在我的 Visual Studio 项目中我必须同时引用 my.dllcygwin1.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:

  1. 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)?
  2. Once I generate a DLL from cygwin, then in my Visual Studio project I must reference both my.dll and the cygwin1.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 技术交流群。

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

发布评论

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

评论(1

强辩 2024-12-09 21:35:32

我制作了一个尝试生成 DLL 的示例项目,这就是我的 CMakeLists.txt 中的内容:

PROJECT(DLLTest)

# Allow the developer to select if Dynamic or Static libraries are built
OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON)

# Set the LIB_TYPE variable to SHARED
SET (LIB_TYPE SHARED)

#IF (BUILD_SHARED_LIBS)
  # User wants to build Dynamic Libraries, so change the LIB_TYPE variable to CMake keyword 'SHARED'
#  SET (LIB_TYPE SHARED)
#ENDIF (BUILD_SHARED_LIBS)

# Create a target for the library
ADD_LIBRARY(DLLTest ${LIB_TYPE} hello.cpp)

SET( EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin" )
SET( LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib" )

生成的输出是 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:

PROJECT(DLLTest)

# Allow the developer to select if Dynamic or Static libraries are built
OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON)

# Set the LIB_TYPE variable to SHARED
SET (LIB_TYPE SHARED)

#IF (BUILD_SHARED_LIBS)
  # User wants to build Dynamic Libraries, so change the LIB_TYPE variable to CMake keyword 'SHARED'
#  SET (LIB_TYPE SHARED)
#ENDIF (BUILD_SHARED_LIBS)

# Create a target for the library
ADD_LIBRARY(DLLTest ${LIB_TYPE} hello.cpp)

SET( EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin" )
SET( LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib" )

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).

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