使用 Visual Studio 2017 构建 open62541 DLL

Posted

技术标签:

【中文标题】使用 Visual Studio 2017 构建 open62541 DLL【英文标题】:Build open62541 DLL with Visual Studio 2017 【发布时间】:2019-07-25 21:35:50 【问题描述】:

社区您好,我想使用 open62541 库为 OPC UA 服务器编写 UI。 输入关闭软件应该读取文本文件并将数据通过服务器投射到已经可用的客户端。

卡在需要编译 dll 文件以便在 windows 窗体项目中进一步使用它的阶段。

有一些信息提到 Davy Triponney 用 Visual C++ 2013 编译它 https://github.com/open62541/open62541/wiki/Using-open62541-from-C%23

但是我在 VS 2017 上没有运气。

我的进度有先后顺序:

已经从https://open62541.org/ 下载了 .h 和 .c windows 64 位文件 已创建动态链接库 (DLL) 项目。 附加 .h 和 .c 文件到项目,选择 64 编译器:

在构建时收到很多警告和错误:

Severity    Code    Description Project File    Line    Suppression State
Warning C4005   'CLOSESOCKET': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39113   
Warning C4005   '_Q_INVALIDATE': macro redefinition open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 130 
Warning C4005   'UA_atomic_sync': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 813 
Warning C4005   'UA_atomic_sync': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 815 
Warning C4005   'CLOSESOCKET': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 35386   
Warning C4005   'errno__': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 35387   
Warning C4005   'CLOSESOCKET': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39088   
Warning C4005   'CLOSESOCKET': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39106   
Warning C4005   'UA_sleep_ms': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39111   
Warning C4005   'UA_sleep_ms': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39127   
Warning C4005   'UA_sleep_ms': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39130   
Warning C4005   'WIN32_INT': macro redefinition open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39135   
Warning C4005   'OPTVAL_TYPE': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39136   
Warning C4005   'ERR_CONNECTION_PROGRESS': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39137   
Warning C4005   'UA_fd_set': macro redefinition open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39167   
Warning C4005   'UA_fd_isset': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39168   
Warning C4005   'errno__': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39176   
Warning C4005   'errno__': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39181   
Warning C4005   'INTERRUPTED': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39182   
Warning C4005   'WOULDBLOCK': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39183   
Warning C4005   'AGAIN': macro redefinition open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 39184   
Warning C4005   'ANSI_COLOR_RED': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40158   
Warning C4005   'ANSI_COLOR_GREEN': macro redefinition  open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40159   
Warning C4005   'ANSI_COLOR_YELLOW': macro redefinition open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40160   
Warning C4005   'ANSI_COLOR_BLUE': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40161   
Warning C4005   'ANSI_COLOR_MAGENTA': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40162   
Warning C4005   'ANSI_COLOR_CYAN': macro redefinition   open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40163   
Warning C4005   'ANSI_COLOR_RESET': macro redefinition  open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40164   
Warning C4005   'BEGIN_CRITSECT': macro redefinition    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40709   
Warning C4005   'END_CRITSECT': macro redefinition  open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 40710   
Error   C1010   unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?    open62541   c:\users\scadaadmin\desktop\open62541-win64\open62541.c 43979   

问题是我如何使用 Visual Studio 2017 编译 open62541 c 代码 dll 文件以在 Windows 窗体 C# 应用程序中将其用作服务器?

【问题讨论】:

我认为这些警告并不重要。这是最后一行。你打开open62541.c并检查线路:43979? 这可能与架构选择有关。如果架构选择了POSIX,那么win32就不行了。官方的单文件下载似乎是针对 POSIX 的。只有一个解决方案:在合并中添加所有架构。并用条件(ifdef)包装它们,以便只使用检测到的架构。这意味着我们需要额外检测带有宏的架构。但实际上是如何做到的呢? 您是否像我说的那样打开了文件?听起来你可能按照我的建议做了。 第 43979 行是 .c 文件的结尾,我还发现了一篇关于 ***.com/questions/20113280/… 的文章 .. 但是当我设置一个不使用的预编译标题时,会出现更多错误。 新的错误是什么? 43979 的线路是什么? 【参考方案1】:

由于项目 open62541 已经更新,我建议您直接获取源的当前状态。这样,您将能够更好地使用他们提供的示例。这是构建项目的方法。也许它可以更简单,但我遇到了很多问题,所以我给你我做的所有步骤。


准备环境

    安装 cmake(使用 PATH 更新)

https://cmake.org/download/

    安装最新版本的 python(带有 PATH 更新)

https://www.python.org/downloads/

    重新打开会话,以便考虑新的 PATH 变量

    安装python模块“六”

pip 安装六

构建 MBEDTLS

    下载库的源代码(apache 许可证)

https://tls.mbed.org/download

    在源代码中运行 cmake
cmake . -DUSE_SHARED_MBEDTLS_LIBRARY=ON -D CMAKE_BUILD_TYPE=Release

    使用 Visual Studio 打开“mbed TLS.sln”(我使用的是 Community 2015)并在“发布”模式下生成所有内容

    复制以下文件

    库 .lib 从“library\Release”到“Path\To\OpcUa_lib” 二进制 .dll 从“library\Release”到“Path\To\OpcUa_bin” “include”中的“mbedtls”文件夹到“Path\To\OpcUa_include”

建筑开放62541

    克隆 open62541 存储库以获取源代码(使用 GitHub 桌面或命令行)

https://github.com/open62541/open62541

    在源代码中创建一个“build”文件夹(例如 C:\Users\Davy\Documents\GitHub\open62541\build)

    在“build”文件夹中打开一个终端并运行 cmake(先适应 \path\To...)

cmake .. -G "Visual Studio 14 2015" -DUA_ENABLE_ENCRYPTION=ON -DUA_ENABLE_AMALGAMATION=ON -DMBEDTLS_INCLUDE_DIRS="Path\To\OpcUa_include" -DMBEDTLS_LIBRARY="Path\To\OpcUa_lib" -DMBEDX509_LIBRARY="Path\To\OpcUa_lib" -DMBEDCRYPTO_LIBRARY="Path\To\OpcUa_lib" -DBUILD_SHARED_LIBS=ON -D CMAKE_BUILD_TYPE=Release -DUA_LOGLEVEL=100

    使用 Visual Studio (Community 2015) 打开解决方案“build\open62541.sln”,并在项目“open62541/”中添加“mbedcrypto.lib”、“mbedtls.lib”和“mbedx509.lib”作为链接器的资源lib/open62541"

    右击“open62541/lib/open62541”,选择“属性” 转到“链接器”=>“常规” 添加“Path\To\OpcUa_lib”作为依赖项的附加目录 转到“链接器”=>“条目” 在“附加依赖项”中添加“mbedcrypto.lib”、“mbedtls.lib”和“mbedx509.lib”三行

    在“发布”模式下生成“open62541/lib/open62541”

    您现在可以使用以下文件

    库“build\bin\Release\open62541.lib” 二进制“build\bin\Release\open62541.dll” 文件“build\open62541.h”

在另一个项目中集成

您需要引用 open62541 的合并 .h 并在链接器中添加以下依赖项:open62541.lib、mbedcrypto.lib、mbedtls.lib、mbedx509.lib、Ws2_32.lib。我还必须在项目属性中指定平台 Visual Studio 2013 (v120)。

【讨论】:

以上是关于使用 Visual Studio 2017 构建 open62541 DLL的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 2017 无法使用库构建

如何使用 Visual Studio 2017 在 Windows 上构建 OpenSSL?

swig 和使用 Visual Studio 2017 命令行构建项目

在 Visual Studio 2017 中使用 Cmake 构建 ssh.dll

使用 Visual Studio 2017 构建 open62541 DLL

Visual Studio 2017不构建项目