问题
Hello community I would like to write UI for OPC UA server using open62541 lib. The input off software should read text file and cast data throe server to already available client.
Stuck at stage where need compile dll file for further using it in windows forms project.
There is some info mentioned that Davy Triponney compiled it with Visual C++ 2013 https://github.com/open62541/open62541/wiki/Using-open62541-from-C%23
But I have no luck with VS 2017.
There is sequence of my progress:
- Already downloaded .h and .c windows 64 bit files from https://open62541.org/
- Created Dynamic-Link Library(DLL) project.
- Attached .h and .c files to project, selected 64 compiler:
Getting lot warnings and fault on build:
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
The question is how property I can compile open62541 c code dll file with Visual Studio 2017 to able use it in a windows forms C# application as server?
回答1:
Since the project open62541 has been updated I suggest you take directly the current state of the sources. This way you will be able to better use the examples they provide. Here is the howto for building the project. Maybe it could be simpler but I encountered quite a lot of problems so I give you all the steps I did.
PREPARING THE ENVIRONMENT
- Install cmake (with the PATH update)
https://cmake.org/download/
- Install a recent version of python (with the PATH update)
https://www.python.org/downloads/
Reopen your session so that the new PATH variables are taken into account
Install the python module "six"
pip install six
BUILDING MBEDTLS
- Download the sources of the library (apache license)
https://tls.mbed.org/download
- Run cmake in the sources
cmake . -DUSE_SHARED_MBEDTLS_LIBRARY=ON -D CMAKE_BUILD_TYPE=Release
Open "mbed TLS.sln" with Visual Studio (I am using Community 2015) and generate everything in "Release" mode
Copy the following files
- libraries .lib from "library\Release" to "Path\To\OpcUa_lib"
- binaries .dll from "library\Release" to "Path\To\OpcUa_bin"
- the folder "mbedtls" in "include" to "Path\To\OpcUa_include"
BUILDING OPEN62541
- Clone the open62541 repository to get the sources (with GitHub desktop or command line)
https://github.com/open62541/open62541
Create a folder "build" in the sources (for example C:\Users\Davy\Documents\GitHub\open62541\build)
Open a terminal in the "build" folder and run cmake (adapt \path\To... first)
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
Open the solution "build\open62541.sln" with Visual Studio (Community 2015) and add "mbedcrypto.lib", "mbedtls.lib" and "mbedx509.lib" as resources for the linker in the project "open62541/lib/open62541"
- right click on "open62541/lib/open62541", select "properties"
- go to "Linker" => "General"
- add "Path\To\OpcUa_lib" as additional directory for the dependencies
- go to "Linker" => "Entry"
- add three lines with "mbedcrypto.lib", "mbedtls.lib" and "mbedx509.lib" in "Additional dependencies"
Generate "open62541/lib/open62541" in "Release" mode
You can now use the following files
- the library "build\bin\Release\open62541.lib"
- the binary "build\bin\Release\open62541.dll"
- the file "build\open62541.h"
INTEGRATION IN ANOTHER PROJECT
You will need to reference the amalgamated .h of open62541 and add these dependencies in the linker: open62541.lib, mbedcrypto.lib, mbedtls.lib, mbedx509.lib, Ws2_32.lib. I also had to specify the platform Visual Studio 2013 (v120) in the project properties.
来源:https://stackoverflow.com/questions/54985518/build-open62541-dll-with-visual-studio-2017