Visual Studio trying to include linux headers despite if statement

百般思念 提交于 2021-01-27 23:44:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!