title: C++ Using Gdiplus in Code::Blocks with MinGW compiler
body: <p><strong>My development environment:</strong>
  Windows 7 64bit
  Code::blocks 13.12 with MinGW compiler.
  Changed settings in Code::blocks - only in settings -> compiler -> linker settings -> other linker options: <code>-static-libgcc -static-libstdc++</code></p>
  
  <p><strong>This is my failed code:</strong>
  Main:</p>
  
  <pre><code>#include "main.h"
  
  #include &lt;iostream&gt;
  #include &lt;objidl.h&gt;
  #include &lt;Gdiplus.h&gt;
  
  
  using namespace std;
  using namespace Gdiplus;
  #pragma comment(lib, "gdiplus.lib")
  
  
  MainCode /// Look better even then main() :).
  {
  
      Execute_InitializeWindow; /// Initialize root window (For WinGUI)
  
  
  
      HWND hwnd = CreateWindowEx (
             0,                   /* Extended possibilites for variation */
             szClassName,         /* Classname */
             "Code::Blocks Template Windows App",       /* Title Text */
             WS_OVERLAPPEDWINDOW | WS_VISIBLE, /* default window */
             CW_USEDEFAULT,       /* Windows decides the position */
             CW_USEDEFAULT,       /* where the window ends up on the screen */
             544,                 /* The programs width */
             375,                 /* and height in pixels */
             HWND_DESKTOP,        /* The window is a child-window to desktop */
             NULL,                /* No menu */
             hThisInstance,       /* Program Instance handler */
             NULL                 /* No Window Creation data */
             );
  
  
  
      Graphics* hGraphics = Graphics::FromHWND(hwnd,false); /// &lt;---------------- HERE IT FAILS!
  
  
      /// Massage handler
      while (1){ if (GetMessage (&amp;msg, NULL, 0, 0) &lt;= 0) {break;}
  
          /* Translate virtual-key messages into character messages */
          TranslateMessage(&amp;msg);
          //cout &lt;&lt; msg &lt;&lt; endl;
          /* Send message to WindowProcedure */
          DispatchMessage(&amp;msg);
      }
  
  
      return msg.wParam; // The program return-value is 0 - The value that PostQuitMessage() gave
  }
  
  
  
  LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
      return DefWindowProc (hwnd, message, wParam, lParam);
  
      switch (message)                  /* handle the messages */
      {
  
  
              break;
          case WM_DESTROY:
              PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
              break;
          default:                      /* for messages that we don't deal with */
              //cout &lt;&lt; 1;
              return DefWindowProc (hwnd, message, wParam, lParam);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Heder for the main code (named as main.h):</p>
  
  <pre><code>#include &lt;windows.h&gt;
  
  
  
  
  /// ---------------------------------------- Codes for Initialize of main GUI -----------------------------------------
  
  #define MainCode int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
  
  LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); // Declare Windows procedure
  
  TCHAR szClassName[ ] = "my_program_root_name";
  
  
  #define Execute_InitializeWindow                        \
                                                          \
      MSG msg;                                            \
                                                          \
      WNDCLASSEX wincl;                                   \
      wincl.hInstance = hThisInstance;                    \
      wincl.lpszClassName = szClassName;                  \
      wincl.lpfnWndProc = WindowProcedure;                \
      wincl.style = CS_DBLCLKS;                           \
      wincl.cbSize = sizeof (WNDCLASSEX);                 \
                                                          \
                                                          \
      wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);     \
      wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);   \
      wincl.hCursor = LoadCursor (NULL, IDC_ARROW);       \
      wincl.lpszMenuName = NULL;                          \
      wincl.cbClsExtra = 0;                               \
      wincl.cbWndExtra = 0;                               \
      wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;    \
                                                          \
      if (!RegisterClassEx (&amp;wincl)) {return 1;}          \
  
  
  
  /// --------------------------------------------------------------------------------------------------
  </code></pre>
  
  <p><strong>Problem:</strong>
  Failing in line <code>Graphics* hGraphics = Graphics::FromHWND(hwnd,false);</code></p>
  
  <p>When I try to run this, I get this error:</p>
  
  <pre><code>mingw32-g++.exe   -c "D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.cpp" -o "D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o"
  mingw32-g++.exe  -o "D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.exe" "D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o"  -static-libgcc -static-libstdc++  
  D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o:main.cpp:(.text$_ZN7Gdiplus11GdiplusBasenwEj[__ZN7Gdiplus11GdiplusBasenwEj]+0xd): undefined reference to `GdipAlloc@4'
  D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o:main.cpp:(.text$_ZN7Gdiplus11GdiplusBasedlEPv[__ZN7Gdiplus11GdiplusBasedlEPv]+0xd): undefined reference to `GdipFree@4'
  D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o:main.cpp:(.text$_ZN7Gdiplus8GraphicsC1EP6HWND__i[__ZN7Gdiplus8GraphicsC1EP6HWND__i]+0x3a): undefined reference to `GdipCreateFromHWNDICM@8'
  D:\גיבויים\DATA\Desktop\cpp gdi plus problem\main.o:main.cpp:(.text$_ZN7Gdiplus8GraphicsC1EP6HWND__i[__ZN7Gdiplus8GraphicsC1EP6HWND__i]+0x57): undefined reference to `GdipCreateFromHWND@8'
  collect2.exe: error: ld returned 1 exit status
  Process terminated with status 1 (0 minute(s), 1 second(s))
  4 error(s), 0 warning(s) (0 minute(s), 1 second(s))
  </code></pre>
  
  <p>I spend a lot of time on it .. I do not know what to do. I realized that I need "def" file for Gdiplus.
  I found this: <a href="https://sourceforge.net/u/earnie/winapi/winapi/ci/master/tree/lib/gdiplus.def" rel="nofollow">https://sourceforge.net/u/earnie/winapi/winapi/ci/master/tree/lib/gdiplus.def</a></p>
  
  <p>And I tried to add it in the linker settings by adding this (in compiler -> linker settings -> other linker options): <code>-def gdiplus.def</code> .
  In this case the error was the same but this time with something different -
  during compile time, It prints lots of error about extracting functions from the dll. it failed to extract all functions from the dll. All failures with the same error message <code>... symbol not definded</code></p>
  
  <p>I feel a little lost .. I looked for solutions on Google but it did not help .. I am very new in C ++</p>
  
  <p>Thanks for helpers!</p>