0

Trying to use CreateSolidBrush to change a window background color. I've included wingdi.h, I believe I've linked gdi32.lib ( however I converted gdi32.lib to a gdi32.a by using LIB2A, and I wonder if this may be an issue? ).

I wouldn't mind using another function but I worry this could be come a re-occuring issue if I'm not able to find a solution.

Some relevant code:

#include <stdio.h> 
#include <windows.h>
#include <wingdi.h>

#include <main.h>

DWORD CreateMainWindow(void)
{
    .............

    WNDCLASSEXA WindowClass = { 0 };

    WindowClass.hbrBackground = CreateSolidBrush(RGB(200, 200, 200));

    .............
}

I use a function to easily compile

int Compile()
{
        ................

        int result = 0;

        char *include = "C:\\Users\\Coding\\C\\src\\include";
        char *link = "C:\\Users\\Coding\\C\\src\\lib";
        char command[256];
        
        if(snprintf(
            command,
            sizeof(command), 
            "gcc -o main -I%s  -l gdi32 -L%s main.c", include, link) >= sizeof(command))
        {
            //exception catching and handling
        }               
        else
        {
            system(command);
        }

        return result;
}

I have no reason to believe the file isn't being linked as I'm not receiving an error.

Also I'm only using Notepad++, mingw64, and command prompt.

Shogery
  • 23
  • 1
  • 7
  • You're missing gdi32.lib. The conversion thing didn't seem to work – user253751 Sep 16 '22 at 01:02
  • I'm not sure why you want to change `gdi32.lib` into `gdi32.a`. See: https://stackoverflow.com/questions/2337949/whats-the-difference-between-lib-and-a-files Notably, `gdi32.lib` might be a link/reference to `gdi32.dll` and converting to `gdi32.a` might be an issue. You may want to put the `-L` option _before_ the `-l` option. But, usually `-lgdi32` will look for `libgdi32.X` and _not_ `gdi32.X` [where `X` is one of: lib, a, or dll]. To debug, you may wish to remove the `-L/-l` and just do full path: `/path_to_lib/gdi32.X` – Craig Estey Sep 16 '22 at 01:16
  • Why would GetLastError work, did you convert it the same way? – Anders Sep 16 '22 at 01:26
  • I changed to include just the path to the Gdi32.Lib as well as the path to libgdi32.a and still receive a no reference to CreateSolidBrush – Shogery Sep 16 '22 at 01:34
  • @Anders a lot of code is missing for ease of reading. I'll remove that as it's irrelevant – Shogery Sep 16 '22 at 01:36
  • Are you using `c` or `c++`? The `__imp_CreateSolidBrush` looks [somewhat] like a "mangled" name. Maybe try linking with `g++` if you've got C++ code (or the lib does). – Craig Estey Sep 16 '22 at 01:44
  • Hmm, I am writing in C, but could perhaps the library be a c++ library and is incompatible somehow? ... The library does appear to be c++ – Shogery Sep 16 '22 at 01:47
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Sep 16 '22 at 02:48

2 Answers2

0

The error is a linker error, because it can't find the shared library symbol CreateSolidBrush.

All that is needed is linker flag -lgdi32, so it links with MinGW's libgdi32.a.

Don't try to generate this file by converting it from some other file you found which is probably built with a totally different compiler. If you already experimented with that make sure to clean up any lingering gdi32 .a or .lib files from your previous attempts.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40
  • So I had tried to combine a copy of gdi32.dll from system32 and a copy of Gdi32.lib from window kits, as this is that was required by the Lib2a program, a lib and it's corresponding dll. I totally didn't realize MinGW had it's own gdi32 though, hopefully this will be the solution that's been evading me – Shogery Sep 16 '22 at 23:45
  • ... No good, still receiving undefined reference, including when just typed into command prompt manually. I'm aware it's a linker issue, but for some reason, the function is still not linked – Shogery Sep 17 '22 at 00:15
0

Well the answer was extremely simple, linkages and includes must come after the file.


C:\User> gcc main.c -lgdi32 -I<include path> -o main

If this was obvious then I apologize, hopefully this helps another confused individual

Shogery
  • 23
  • 1
  • 7