0%

嵌入式开发--交叉编译open65421

交叉编译open65421

参考

https://blog.csdn.net/Iron_Sky/article/details/109220261

https://open62541.org/doc/current/building.html

安装依赖

1
2
3
4
5
sudo apt-get install cmake-curses-gui # for the ccmake graphical interface
sudo apt-get install libmbedtls-dev # for encryption support
sudo apt-get install check libsubunit-dev # for unit tests
sudo apt-get install python-sphinx graphviz # for documentation generation
sudo apt-get install python-sphinx-rtd-theme # documentation style
1
2
3
4
5
6
7
8
9
10
vmuser@ubuntu:~/opcua$ git clone https://github.com/open62541/open62541.git
vmuser@ubuntu:~/opcua$ cd open62541
vmuser@ubuntu:~/opcua/open62541$ git submodule update --init --recursive
vmuser@ubuntu:~/opcua/open62541-1.2.2$ mkdir build && cd build
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ export CC=arm-none-linux-gnueabi-gcc
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ export CXX=arm-none-linux-gnueabi-g++
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUA_NAMESPACE_ZERO=FULL .. # -DUA_ENABLE_AMALGAMATION=ON .. 这个是static 文件不建议加
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ ccmake .. #指定路径
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ make
vmuser@ubuntu:~/opcua/open62541-1.2.2/build$ make install

动态链接库

export LD_LIBARY_PATH 或者拷贝至/user/lib

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// client.c,功能主要是从server那里获取时间                                                                                                                             

#include <stdlib.h>
#include "open62541.h"
int main(void)
{
UA_Client *client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://127.0.0.1:4840");
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return (int)retval;
}

/* Read the value attribute of the node. UA_Client_readValueAttribute is a
* wrapper for the raw read service available as UA_Client_Service_read. */
UA_Variant value; /* Variants can hold scalar values and arrays of any type */
UA_Variant_init(&value);

/* NodeId of the variable holding the current time */
const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
retval = UA_Client_readValueAttribute(client, nodeId, &value);

if(retval == UA_STATUSCODE_GOOD && UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME]))
{
UA_DateTime raw_date = *(UA_DateTime *) value.data;
UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
}

/* Clean up */
UA_Variant_clear(&value);
UA_Client_delete(client); /* Disconnects the client internally */

return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// server.c                                                                                                                                                             

/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#include "open62541.h"
#include <signal.h>
#include <stdlib.h>

UA_Boolean running = true;

static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}

int main(void)
{
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}

编译

1
2
3
arm-none-linux-gnueabi-gcc -std=c99 open62541.c server.c -o server -lrt
arm-none-linux-gnueabi-gcc -std=c99 open62541.c client.c -o client -lrt

问题

error :initializers

CMakeLists.txt文件,将-Werror改为-Wno-missing-field-initializers

error:undefined reference to `clock_gettime

编译时加 -lrt

无法ping通127.0.0.1

ifconfig lo 127.0.0.1