-3
    //example code
    #include<iostream>
    #include<Windows.h>
    Int main()
    {
    BOOL MoveWindow( 
    HWND hWnd, 
    int X, 
    int Y, 
    int nWidth,
    int nHeight, 
    BOOL bRepaint 
    ); 
    return 0;
    }

I have used a similar function in some of my code recently and I was getting a linker error. I was under the impression MoveWindow was included with Windows.h however it wouldn't link.

After looking up MoveWindow, I decided to add 'User32.lib' to the additional dependencies. This fixed the problem but why I don't know? Isn't user32 inside windows.h as that is what the include needed is? What caused my dependency to be missing. I'm running the latest version of VS17.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
TrevorLee
  • 231
  • 2
  • 13
  • 1
    Not only won't it link, that code won't compile. –  Oct 02 '18 at 22:28
  • You can't just declare these arbitrarily. You should include the appropriate header. Linking and including are two independent things as far as the compiler's concerned and it's your responsibility to include and link correctly. – tadman Oct 02 '18 at 22:32
  • 2
    `.h` files are not "libraries". These are just *header files*. *Libraries* are completely separate entities, which normally have to be added to your project separately. Where do newbies get that strangely widespread idea that by including an `.h` file they somehow "include libraries" is entirely not clear to me. Linker errors do indeed often mean that you forgot to link some library into your project. But this has absolutely nothing to do with including an `.h` file. – AnT stands with Russia Oct 02 '18 at 22:33
  • 1
    @AnT Python's `import` command does both, for example, so it's a point of confusion for anyone who hasn't used a traditional compiled language. – tadman Oct 02 '18 at 22:34
  • @AnT an include file can have a directive that adds the appropriate library to the link phase automatically. – Mark Ransom Oct 02 '18 at 22:37
  • @tadman: Well, Microsoft compiler has that `#pragma comment(lib, ...)` feature, which allows one to tie a library to its header file (i.e. implicitly/automatically add the library to the project when the header is included). It often creates that illusion that including an `.h` file is all that's needed to "include a library". Some developers today are spoiled by that practice. Later, classic "header file plus library" packages catch them by surprise. – AnT stands with Russia Oct 02 '18 at 22:38
  • @AnT Truly any sufficiently fancy build environment is indistinguishable from magic. – tadman Oct 02 '18 at 22:40
  • Essential hint: indent your code correctly. – Jabberwocky Oct 03 '18 at 07:13

1 Answers1

3

Headers files and DLL import libs are two separate things. Header files are only used by the compiler. Lib files are only used by the linker. MOST header files DO NOT trigger a specific lib file to be linked (though that CAN be done, using #pragma comment(lib, filename) statements). It is the project's responsibility to specify the appropriate lib files it needs to link to, as you discovered. Just because you #include the windows.h header file does NOT guarantee that user32.lib and other Win32 API libs are linked automatically. However, most compilers that come with pre-defined Windows project templates will link against common Win32 API lib files by default. Apparently your project did not.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770