在 Windows 中使用 open62541 构建一个简单的服务器
Posted
技术标签:
【中文标题】在 Windows 中使用 open62541 构建一个简单的服务器【英文标题】:Building a simple server using open62541 in Windows 【发布时间】:2021-05-17 08:06:49 【问题描述】:我正在尝试通过参考page 提供的文档来使用 open62541 构建服务器。
通过参考webpage,我可以生成 open62541.c 和 open62541.h 文件。 如前所述,我使用 CMake 和 VisualStudio 生成了这些文件。当我尝试编译 myServer.c 文件时,出现以下错误。
大多数thr错误描述未定义某些变量。我发现在open62541.h或open62541.c文件中未定义UA_SOCKET,但在文档中写到这两个文件足以构建服务器文件.
有人成功编译服务器文件吗?谢谢。
错误
In file included from open62541.c:28: open62541.h:22834:5: error: unknown type name 'UA_SOCKET' 22834 | UA_SOCKET sockfd; /* Most connectivity solutions run on | ^~~~~~~~~ open62541.h:30209:1: error: unknown type name 'ssize_t'; did you mean '_ssize_t'? 30209 | ssize_t UA_send(UA_SOCKET sockfd, const void *buf, size_t len, int flags); //equivalent to posix send implementation | ^~~~~~~ | _ssize_t open62541.h:30209:17: error: unknown type name 'UA_SOCKET' 30209 | ssize_t UA_send(UA_SOCKET sockfd, const void *buf, size_t len, int flags); //equivalent to posix send implementation | ^~~~~~~~~ open62541.h:30213:1: error: unknown type name 'ssize_t'; did you mean '_ssize_t'? 30213 | ssize_t UA_sendto(UA_SOCKET sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); //equivalent to posix sendto implementation | ^~~~~~~ | _ssize_t open62541.h:30213:19: error: unknown type name 'UA_SOCKET' 30213 | ssize_t UA_sendto(UA_SOCKET sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); //equivalent to posix sendto implementation | ^~~~~~~~~ open62541.h:30213:111: error: unknown type name 'socklen_t' 30213 | ssize_t UA_sendto(UA_SOCKET sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); //equivalent to posix sendto implementation | ^~~~~~~~~ open62541.h:30217:15: error: unknown type name 'UA_SOCKET' 30217 | int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); //equivalent to posix select implementation | ^~~~~~~~~ open62541.h:30217:31: error: unknown type name 'fd_set' 30217 | int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); //equivalent to posix select implementation | ^~~~~~ open62541.h:30217:48: error: unknown type name 'fd_set' 30217 | int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); //equivalent to posix select implementation | ^~~~~~ open62541.h:30217:66: error: unknown type name 'fd_set' 30217 | int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); //equivalent to posix select implementation | ^~~~~~ open62541.h:30221:1: error: unknown type name 'ssize_t'; did you mean '_ssize_t'? 30221 | ssize_t UA_recv(UA_SOCKET sockfd, void *buf, size_t len, int flags); //equivalent to posix recv implementation | ^~~~~~~ | _ssize_t open62541.h:30221:17: error: unknown type name 'UA_SOCKET' 30221 | ssize_t UA_recv(UA_SOCKET sockfd, void *buf, size_t len, int flags); //equivalent to posix recv implementation | ^~~~~~~~~ open62541.h:30225:1: error: unknown type name 'ssize_t'; did you mean '_ssize_t'? 30225 | ssize_t UA_recvfrom(UA_SOCKET sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); | ^~~~~~~ | _ssize_t open62541.h:30225:21: error: unknown type name 'UA_SOCKET' 30225 | ssize_t UA_recvfrom(UA_SOCKET sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); | ^~~~~~~~~ open62541.h:30225:100: error: unknown type name 'socklen_t' 30225 | ssize_t UA_recvfrom(UA_SOCKET sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); | ^~~~~~~~~ open62541.h:30229:17: error: unknown type name 'UA_SOCKET' 30229 | int UA_shutdown(UA_SOCKET sockfd, int how); //equivalent to posix shutdown implementation | ^~~~~~~~~ open62541.h:30233:1: error: unknown type name 'UA_SOCKET' 30233 | UA_SOCKET UA_socket(int domain, int type, int protocol);//equivalent to posix socket implementation | ^~~~~~~~~ open62541.h:30237:13: error: unknown type name 'UA_SOCKET' 30237 | int UA_bind(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix bind implementation | ^~~~~~~~~ open62541.h:30237:60: error: unknown type name 'socklen_t' 30237 | int UA_bind(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix bind implementation | ^~~~~~~~~ open62541.h:30241:15: error: unknown type name 'UA_SOCKET' 30241 | int UA_listen(UA_SOCKET sockfd, int backlog);//equivalent to posix listen implementation | ^~~~~~~~~ open62541.h:30245:15: error: unknown type name 'UA_SOCKET' 30245 | int UA_accept(UA_SOCKET sockfd, struct sockaddr *addr, socklen_t *addrlen);//equivalent to posix accept implementation | ^~~~~~~~~ open62541.h:30245:56: error: unknown type name 'socklen_t' 30245 | int UA_accept(UA_SOCKET sockfd, struct sockaddr *addr, socklen_t *addrlen);//equivalent to posix accept implementation | ^~~~~~~~~ open62541.h:30249:14: error: unknown type name 'UA_SOCKET' 30249 | int UA_close(UA_SOCKET sockfd);//equivalent to posix close implementation | ^~~~~~~~~ open62541.h:30253:16: error: unknown type name 'UA_SOCKET' 30253 | int UA_connect(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix connect implementation | ^~~~~~~~~ open62541.h:30253:63: error: unknown type name 'socklen_t' 30253 | int UA_connect(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix connect implementation | ^~~~~~~~~ open62541.h:30257:16: error: unknown type name 'UA_SOCKET' 30257 | void UA_fd_set(UA_SOCKET fd, fd_set *set); //equivalent to posix FD_SET implementation | ^~~~~~~~~ open62541.h:30257:30: error: unknown type name 'fd_set' 30257 | void UA_fd_set(UA_SOCKET fd, fd_set *set); //equivalent to posix FD_SET implementation | ^~~~~~ open62541.h:30261:17: error: unknown type name 'UA_SOCKET' 30261 | int UA_fd_isset(UA_SOCKET fd, fd_set *set);//equivalent to posix FD_ISSET implementation | ^~~~~~~~~ open62541.h:30261:31: error: unknown type name 'fd_set' 30261 | int UA_fd_isset(UA_SOCKET fd, fd_set *set);//equivalent to posix FD_ISSET implementation | ^~~~~~ open62541.h:30265:72: warning: 'struct addrinfo' declared inside parameter list will not be visible outside of this definition or declaration 30265 | int UA_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);//equivalent to posix getaddrinfo implementation | ^~~~~~~~ open62541.h:30287:37: error: unknown type name 'UA_SOCKET' 30287 | unsigned int UA_socket_set_blocking(UA_SOCKET sockfd);//set a socket as blocking. Returns 0 if OK, other value otherwise | ^~~~~~~~~ open62541.h:30291:40: error: unknown type name 'UA_SOCKET' 30291 | unsigned int UA_socket_set_nonblocking(UA_SOCKET sockfd);//set a socket as non-blocking. Returns 0 if OK, other value otherwise | ^~~~~~~~~ open62541.h:30295:69: error: unknown type name 'socklen_t' 30295 | int UA_getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); //equivalent to posix getsockopt implementation. Only in non windows architectures | ^~~~~~~~~ open62541.h:30299:75: error: unknown type name 'socklen_t' 30299 | int UA_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);//equivalent to posix setsockopt implementation | ^~~~~~~~~ open62541.h:30303:29: warning: 'struct addrinfo' declared inside parameter list will not be visible outside of this definition or declaration 30303 | void UA_freeaddrinfo(struct addrinfo *res);//equivalent to posix freeaddrinfo implementatio | ^~~~~~~~ open62541.h:30311:55: error: unknown type name 'socklen_t' 30311 | int UA_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);//equivalent to posix getsockname implementation | ^~~~~~~~~ open62541.c: In function 'UA_Timer_init': open62541.c:19600:5: warning: implicit declaration of function 'UA_LOCK_INIT'; did you mean 'UA_LOG_INFO'? [-Wimplicit-function-declaration] 19600 | UA_LOCK_INIT(&t->timerMutex); | ^~~~~~~~~~~~ | UA_LOG_INFO open62541.c:19600:20: error: 'UA_Timer' has no member named 'timerMutex' 19600 | UA_LOCK_INIT(&t->timerMutex); | ^~ open62541.c: In function 'UA_Timer_addTimerEntry': open62541.c:19605:5: warning: implicit declaration of function 'UA_LOCK' [-Wimplicit-function-declaration] 19605 | UA_LOCK(&t->timerMutex); | ^~~~~~~ open62541.c:19605:15: error: 'UA_Timer' has no member named 'timerMutex' 19605 | UA_LOCK(&t->timerMutex); | ^~ open62541.c:19611:5: warning: implicit declaration of function 'UA_UNLOCK' [-Wimplicit-function-declaration] 19611 | UA_UNLOCK(&t->timerMutex); | ^~~~~~~~~ open62541.c:19611:17: error: 'UA_Timer' has no member named 'timerMutex' 19611 | UA_UNLOCK(&t->timerMutex); | ^~ 37908 | UA_LOCK(&server->serviceMutex); | ^~ open62541.c:37910:22: error: 'UA_Server' has no member named 'serviceMutex' 37910 | UA_UNLOCK(&server->serviceMutex); | ^~ open62541.c: In function 'Service_FindServers': open62541.c:38031:27: error: 'UA_Server' has no member named 'serviceMutex' 38031 | UA_LOCK_ASSERT(&server->serviceMutex, 1); | ^~ open62541.c: In function 'Service_GetEndpoints': open62541.c:38115:27: error: 'UA_Server' has no member named 'serviceMutex' 38115 | UA_LOCK_ASSERT(&server->serviceMutex, 1); | ^~ open62541.c: In function 'setSubscriptionSettings': open62541.c:38662:27: error: 'UA_Server' has no member named 'serviceMutex' 38662 | UA_LOCK_ASSERT(&server->serviceMutex, 1)ent 2 of 'addServerSocket' from incompatible pointer type [-Wincompatible-pointer-types] 64148 | UA_StatusCode statusCode = addServerSocket(layer, ai); | ^~ | | | struct addrinfo * open62541.c:64006:64: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *' 64006 | addServerSocket(ServerNetworkLayerTCP *layer, struct addrinfo *ai) | ~~~~~~~~~~~~~~~~~^~ open62541.c:64151:29: warning: passing argument 1 of 'UA_freeaddrinfo' from incompatible pointer type [-Wincompatible-pointer-types] 64151 | UA_freeaddrinfo(res); | ^~~ | | | struct addrinfo * In file included from open62541.c:28: open62541.h:30303:39: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *' 30303 | void UA_freeaddrinfo(struct addrinfo *res);//equivalent to posix freeaddrinfo implementatio | ~~~~~~~~~~~~~~~~~^~~ open62541.c:64155:21: warning: passing argument 1 of 'UA_freeaddrinfo' from incompatible pointer type [-Wincompatible-pointer-types] 64155 | UA_freeaddrinfo(res); | ^~~ | | | struct addrinfo * In file included from open62541.c:28: open62541.h:30303:39: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *' 30303 | void UA_freeaddrinfo(struct addrinfo *res);//equivalent to posix freeaddrinfo implementatio | ~~~~~~~~~~~~~~~~~^~~ open62541.c: At top level: open62541.c:64186:40: error: unknown type name 'fd_set' 64186 | setFDSet(ServerNetworkLayerTCP *layer, fd_set *fdset) | ^~~~~~ open62541.c: In function 'ServerNetworkLayerTCP_listen': open62541.c:64215:5: error: unknown type name 'fd_set' 64215 | fd_set fdset, errset; | ^~~~~~ open62541.c:64216:26: warning: implicit declaration of function 'setFDSet' [-Wimplicit-function-declaration] 64216 | UA_Int32 highestfd = setFDSet(layer, &fdset); | ^~~~~~~~ open62541.c:64218:12: error: variable 'tmptv' has initializer but incomplete type 64218 | struct timeval tmptv = 0, timeout * 1000; | ^~~~~~~ open62541.c:64218:29: warning: excess elements in struct initializer 64218 | struct timeval tmptv = 0, timeout * 1000; | ^ open62541.c:64218:29: note: (near initialization for 'tmptv') open62541.c:64218:32: warning: excess elements in struct initializer 64218 | struct timeval tmptv = 0, timeout * 1000; | ^~~~~~~ open62541.c:64218:32: note: (near initialization for 'tmptv') open62541.c:64218:20: error: storage size of 'tmptv' isn't known 64218 | struct timeval tmptv = 0, timeout * 1000; | ^~~~~ open62541.c:64222:60: error: 'errno_str' undeclared (first use in this function) 64222 | "Socket select failed with %s", errno_str)); | ^~~~~~~~~ open62541.c:64229:13: warning: implicit declaration of function 'UA_fd_isset' [-Wimplicit-function-declaration] 64229 | if(!UA_fd_isset(layer->serverSockets[i], &fdset)) | ^~~~~~~~~~~ open62541.c:64232:33: error: storage size of 'remote' isn't known 64232 | struct sockaddr_storage remote; | ^~~~~~ open62541.c:64233:9: error: unknown type name 'socklen_t' 64233 | socklen_t remote_size = sizeof(remote); | ^~~~~~~~~ open62541.c:64234:9: error: unknown type name 'UA_SOCKET' 64234 | UA_SOCKET newsockfd = UA_accept(layer->serverSockets[i], | ^~~~~~~~~ open62541.c:64234:31: warning: implicit declaration of function 'UA_accept'; did you mean 'UA_access'? [-Wimplicit-function-declaration] 64234 | UA_SOCKET newsockfd = UA_accept(layer->serverSockets[i], | ^~~~~~~~~ | UA_access open62541.c:64236:25: error: 'UA_INVALID_SOCKET' undeclared (first use in this function) 64236 | if(newsockfd == UA_INVALID_SOCKET) | ^~~~~~~~~~~~~~~~~ open62541.c: At top level: open62541.c:64373:21: error: field 'hints' has incomplete type 64373 | struct addrinfo hints, *server; | ^~~~~ open62541.c: In function 'ClientNetworkLayerTCP_close': open62541.c:64388:30: error: 'UA_INVALID_SOCKET' undeclared (first use in this function) 64388 | if(connection->sockfd != UA_INVALID_SOCKET) | ^~~~~~~~~~~~~~~~~ open62541.c: In function 'ClientNetworkLayerTCP_free': open62541.c:64402:38: warning: passing argument 1 of 'UA_freeaddrinfo' from incompatible pointer type [-Wincompatible-pointer-types] 64402 | UA_freeaddrinfo(tcpConnection->server); | ~~~~~~~~~~~~~^~~~~~~~ | | | struct addrinfo * In file included from open62541.c:28: open62541.h:30303:39: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *' 30303 | void UA_freeaddrinfo(struct addrinfo *res);//equivalent to posix freeaddrinfo implementatio | ~~~~~~~~~~~~~~~~~^~~ open62541.c: In function 'UA_ClientConnectionTCP_poll': open62541.c:64426:30: error: 'UA_INVALID_SOCKET' undeclared (first use in this function) 64426 | if(connection->sockfd == UA_INVALID_SOCKET) | ^~~~~~~~~~~~~~~~~ open62541.c:64427:61: error: invalid use of undefined type 'struct addrinfo' 64427 | connection->sockfd = UA_socket(tcpConnection->server->ai_family, | ^~ open62541.c:64428:61: error: invalid use of undefined type 'struct addrinfo' 64428 | tcpConnection->server->ai_socktype, | ^~ open62541.c:64429:61: error: invalid use of undefined type 'struct addrinfo' 64429 | tcpConnection->server->ai_protocol); | ^~ open62541.c:64432:68: error: 'UA_ERRNO' undeclared (first use in this function) 64432 | "Could not create client socket: %s", strerror(UA_ERRNO)); | ^~~~~~~~ open62541.c:64457:17: warning: implicit declaration of function 'UA_connect' [-Wimplicit-function-declaration] 64457 | int error = UA_connect(connection->sockfd, tcpConnection->server->ai_addr, | ^~~~~~~~~~ open62541.c:64457:69: error: invalid use of undefined type 'struct addrinfo' 64457 | int error = UA_connect(connection->sockfd, tcpConnection->server->ai_addr, | ^~ open62541.c:64458:49: error: invalid use of undefined type 'struct addrinfo' 64458 | tcpConnection->server->ai_addrlen); | ^~ open62541.c:64467:20: error: 'UA_ERR_CONNECTION_PROGRESS' undeclared (first use in this function) 64467 | if(UA_ERRNO != UA_ERR_CONNECTION_PROGRESS) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ open62541.c:64503:5: error: unknown type name 'fd_set' 64503 | fd_set fdset; | ^~~~~~ open62541.c:64506:12: error: variable 'tmptv' has initializer but incomplete type 64506 | struct timeval tmptv = (long int) (timeout_usec / 1000000), | ^~~~~~~ open62541.c:64506:30: warning: excess elements in struct initializer 64506 | struct timeval tmptv = (long int) (timeout_usec / 1000000), | ^ open62541.c:64506:30: note: (near initialization for 'tmptv') open62541.c:64507:30: warning: excess elements in struct initializer 64507 | (int) (timeout_usec % 1000000) ; | ^ ^~~~~~~~~ open62541.h:30253:16: error: unknown type name 'UA_SOCKET' 30253 | int UA_connect(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix connect implementation | ^~~~~~~~~ open62541.h:30253:63: error: unknown type name 'socklen_t' 30253 | int UA_connect(UA_SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);//equivalent to posix connect implementation | ^~~~~~~~~ open62541.h:30257:16: error: unknown type name 'UA_SOCKET' 30257 | void UA_fd_set(UA_SOCKET fd, fd_set *set); //equivalent to posix FD_SET implementation | ^~~~~~~~~ open62541.h:30257:30: error: unknown type name 'fd_set' 30257 | void UA_fd_set(UA_SOCKET fd, fd_set *set); //equivalent to posix FD_SET implementation | ^~~~~~ open62541.h:30261:17: error: unknown type name 'UA_SOCKET' 30261 | int UA_fd_isset(UA_SOCKET fd, fd_set *set);//equivalent to posix FD_ISSET implementation | ^~~~~~~~~ open62541.h:30261:31: error: unknown type name 'fd_set' 30261 | int UA_fd_isset(UA_SOCKET fd, fd_set *set);//equivalent to posix FD_ISSET implementation | ^~~~~~ open62541.h:30265:72: warning: 'struct addrinfo' declared inside parameter list will not be visible outside of this definition or declaration 30265 | int UA_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);//equivalent to posix getaddrinfo implementation | ^~~~~~~~ open62541.h:30287:37: error: unknown type name 'UA_SOCKET' 30287 | unsigned int UA_socket_set_blocking(UA_SOCKET sockfd);//set a socket as blocking. Returns 0 if OK, other value otherwise | ^~~~~~~~~ open62541.h:30291:40: error: unknown type name 'UA_SOCKET' 30291 | unsigned int UA_socket_set_nonblocking(UA_SOCKET sockfd);//set a socket as non-blocking. Returns 0 if OK, other value otherwise | ^~~~~~~~~ open62541.h:30295:69: error: unknown type name 'socklen_t' 30295 | int UA_getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); //equivalent to posix getsockopt implementation. Only in non windows architectures | ^~~~~~~~~ open62541.h:30299:75: error: unknown type name 'socklen_t' 30299 | int UA_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);//equivalent to posix setsockopt implementation | ^~~~~~~~~ open62541.h:30303:29: warning: 'struct addrinfo' declared inside parameter list will not be visible outside of this definition or declaration 30303 | void UA_freeaddrinfo(struct addrinfo *res);//equivalent to posix freeaddrinfo implementatio | ^~~~~~~~ open62541.h:30311:55: error: unknown type name 'socklen_t' 30311 | int UA_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);//equivalent to posix getsockname implementation
【问题讨论】:
【参考方案1】:请向他们投诉他们的构建说明。
【讨论】:
以上是关于在 Windows 中使用 open62541 构建一个简单的服务器的主要内容,如果未能解决你的问题,请参考以下文章
open62541 windows VS client/server 实例
open62541 windows VS client/server 实例
使用 Visual Studio 2017 构建 open62541 DLL