[英]Define preprocessor macro for iOS
I have cpp files that are linked as headers to my application. 我有cpp文件作为标题链接到我的应用程序。 These cpp and header files are used with other applications as well. 这些cpp和头文件也与其他应用程序一起使用。 How do i define pre-processor macros for iphones, so when the application builds it uses the function for ios rather the the other for other platforms. 我如何为iPhone定义预处理器宏,以便在构建应用程序时将其用于ios,而将其用于其他平台。
For example: 例如:
How do i define a macro so the connect function uses getaddrinfo_compact when connecting with iOS rather than the simple socket connect function that i used. 如何定义宏,以便在与iOS连接时使用connectadd函数而不是我使用的简单套接字连接函数使用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 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;
}
Can you define something like 你能定义像
IOSBUILD=1
In the build settings for the target under 'Preprocessor Macros' (set it for all of the configurations eg debug and release). 在“预处理器宏”下的目标的构建设置中(为所有配置(例如,调试和发布)进行设置)。
Then in the C++ code, you can check whether this is an iOS build or not: 然后在C ++代码中,您可以检查这是否是iOS版本:
#ifdef IOSBUILD
// Use the compat version.
#else
// Use the original version.
#endif
That won't upset the other applications, unless they decide to define IOSBUILD too for some reason. 除非其他应用程序出于某种原因也决定定义IOSBUILD,否则这不会使其他应用程序感到不适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.