UaModeler  1.3.2.273
 All Pages
HowTo (Linux): Compile the Generated Method Project Files

The generated code itself is identical for Windows and Linux; hence code that was generated under Windows can be used in a Linux SDK and vice versa. The integration of the generated code into your application strongly depends on the build environment you are using, e.g. Visual Studio, Eclipse, CMake, etc. Hello World Server example delivered with the Unified Automation C++ SDK using CMake in six simple steps.

Step 1: Adding Files to Your Build Environment

First, the generated source files must be added to the project. The Hello world server example can be found in [SDK Installation Directory]/examples/server_hello_world. Make sure that the files generated in HowTo (Linux): Create a New Project With a Method are actually in this folder. Open the file [SDK Installation Directory]/examples/server_hello_world/CMakeLists.txt and add the generated *.cpp files starting with “newns_”:

...
####################################### Create executable ###################################
ADD_EXECUTABLE(${PROJECT_NAME} ${HELLO_H}
servermain.cpp
// New code begins
newns_datatypes.cpp
newns_instancefactory_newnamespace.cpp
newns_myobjecttype.cpp
newns_myobjecttypebase.cpp
newns_nodemanagernewnamespace.cpp
newns_nodemanagernewnamespacebase.cpp
// New code ends
../utilities/opcserver.cpp
../utilities/shutdown.cpp
)
...

Step 2: Include Header File

The generated header files must be included in the file servermain.cpp. Add the marked lines:

#include <stdio.h>
#include <string.h>
#include "opcserver.h"
#include "shutdown.h"
#include "uaplatformlayer.h"
#include "uathread.h"
#if SUPPORT_XML_PARSER
#include "xmldocument.h"
#endif
#include "opcua_basedatavariabletype.h"
// New code begins
#include "newns_datatypes.h"
#include "newns_identifiers.h"
#include "newns_instancefactory_newnamespace.h"
#include "newns_myobjecttype.h"
#include "newns_myobjecttypebase.h"
#include "newns_nodemanagernewnamespace.h"
#include "newns_nodemanagernewnamespacebase.h"
// New code ends
...

Step 3: Creating NodeManager Instance

Now, we create an instance of the NewNs::NodeManagerNewNamespace in the server startup and add it to the Server’s Nodemanager. Add the marked code to servermain.cpp:

...
//- Start up OPC server ---------------------
// This code can be integrated into a startup
// sequence of the application where the
// OPC server should be integrated
//-------------------------------------------
// Create and initialize server object
OpcServer* pServer = new OpcServer;
pServer->setServerConfig(sConfigFileName, szAppPath);
// New code begins
NewNs::NodeManagerNewNamespace* pNM = new NewNs::NodeManagerNewNamespace(false);
pServer->addNodeManager(pNM);
// New code ends
// Start server object
...

Step 4: Creating an Object Instance

In the start method of the server the object must be instantiated and the node is added to the objects folder. After this step, the new object will be browseable and accessible in the server’s address space.

Add the following code to servermain.cpp:

...
// Start server object
ret = pServer->start();
if ( ret != 0 )
{
delete pServer;
return ret;
}
//-------------------------------------------
// New code begins
// Add an object instance to the objects folder
NewNs::MyObjectType* pObject = new NewNs::MyObjectType(
UaNodeId("MyObject", pNM->getNameSpaceIndex()), // NodeId
"MyObject", //Name
pNM->getNameSpaceIndex(), // Name space index
pNM); // Interface for adding nodes
pNM->addNodeAndReference(OpcUaId_ObjectsFolder, pObject,
OpcUaId_Organizes);
// New code ends
...

Step 5: Method Implementation (fktSum)

At last, the method has to be implemented in the file newns_myobjecttype.cpp.

/* MyObjectType method fktSum
*/
UaStatus MyObjectType::fktSum(const ServiceContext& serviceContext,
/*IN*/ OpcUa_UInt32 in1,
/*IN*/ OpcUa_UInt32 in2,
/*OUT*/ OpcUa_UInt32& out1)
{
OpcUa_ReferenceParameter(serviceContext);
out1 = in1 + in2; // Add this line
UaStatus ret;
return ret;
}

Step 6: Compile and Run

To compile the project server_hello_world with the generated *.cpp and .h files, just use the delivered buildExamples.sh script. You can also compile it in 64Bit mode using the script buildExamples64.sh.

Note
If you don’t want to build all examples, edit the line starting with “MODULES=”.
./buildExamples.sh

If the compilation succeeded, you should find compiled application server_hello_world in release version and server_hello_worldd in debug version in the folder [SDK Installation Directory]/bin. Just start the application and connect to the server with UaExpert.

To check if the generated code works, just browse to the node MyObject and call the method fktSum.