简体   繁体   中英

How to check if Xlib is used using #ifdef?

For example you can check if Windows is used by checking if "WIN32" macro is defined. And i would like to get the same behaviour but to check if Xlib is used. But i don't know what Xlib defines to know it's defined. If you don't know what i mean here is an example:

#ifdef WIN32 //Check if WIN32 is defined
//Do something
#endif

And i would like to change this in a way that it does something when Xlib used.

I'm sorry if there are some grammatical errors but i'm not a native english speaker.

The macro WIN32 is only defined, if you included #include <windows.h> before or set the macro in your compiler flags ( -DWIN32 ). For Xlib you could use the macro X_PROTOCOL very similar if you included #include <X11/Xh> before.

#ifdef X_PROTOCOL //Check if X_PROTOCOL is defined
//Do something
#endif

Another way would be to use _XLIB_H_ if you included #include <X11/Xlib.h> before, but I wouldn't because identifiers beginning with an underscore are reserved to the implementation and there is no guarantee that this identifiers won't change.

you can check if Windows is used by checking if "WIN32" macro is defined

The WIN32 macro is defined by default by compilers that target Windows OS. Its purpose is to let the program detect Windows OS API, not necessarily any graphic subsystem.

X11 is not and OS. It's optional userland software that installs and runs under many operating systems, including Windows. When compiling an X11 programs for Windows, WIN32 will be defined, just like for any other Windows program.

No compiler defines anything by default on a system where X11 is installed.

If you want to detect X11 at build time, your best bet is probably a meta-build tool like GNU autotools or CMake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM