我有cpp文件,它们作为头文件链接到我的应用程序。这些cpp和头文件也可用于其他应用程序。如何为iphones定义预处理器宏,以便在应用程序构建时使用ios的函数,而不是其他平台的函数。
例如:
如何定义宏,使connect函数在使用iOS连接时使用getaddrinfo_compact,而不是我使用的简单套接字连接函数。
bool SocketSender::Connect (const char *host, int port, CApiError &err)
{
errno = 0;
struct hostent *hostinfo;
//struct in6_addr ipv6addr;
hostinfo = gethostbyname(host);
struct addrinfo hints, *res, *res0;
int error,result;
const char *cause = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//hints.ai_flags = AI_DEFAULT;
hints.ai_protocol = 0;
error = getaddrinfo_compat(host, boost::to_string(port).c_str(), &hints, &res0);
if (error) {
errx(1, "%s", gai_strerror(error));
/*NOTREACHED*/
}getaddrinfo_compact
static int getaddrinfo_compat(
const char * hostname,
const char * servname,
const struct addrinfo * hints,
struct addrinfo ** res
) {
int err;
int numericPort;
// If we're given a service name and it's a numeric string, set `numericPort` to that,
// otherwise it ends up as 0.
numericPort = servname != NULL ? atoi(servname) : 0;
// Call `getaddrinfo` with our input parameters.
err = getaddrinfo(hostname, servname, hints, res);
// Post-process the results of `getaddrinfo` to work around <rdar://problem/26365575>.
if ( (err == 0) && (numericPort != 0) ) {
for (const struct addrinfo * addr = *res; addr != NULL; addr = addr->ai_next) {
in_port_t * portPtr;
switch (addr->ai_family) {
case AF_INET: {
portPtr = &((struct sockaddr_in *) addr->ai_addr)->sin_port;
} break;
case AF_INET6: {
portPtr = &((struct sockaddr_in6 *) addr->ai_addr)->sin6_port;
} break;
default: {
portPtr = NULL;
} break;
}
if ( (portPtr != NULL) && (*portPtr == 0) ) {
*portPtr = htons(numericPort);
}
}
}
return err;
}发布于 2016-08-18 00:00:31
你能定义像这样的东西吗
IOSBUILD=1
在“预处理器宏”下的目标的构建设置中(将其设置为所有配置,例如调试和发布)。

然后,在C++代码中,您可以检查这是否是iOS构建:
#ifdef IOSBUILD
// Use the compat version.
#else
// Use the original version.
#endif这不会扰乱其他应用程序,除非它们出于某种原因也决定定义IOSBUILD。
https://stackoverflow.com/questions/39000889
复制相似问题