Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created November 2, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silva97/29cd62e2f973c33406fb29afdf88a1e0 to your computer and use it in GitHub Desktop.
Save Silva97/29cd62e2f973c33406fb29afdf88a1e0 to your computer and use it in GitHub Desktop.
Conditional function by target O.S. or architecture
#include <stdio.h>
#include "target.h"
OS_LINUX(char *os, void)
{
return "Linux";
}
OS_WINDOWS(char *os, void)
{
return "Windows";
}
ARCH_AMD64(char *arch, void)
{
return "x86-64";
}
ARCH_I386(char *arch, void)
{
return "IA-32";
}
int main(void)
{
printf("%s | %s\n", os(), arch());
return 0;
}
#ifndef _TARGET_H
#define _TARGET_H
#ifdef __x86_64__
#define target_arch "AMD64"
#define target_amd64 1
#define ARCH_AMD64(func_sign, ...) func_sign(__VA_ARGS__)
#define ARCH_I386(func_sign, ...) static func_sign ## _discard_me()
#endif /* __x86_64__ */
#ifdef __i386__
#define target_arch "i386"
#define target_i386 1
#define ARCH_AMD64(func_sign, ...) static func_sign ## _discard_me()
#define ARCH_I386(func_sign, ...) func_sign(__VA_ARGS__)
#endif /* __i386__ */
#ifdef __unix__
#define target_os "UNIX"
#define target_unix 1
#define OS_LINUX(func_sign, ...) static func_sign ## _discard_me()
#define OS_WINDOWS(func_sign, ...) static func_sign ## _discard_me()
#define OS_UNIX(func_sign, ...) func_sign(__VA_ARGS__)
#endif /* __unix__ */
#ifdef __linux__
#undef target_os
#undef OS_LINUX
#define target_os "Linux"
#define target_linux 1
#define OS_LINUX(func_sign, ...) func_sign(__VA_ARGS__)
#endif /* __linux__ */
#if defined(__WINNT__) || defined(_WIN32)
#define target_os "Windows"
#define target_windows 1
#define OS_LINUX(func_sign, ...) static func_sign ## _discard_me()
#define OS_WINDOWS(func_sign, ...) func_sign(__VA_ARGS__)
#endif /* __WINNT__ */
#endif /* _TARGET_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment