'Undefined reference to GetConsoleWindow

I am trying to get a handle for the console window with the following:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

#define NTDDI_WIN7 as 0x06010000
#define _WIN32_WINNT as 0x0500

int main(int argc, char *argv[]) {
    HWND self = GetConsoleWindow();
    /* some more code */
    return 0;
}

I followed the instructions from the GetConsoleWindow documentation and "Using the Windows Headers", but I still get:

undefined reference to `GetConsoleWindow'



Solution 1:[1]

You have to have kernel32.lib in your list of input libraries.

Solution 2:[2]

remove gdi32.def kernel32.def msvcrt.def user32.def files from C:\tcc\lib; remove all .def files from C:\tcc\lib

#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <conio.h>

void main()
{
    HWND hwnd;   
    hwnd = GetConsoleWindow(); 
    HDC hdc;
    hdc = GetWindowDC(hwnd);
    
    printf("console hwnd:  %p\n", hwnd);
    printf("console hdc:  %p\n", hdc);
    
    HPEN hPenNull, hPenBlack, hPenRed, hPenGreen, hPenBlue;
    hPenNull=GetStockObject(NULL_PEN); 
    hPenBlack=CreatePen(PS_SOLID, 2, RGB(0,0,0)); 
    hPenRed=CreatePen(PS_SOLID, 2, RGB(255,0,0));
    hPenGreen=CreatePen(PS_SOLID, 2, RGB(0,255,0)); 
    hPenBlue=CreatePen(PS_SOLID, 2, RGB(0,0,255)); 

    HBRUSH hBrushNull, hBrushBlack, hBrushRed, hBrushGreen, hBrushBlue, hBrushYellow;
    hBrushNull=GetStockObject(NULL_BRUSH); 
    hBrushBlack=CreateSolidBrush(RGB(0,0,0)); 
    hBrushRed=CreateSolidBrush(RGB(255,0,0)); 
    hBrushYellow=CreateSolidBrush(RGB(255,255,0)); 
    hBrushGreen=CreateSolidBrush(RGB(0,255,0)); 
    hBrushBlue=CreateSolidBrush(RGB(0,0,255)); 
    
    SelectObject(hdc, hPenRed);
    SelectObject(hdc, hBrushYellow);
    Ellipse(hdc, 200,50,260,150);

    SelectObject(hdc, hPenNull);
    SelectObject(hdc, hBrushRed);
    Ellipse(hdc, 140, 80, 180, 120);
    
    SelectObject(hdc, hPenBlue);
    SelectObject(hdc, hBrushNull);
    Ellipse(hdc, 280, 50, 340, 150);
    
getch();
}

-L"C:\tcc\lib" -lkernel32 -luser32 -lgdi32 -Wl,-subsystem=console

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SoronelHaetir
Solution 2