Can anyone give me the solution, for changing the entry point instead of main function.
How to change the entry point main() in c langauge
Collapse
X
-
Tags: None
-
Check your linker options. There is usually an option where you can specify the entry point function. main() is just the default.
If you are using Visual Studio.NET you select project properties->Configuratio n Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.Comment
-
Originally posted by weaknessforcatsCheck your linker options. There is usually an option where you can specify the entry point function. main() is just the default.
If you are using Visual Studio.NET you select project properties->Configuratio n Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.Comment
-
OK, I went thorugh the complete thread but there are some doubts---->
1. Why would one like to do something like this? (could not find appropriate explanation about this on GOOGLE) What are it's practical uses?
2. Even if it's possible with some practical uses, this contradicts the standard i.e.
According to Ritchie and Kernigham's "The C programming language" book (page10-second edition-ANSI C)-
"........yo ur program begins executing at the beginning of main. This means that every program must have a main somewhere"
So if program begins executin at main, how's this possible? Doesn't it contradicts the standard?
_NOTE_I may be wrong here as I am a newbie
PLUS I don't own a latest edition of The C programming language BOOK by Dennis Ritchie and Brian Kernigham
PLUS I have just started reading the above mentioned book for the first time.
So please guide me....
THANKS TO EVERY ONE IN ADVANCE........ .......
============
AmbrNewlearner
============Comment
-
K&R C hasn't been standard for nearly 20 years now, though it's still a great book. As a beginner, yes, everything starts with main. This is because when you begin execution of a compiled C program, there is a function that calls main. By changing the entry point, you change what function that calls (I think). I'm not entirely sure why it would be useful, but I'm sure there are uses.Comment
-
Only use I could come up with is to create "different" programs from the same source code.
For example I suppose you could compile with "DebugMain" as your entry point to perform lots of other little things or something? And then compile with "ReleaseMai n" as entry point when you are satisfied.
A quick change in the tool chain could do these things automatically, perhaps easier for developing?Comment
-
Why would one like to do something like this?
Even if it's possible with some practical uses, this contradicts the standard
So if program begins executin at mainComment
-
Originally posted by oler1sTechnically, main is not the entry point. It’s compiler implemented. That compiler implementation calls main(). From a standards perspective, the compiler implementation is irrelevant.
The processor has a start-up mechanism as follows
1. Jump to the location stored in program address 0
2. Execute from there
This code all has to be implemented in assembler and includes initialising the C data segments as required as well as other platform related stuff like initialising the memory management unit.
A default implementation of this code is provided with the tool chain and the last thing it does is
bsr $main
If you don't know bsr is the assembler directive to branch to subroutine, basically start running from main. It could be anything though.
All programs written in C have this sort of start-up code in them somewhere, something has to initialise the C data segments (stack, heap, global data etc). But on some platforms, PCs for instance this is hidden from you by the tool chain. On many embedded platforms this is available for editing but a default implementation is provided.Comment
-
Originally posted by JosAHFor gcc try the '-e <symbol>' flag to make your program start at <symbol>
instead of the default 'main' entry point.
kind regards,
Jos
$>cc -e test test.c -o test
#include <stdio.h>
int test(void)
{
printf("Hai\n") ;
main();
return 1;
}
main()
{
printf("OK\n");
}
The output is as follows :
Hai
OK
Segmentation fault
Can anyone tell what's the reason for the Segmentation fault
Thanks in advanceComment
-
Originally posted by Banfayou have not declared the return type of main
[code=c]
int main()
{
}
[/code]
has the responsibility of returning to the OS again so it can perform its process
cleanup code and other bookkeeping stuff. The test() function simply returns;
but there is no place to return to and hence the segmentation fault.
That function should call the exit() function instead; maybe that'll work ...
kind regards,
JosComment
-
Originally posted by JosAHFor gcc try the '-e <symbol>' flag to make your program start at <symbol>
instead of the default 'main' entry point.
kind regards,
Jos
Well, I wrote a simple code in C. It is given below--->
[CODE=C]#include <stdio.h>
int test(void);
int main()
{
int a=1;
printf("\nHELLO main %d",a);
printf("\n\nPRE SS ANY KEY TO EXIT");
getch();
return(0);
}
int test(void)
{
int val;
printf("\nHELLO test()");
val=main();
return(0);
}
[/CODE]
Now, what I want is that test() must be entry point instead of main(). What should I do?
THANKS IN ADVANCE........ .....
============
AmbrNewlearner
============Comment
Comment