Pelles C forum

C language => Beginner questions => Topic started by: x4nd3r on April 27, 2016, 11:32:56 PM

Title: Compilation settings for smaller size
Post by: x4nd3r on April 27, 2016, 11:32:56 PM
Hello! Decided to test Pelles C ide, chosed Win32 Program from project menu.
Setted Project settings -> Compiler -> Runtime Library to Multithreaded (DLL). This reduced my exe file from 17kb to 2kb. Launch/Debug from Pelles C works fine. But when I tried to lauch not from ide, I got error about pocrt.dll: "The program can't start because pocrt.dll is missing from your computer. Try reinstalling the program to fix this problem."
This is my code:
Code: [Select]
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
return MessageBox(NULL,"Win Computer", "Ok", MB_OK);
}

After googling, decided to set settings back and set compiler settings via pragmas:
Code: [Select]
#include <windows.h>

#pragma comment(linker, "/NODEFAULTLIB")
#pragma comment(linker, "/entry:myMain")

int myMain()
{
return MessageBox(NULL,"Win Computer", "Ok", MB_OK);
}
And this works!

So, why I got an error with this pocrt.dll, and what I got or lost, using my own entry point?
Title: Re: Compilation settings for smaller size
Post by: frankie on April 28, 2016, 09:37:31 AM
The first error message informs you that the path of the DLL is not in your default paths. The options are:
The last option is what the IDE does in the context of the process where is executed, or debugged, the program, and that's why it works.

When you put your own entry point it works anyway because you are bypassing any CRT code so the executable simply doesn't load anymore the library.
Title: Re: Compilation settings for smaller size
Post by: x4nd3r on May 02, 2016, 07:13:09 PM
But why at pelles c I got linked this libabry, when at visual studio not?
"Project settings -> Compiler -> Runtime Library to Multithreaded (DLL)" This option don't working properly?
Title: Re: Compilation settings for smaller size
Post by: TimoVJL on May 02, 2016, 07:32:09 PM
pocrt.dll is PellesC specific runtime library.
With msvc you face similar problem, if version specific dll isn't installed.
Title: Re: Compilation settings for smaller size
Post by: x4nd3r on May 02, 2016, 08:37:16 PM
pocrt.dll is PellesC specific runtime library.
With msvc you face similar problem, if version specific dll isn't installed.
So, with this pragma's:
Code: [Select]
#pragma comment(linker, "/NODEFAULTLIB")
#pragma comment(linker, "/entry:myMain")
as I understood, I can avoid including default crt libraries in PellesC and Visual Studio?
Title: Re: Compilation settings for smaller size
Post by: TimoVJL on May 03, 2016, 08:37:16 AM
Sure.
If you compile from commandline:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// cl -Zl -GS- TestMinWin1.c
//#pragma comment(linker, "-nodefaultlib") // use cl /Zl omit default library name in .OBJ
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(linker, "-subsystem:windows -entry:myMain")

void __cdecl myMain(void)
{
MessageBox(NULL,"Win Computer", "Ok", MB_OK);
ExitProcess(0);
}

Support file for console program using OS msvcrt.dll
Code: [Select]
// msvcrt_main.c
#pragma comment(lib, "msvcrtXP.lib")
#ifdef _WIN64
#pragma comment(linker,"/subsystem:console,5.2")
else
#pragma comment(linker,"/subsystem:console,5.1")
#endif
void __cdecl mainCRTStartup(void)
{
int __cdecl main(int argc, char **argv);
__declspec(dllimport) int __cdecl __getmainargs(int*, char***, char***, int, void*);
__declspec(dllimport) void __cdecl exit(int status);

int    argc;
char** argv;
char** env;
int    sinfo = 0;

__getmainargs(&argc,&argv,&env,0,&sinfo);
exit(main(argc,argv));
}
Title: Re: Compilation settings for smaller size
Post by: frankie on May 03, 2016, 09:48:44 AM
Of course if you remove standard libraries and relative initializations almost all subsystems, I/O, memory management, etc, wil not work properly.
You will have UB (undefined behavior) on all standard lib functions...

P.S. I forgot to add that for the MSVC case the runtime libraries are generally already present in a standard path and under windows directory. This is the reason because you don't have the problem with MSVC.