I'm trying to link object files generated by MinGW gcc with ld.exe supplied with MinGW to create a final 32 bit executable on Windows 10 64 bit . The code is more than simple enough. I have a maindefined.c file with the main function:
void main(int argc, char **argv) {}
For the mainCRTStartup I'm using a custom C startup code nocrt0 I found on GitHub that claims is fully compatible with MinGW: https://github.com/matveyt/nocrt0 which parses the command line arguments and calls main() by supplying these arguments.
I've put this startup code nocrt0c.c (as I'm trying to develop a simple console application) on a file called maincaller.c
I'm creating object file for these two C source via the MinGW gcc command line
gcc -g -O -c maincaller.c -m32
gcc -g -O -c maindefined.c -m32
I've looked through the nocrt0 startup code and found it uses the GetCommandLine()
API exported by kernel32.dll to fetch the command line arguments before parsing them and supplying them to main, so during linking with ld I supplied the 32 bit version of kernel32.dll on my PC in the input as well
ld --verbose -o output.exe maincaller.o maindefined.o "c:\windows\syswow64\kernel32.dll"
This throws no unresolved external error as expected, but the final output.exe produced is a zero kb file with absolutely no input. Ld verbose doesn't emit any useful error output either. What could be going wrong? Would love some help regarding this
This turned out to be a silly issue but I want to leave it here regardless for anyone checking in later. The Symantec Antivirus on my PC was deleting the content of the entire binary after it was successfully being created by ld.exe. So if you're doing anything MinGW or linking related with any other toolchain, a good idea would be to disable any Antivirus/Access Protection solution that you have.