Search code examples
c++compiler-errorsvisual-studio-2019in-addr

Compilation errors with VS-2019


The code below does not compile. I get two errors:

    _TCHAR* pStrAddress;
    ... some stuff here...
    IN_ADDR  sa;
    InetPton(AF_INET, pStrAddress, &sa);
    *pIP = sa.S_un.S_addr;

1) IN_ADDR --> error C2065: 'AF_INET': undeclared identifier

2) InetPton(...) --> C3861: 'InetPton': identifier not found

My configuration is as follows:

  • VS-2019 pro
  • Windows SDK 10.0.18362.0 (Latest as of 9/26/2019)
  • When I press F12 (Go to definition) the following files opens in the editor (C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared\inaddr.h and C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\WS2tcpip.h).
  • The project settings "C/C++ | General | Additional include directories" has the "Inherit from parent or project default" checkbox checked.
  • Ws2_32.lib is present in "linker | Additional dependencies" (but my question is not about link issue, or not there yet)
  • My program uses unicode and InetPton(...) expands to InetPtonW, which is correct.

To resume, from within the editor, the symbols are accessible, but the preprosessor/compiler don't seem to have the same paths. Obviously I am missing something most probably obvious. Your help will be immensely appreciated.

Thanks!


Solution

  • Found it! I created a small project with just the code of interest to post as an example of code and everything was compiling fine. So, something must be obvious in the first place. Then it hit me!: The precompiled header was not the first include.

    Thanks guys for your help!

    BAD code:

        #include <windows.h>
        #include <ws2tcpip.h>
        #include <stdlib.h>
    
        #include "pch.h"   <-- this must be the first include!!!!!!
    

    GOOD code:

        #include "pch.h"   <-- ok, happy compiler and developer  ;)
    
        #include <windows.h>
        #include <ws2tcpip.h>
        #include <stdlib.h>