0

Lets assume we have redefinition problem with windows.h and winsock2.h .

I know how to solve it. To be 100% sure just add line _WINSOCKAPI_ in preprocessor definition. Or define #define _WINSOCKAPI_ befor each windows.h include. But I would like to go more subtile way and define _WINSOCKAPI_ just befor first include windows.h. How to know unit compilation order in order to place #define _WINSOCKAPI_ in right place?

Please correct me if my understanding of redefinition problem is wrong.

Community
  • 1
  • 1
vico
  • 17,051
  • 45
  • 159
  • 315
  • Err.. I don't think you can predict it consistently. And even if you could for a particular compiler, it would surely be non-portable. Go with the preprocessor directive. That's what they're for. – Baldrick Nov 27 '15 at 11:48

1 Answers1

0

I don't think you can predict the compilation order. I've not read the standards on this, but I think one of the premises of how c-code can be compiled is that every source file can be compiled independently. This enables you to do parallel builds etc.

So I wouldn't rely on your c/cpp files being compiled in a particular order.

However, the order that included files are processed is predictable; it is the order they are included in the c/cpp file.

I personally would include winsock2.h before windows.h as a solution to this problem rather than defining _WINSOCKAPI_, but defining that as a preprocessor def is an interesting idea.

Erik
  • 812
  • 1
  • 7
  • 15