2

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.

ccxjn
  • 21
  • 2
  • 1
    The [order that you link matters](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc). Try `gcc demo1.c -lcomdlg32 -o demo` – yano Apr 28 '22 at 05:19
  • Also you're giving `demo1.c` instead of `demo.c`. – Avinash Apr 28 '22 at 05:20
  • Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – phuclv Apr 28 '22 at 08:54

1 Answers1

0

Thank yano, the question was solved!

just usegcc demo1.c -lcomdlg32 -o demo

ccxjn
  • 21
  • 2
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Jeaninez - MSFT May 03 '22 at 03:12