Search code examples
c++cgcccygwinmingw

mingw very very simple code, report undefined reference to XXX


extern int aabbcc;
void fun1()
{
    aabbcc = 1;
}

compile it with mingw

i686-w64-mingw32-gcc -shared -o a.dll a.c

reports error: undefined reference to 'aabbcc'

compile it with linux gcc, it is ok

gcc -fPIC -shared -o liba.so a.c

WHY ? what is the different between linux-gcc and mingw ?


Solution

  • In Linux, .so files are allowed to have undefined references. When the .so is loaded into a process, it tries to find the reference in any other .so or the main program. The .so doesn't even have to know where the referenced object is located.

    In Windows, .dll files are not allowed to have undefined references. The .dll has to either define the referenced object itself, or tell the loader which other .dll to find the object in.