Error: "undefined reference to `__imp_GetOpenFileNameA'" Although I use gcc -lcomdlg32

Viewed 142

I creat a file named "demo.c". Here is the code:

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

int FileDialog(char *path)
{
    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = path;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    return GetOpenFileName(&ofn);
}

int main(char argc, char *argv[])
{
    char szFile[MAX_PATH] = {0};
    if (FileDialog(szFile))
    {
        puts(szFile);
    }
    return 0;
}

Then I enter "gcc -lcomdlg32 demo.c -o demo" in cmd. But the result is error:

D:\MyProject\C_Programming\WindowsApiTest>gcc -lcomdlg32 demo1.c -o demo 
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\c\AppData\Local\Temp\ccqGNley.o:demo1.c:(.text+0x64): undefined reference to `__imp_GetOpenFileNameA'
collect2.exe: error: ld returned 1 exit status

So why do it report an error?

My operating system is win11, and my GCC is tdm-gcc-64.

1 Answers

Thank yano, the question was solved!

just usegcc demo1.c -lcomdlg32 -o demo

Related