问题
I am attempting to create a robust header file that will compile on both windows and linux without need for changes. To this end I have an if statement in my includes that looks like
#if (!defined(__WINDOWS__))
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#endif
I am having problems with visual studio still attempting to include these headers despite the if statement resulting in errors like
error C1083: Cannot open include file: 'sys/time.h'
Is there a way to resolve this issue without removing all the linux blocks of code from the header?
回答1:
__WINDOWS__
is defined by windows.h
. So you have a circularity. Use this instead:
#if defined(_WIN32)
# if defined(_WIN64)
/*64 bit windows*/
# else
/*32 bit windows*/
# endif
#endif
Yes, _WIN32 is defined on 64 bit windows. I kid you not.
来源:https://stackoverflow.com/questions/22384020/visual-studio-trying-to-include-linux-headers-despite-if-statement