NO

Author Topic: Compilation settings for smaller size  (Read 4086 times)

x4nd3r

  • Guest
Compilation settings for smaller size
« 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?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Compilation settings for smaller size
« Reply #1 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:
  • Put a copy of the pocrt.dll in the exe directory.
  • Put a copy of pocrt.dll in windows system directory.
  • Add the path to PellesC binaries, where is pocrt.dll, to your environment PATH.
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.
« Last Edit: April 28, 2016, 11:36:08 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

x4nd3r

  • Guest
Re: Compilation settings for smaller size
« Reply #2 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?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Compilation settings for smaller size
« Reply #3 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.
May the source be with you

x4nd3r

  • Guest
Re: Compilation settings for smaller size
« Reply #4 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?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Compilation settings for smaller size
« Reply #5 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));
}
« Last Edit: April 10, 2017, 08:52:24 AM by TimoVJL »
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Compilation settings for smaller size
« Reply #6 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.
« Last Edit: May 03, 2016, 11:27:07 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide