NO

Author Topic: How to build without CRT ?  (Read 2180 times)

Offline bitcoin

  • Member
  • *
  • Posts: 179
How to build without CRT ?
« on: September 02, 2021, 08:45:03 PM »
Hello, I write some simple program with option "ignore standart places " (no crt).

In Linker section I add entry_point as ep. In code I write

Code: [Select]
int ep(void)
but this fails. Also fails , if write in linker section _ep, _ep@4 and other.

But if I write in code
Code: [Select]
int ep()it works , but I have warnings "old style"

Offline Vortex

  • Member
  • *
  • Posts: 797
    • http://www.vortex.masmcode.com
Re: How to build without CRT ?
« Reply #1 on: September 02, 2021, 11:20:07 PM »
Hi bitcoin,

The entry point should be ep as you mentioned.

Attached is a quick example for you.
Code it... That's all...

Offline bitcoin

  • Member
  • *
  • Posts: 179
Re: How to build without CRT ?
« Reply #2 on: September 03, 2021, 12:41:29 AM »
Thank you Vortex, your sample works good..save it as template )

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to build without CRT ?
« Reply #3 on: September 03, 2021, 08:57:05 AM »
In Pelle's C help is a topic  Program startup: the true story

linker knows these:
mainCRTStartup
wmainCRTStartup
WinMainCRTStartup
wWinMainCRTStartup
DllMainCRTStartup


May the source be with you

Offline Vortex

  • Member
  • *
  • Posts: 797
    • http://www.vortex.masmcode.com
Re: How to build without CRT ?
« Reply #4 on: September 03, 2021, 09:38:03 AM »
Hi bitcoin,

You are welcome. Here is how to build the project with a batch file.

After running \PellesC\Bin\povars32.bat :

Code: [Select]
pocc.exe /Ze /Zx -Tx86-coff CustEntryPoint.c

pocc.exe /Ze /Zx -Tx86-coff StdOut.c

pocc.exe /Ze /Zx -Tx86-coff StrLen.c

polink /SUBSYSTEM:CONSOLE /ENTRY:ep /LIBPATH:\Pellesc\Lib\Win CustEntryPoint.obj StdOut.obj StrLen.obj kernel32.lib user32.lib
Code it... That's all...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to build without CRT ?
« Reply #5 on: September 03, 2021, 10:02:30 AM »
For lazy programmer, to sourcecode:
Code: [Select]
#pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:ep")
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: How to build without CRT ?
« Reply #6 on: September 03, 2021, 01:44:12 PM »
Hello, I write some simple program with option "ignore standart places " (no crt).

In Linker section I add entry_point as ep. In code I write

Code: [Select]
int ep(void)
but this fails. Also fails , if write in linker section _ep, _ep@4 and other.

But if I write in code
Code: [Select]
int ep()it works , but I have warnings "old style"
Probably you compiled your code with MS extensions on and __stdcall as default.
The entry point name can be the base name or the underscore decorated if compiling as __cdecl your function, that automatically happen when you declare it as 'int ep()'.
So you have 2 options declare your function as:
Code: [Select]
int __cdecl ep(void)and the entry point as 'ep' or '_ep', or if you compile the function as __stdcall use the entry point '_ep@0' with full decorations.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline bitcoin

  • Member
  • *
  • Posts: 179
Re: How to build without CRT ?
« Reply #7 on: September 03, 2021, 10:46:45 PM »
Yes, if change to _cdecl, all works also.

Thanks all for the answers!

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to build without CRT ?
« Reply #8 on: September 04, 2021, 04:07:01 PM »
A NoCrt library is also usually needed, as compiler needs support library, like SEH in x86 mode.
Code: [Select]
#include <excpt.h>

void __stdcall ExitProcess(int);
int __cdecl main(void);
void __cdecl mainCRTStartup(void) { ExitProcess(main()); }

int __cdecl main(void)
{
__try
{
char *p = 0;
*p = 0; /* force a null-pointer exception */
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
//printf("An exception occurred - the code is %x\n", exception_code());
}
return 0;
}
map-file
Code: [Select]
0001:00000078       __except_handler3          00401078 f   crt:seh2.obj
 0001:00000150       __seh_longjmp_unwind@4     00401150 f   crt:seh2.obj
 0001:00000170       __global_unwind2           00401170 f   crt:seh1.obj
 0001:00000190       __unwind_handler           00401190 f   crt:seh1.obj
 0001:000001c0       __local_unwind2            004011c0 f   crt:seh1.obj
 0001:0000021f       __NLG_Return2              0040121f     crt:seh1.obj
 0001:00000230       __abnormal_termination     00401230 f   crt:seh1.obj
 0001:00000260       __NLG_Notify1              00401260     crt:seh1.obj
 0001:00000269       __NLG_Notify               00401269     crt:seh1.obj
 0001:0000027d       __NLG_Dispatch             0040127d     crt:seh1.obj
 0002:00000010       __load_config_used         00402010     crt:_loadcfg.obj
 0003:00000068       __NLG_Destination          00403068     crt:seh1.obj

https://forum.pellesc.de/index.php?topic=7264.msg27613#msg27613
« Last Edit: September 06, 2021, 09:46:50 AM by TimoVJL »
May the source be with you