I was following this tutorial on how to create a static library and the author defines a function(for the library) in the lib_mylib.c
as:
#include <stdio.h> //Although I use <iostream>
void fun(void)
{
printf("fun() called from a static library");
}
He goes ahead and simply compiles this as:
gcc -c lib_mylib.c -o lib_mylib.o
Notice that there's no main()
defined which would be the default entry point for the programs but he mentions nothing about that, and to me it seems logical because libraries are not exactly stuff we run, but something we include for our programs to run and hence our source code should be the one to have the main()
function.
But, the minGW g++ compiler says this:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
and according to this question I am not defining the main and the error does seem to go away once I define the main() function. Can anyone please explain to me what is going under the hood and demistify this confusion.
PS: It'd be really helpful if you mention any resources for such learning. Thanks in advance.