I\'m having issues getting the C sockets API to work properly in C++ on z/OS.
Although I am including sys/socket.h
, I still get compile time errors telling m
Use the following c89 flag:
-D_OE_SOCKETS
Example:
bash-2.03$ c89 -D_OE_SOCKETS [filename].c
For more information look for c89 Options in the z/OS XLC/C++ User's Guide.
The _OE_SOCKETS appears to be simply to enable/disable the definition of socket-related symbols. It is not uncommon in some libraries to have a bunch of macros to do that, to assure that you're not compiling/linking parts not needed. The macro is not standard in other sockets implementations, it appears to be something specific to z/OS.
Take a look at this page:
Compiling and Linking a z/VM C Sockets Program
@Jax: The extern "C"
thing matters, very very much. If a header file doesn't have one, then (unless it's a C++-only header file), you would have to enclose your #include
with it:
extern "C" {
#include <sys/socket.h>
// include other similarly non-compliant header files
}
Basically, anytime where a C++ program wants to link to C-based facilities, the extern "C"
is vital. In practical terms, it means that the names used in external references will not be mangled, like normal C++ names would. Reference.
See the Using z/OS UNIX System Services sockets section in the z/OS XL C/C++ Programming Guide. Make sure you're including the necessary header files and using the appropriate #defines.
The link to the doc has changed over the years, but you should be able to get to it easily enough by finding the current location of the Support & Downloads section on ibm.com and searching the documentation by title.
Keep a copy of the IBM manuals handy:
The IBM publications are generally very good, but you need to get used to their format, as well as knowing where to look for an answer. You'll find quite often that a feature that you want to use is guarded by a "feature test macro"
You should ask your friendly system programmer to install the XL C/C++ Run-Time Library Reference: Man Pages on your system. Then you can do things like "man connect" to pull up the man page for the socket connect() API. When I do that, this is what I see:
FORMAT
X/Open
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
Berkeley Sockets
#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>
int connect(int socket, struct sockaddr *address, int address_len);
You may want to take a look to cpp-sockets, a C++ wrapper for the sockets system calls. It works with many operating systems (Win32, POSIX, Linux, *BSD). I don't think it will work with z/OS but you can take a look at the include files it uses and you'll have many examples of tested code that works well on other OSs.