473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to change the entry point main() in c langauge

Can anyone give me the solution, for changing the entry point instead of main function.
Jun 3 '08 #1
25 28441
r035198x
13,262 8TB
Can anyone give me the solution, for changing the entry point instead of main function.
And why would you want to do that?
Jun 3 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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->Configuration Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.
Jun 3 '08 #3
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->Configuration Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.
sorry for delayed reply.. Im working as developer, we develop applications in Linux OS, so i want to know in linux
Jun 5 '08 #4
JosAH
11,448 Expert 8TB
For gcc try the '-e <symbol>' flag to make your program start at <symbol>
instead of the default 'main' entry point.

kind regards,

Jos
Jun 5 '08 #5
AmberJain
884 Expert 512MB
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)-
"........your 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
============
Jun 11 '08 #6
Laharl
849 Expert 512MB
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.
Jun 11 '08 #7
Plater
7,872 Expert 4TB
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 "ReleaseMain" as entry point when you are satisfied.
A quick change in the tool chain could do these things automatically, perhaps easier for developing?
Jun 11 '08 #8
oler1s
671 Expert 512MB
Why would one like to do something like this?
Writing a non-standard program. An example would be a Windows program, which uses WinMain, not main. Another would be the Boost.Test main for testing.

Even if it's possible with some practical uses, this contradicts the standard
Yep. The implications of writing OS specific or API specific programs.

So if program begins executin at main
Technically, main is not the entry point. It’s compiler implemented. That compiler implementation calls main(). From a standards perspective, the compiler implementation is irrelevant.
Jun 11 '08 #9
Banfa
9,065 Expert Mod 8TB
Technically, main is not the entry point. It’s compiler implemented. That compiler implementation calls main(). From a standards perspective, the compiler implementation is irrelevant.
This becomes very obvious on some embedded platforms like the one I am using now.

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.
Jun 11 '08 #10
For gcc try the '-e <symbol>' flag to make your program start at <symbol>
instead of the default 'main' entry point.

kind regards,

Jos
This is the test program I have written and compiled with
$>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 advance
Jun 18 '08 #11
Banfa
9,065 Expert Mod 8TB
you have not declared the return type of main

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. }
  4.  
Jun 18 '08 #12
JosAH
11,448 Expert 8TB
you have not declared the return type of main

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. }
  4.  
That's not it: the -e flag changes the address of the startup code. Startup 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,

Jos
Jun 18 '08 #13
r035198x
13,262 8TB
An exit status should do the trick.
@OP You'd need to include stdlib for that of course.
Jun 18 '08 #14
AmberJain
884 Expert 512MB
For gcc try the '-e <symbol>' flag to make your program start at <symbol>
instead of the default 'main' entry point.

kind regards,

Jos
I'm using Bloodshed Dev c++ 4.9.9.2 with Mingw port of GCC (GNU Compiler Collection) as its compiler.

Well, I wrote a simple code in C. It is given below--->
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int test(void);
  4.  
  5. int main()
  6. {
  7.     int a=1;
  8.     printf("\nHELLO main %d",a);
  9.     printf("\n\nPRESS ANY KEY TO EXIT");
  10.     getch();
  11.     return(0);
  12. }
  13.  
  14. int test(void)
  15. {
  16.    int val; 
  17.    printf("\nHELLO test()");
  18.    val=main();
  19.    return(0);
  20. }
  21.  
Now, what I want is that test() must be entry point instead of main(). What should I do?

THANKS IN ADVANCE.............

============
AmbrNewlearner
============
Jun 19 '08 #15
JosAH
11,448 Expert 8TB
Now, what I want is that test() must be entry point instead of main(). What should I do?
My question is: why do you want to do that? It doesn't buy you much and you
have to take care that your test() function returns to the OS properly; simply
returning doesn't do it. That -e flag tells the linker the name of the startup code.
Normally that'd be c0 or crt0 or similar that does all the nitty gritty work for you.
Why do it the difficult way when it has been made easy for you?

Since you're a beginner, writing a proper startup (and exit) functionality is way
over your head; don't go there now but write proper C instead.

kind regards,

Jos
Jun 19 '08 #16
An exit status should do the trick.
@OP You'd need to include stdlib for that of course.
Thanks for the reply.. it is working..
But my question is.. if the exit is so required for C program.. then even if we dont give the exit in main function.. it doesnt give any seg. fault..
What is the reason behind this????
Jun 23 '08 #17
r035198x
13,262 8TB
Thanks for the reply.. it is working..
But my question is.. if the exit is so required for C program.. then even if we dont give the exit in main function.. it doesnt give any seg. fault..
What is the reason behind this????
Changing your entry point from main also means that your exit point has changed. You don't get the seg fault if you put the exit status in the entry point function.
Jun 23 '08 #18
AmberJain
884 Expert 512MB
My question is: why do you want to do that? It doesn't buy you much and you
have to take care that your test() function returns to the OS properly; simply
returning doesn't do it. That -e flag tells the linker the name of the startup code.
Normally that'd be c0 or crt0 or similar that does all the nitty gritty work for you.
Why do it the difficult way when it has been made easy for you?

Since you're a beginner, writing a proper startup (and exit) functionality is way
over your head; don't go there now but write proper C instead.

kind regards,

Jos
OK, for the time being (till I get A LITTLE expertise in C) I will stick to as you advised.

Thanks JosAH.....

============
AmbrNewlearner
============
Jun 24 '08 #19
JosAH
11,448 Expert 8TB
OK, for the time being (till I get A LITTLE expertise in C) I will stick to as you advised.

Thanks JosAH.....
You're welcome of course. Just to lift a tip of the curtain, the following code shows
(conceptually) what startup code must do; the next function is the target of any
-e flag:

Expand|Select|Wrap|Line Numbers
  1. void startup() {
  2.    int argc= system_dependent_argc();
  3.    char** argv= system_dependent_argv();
  4.  
  5.    exit(main(argc, argv)); /* call main and exit */
  6. }
  7.  
  8. void exit(int exit_code) {
  9.    system_dependent_exit(exit_code);
  10. }
  11.  
You figure out the system_dependent_* functions and you're in business.

kind regards,

Jos

ps. As a side effect the above code snippet perfectly show why main() is supposed
to return an int and not void as Microsoft keeps suggesting.
Jun 24 '08 #20
Banfa
9,065 Expert Mod 8TB
You're welcome of course. Just to lift a tip of the curtain, the following code shows
(conceptually) what startup code must do;
Don't you think that the start-up code would also include initialising all the data-segments that are expected to exist, heap, stack, initialised global data, uninitialised global data, constant data, etc. plus processor registers (some/many processors have stack registers) for the given platform?
Jun 24 '08 #21
JosAH
11,448 Expert 8TB
Don't you think that the start-up code would also include initialising all the data-segments that are expected to exist, heap, stack, initialised global data, uninitialised global data, constant data, etc. plus processor registers (some/many processors have stack registers) for the given platform?
No, I consider that a job of the 'binder' i.e. the loader-editor that is supposed to
resolve symbolic or relative addresses to virtual addresses. I know that embedded
systems don't have a proper binder (a unixism?) but nevertheless, I know about
all that crapola ;-)

kind regards,

Jos
Jun 24 '08 #22
AmberJain
884 Expert 512MB
You're welcome of course. Just to lift a tip of the curtain, the following code shows
(conceptually) what startup code must do; the next function is the target of any
-e flag:

Expand|Select|Wrap|Line Numbers
  1. void startup() {
  2.    int argc= system_dependent_argc();
  3.    char** argv= system_dependent_argv();
  4.  
  5.    exit(main(argc, argv)); /* call main and exit */
  6. }
  7.  
  8. void exit(int exit_code) {
  9.    system_dependent_exit(exit_code);
  10. }
  11.  
You figure out the system_dependent_* functions and you're in business.

kind regards,

Jos

ps. As a side effect the above code snippet perfectly show why main() is supposed
to return an int and not void as Microsoft keeps suggesting.
:=0
HUH.....I accept it's a lot advanced C programming. So what I have decided is that I will stick to PROPER C CODE (as you advised me in REPLY #16) till I get a good nerve of C. And then I will try all this advanced stuff.

BTW, thanks for your help.....

============
AmbrNewlearner
============
Jun 25 '08 #23
lipu
1
I'm using Bloodshed Dev c++ 4.9.9.2 with Mingw port of GCC (GNU Compiler Collection) as its compiler.

Well, I wrote a simple code in C. It is given below--->
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int test(void);
  4.  
  5. int main()
  6. {
  7.     int a=1;
  8.     printf("\nHELLO main %d",a);
  9.     printf("\n\nPRESS ANY KEY TO EXIT");
  10.     getch();
  11.     return(0);
  12. }
  13.  
  14. int test(void)
  15. {
  16.    int val; 
  17.    printf("\nHELLO test()");
  18.    val=main();
  19.    return(0);
  20. }
  21.  
Now, what I want is that test() must be entry point instead of main(). What should I do?

THANKS IN ADVANCE.............

============
AmbrNewlearner
============


do 1 thing ...just delete
val=main() becoz....val cannot call the function main
and rewrite the declaration int val( )
as u have called the main function above
here u also have to call..
see it will work..
Jun 25 '08 #24
JosAH
11,448 Expert 8TB
do 1 thing ...just delete
val=main() becoz....val cannot call the function main
and rewrite the declaration int val( )
as u have called the main function above
here u also have to call..
see it will work..
Please read up on a bit of C programming because what you just wrote doesn't
make any sense at all. main() is a function that returns an int so the assignment
makes perfect sense.

kind regards,

Jos
Jun 25 '08 #25
AmberJain
884 Expert 512MB
Please read up on a bit of C programming because what you just wrote doesn't
make any sense at all. main() is a function that returns an int so the assignment
makes perfect sense.

kind regards,

Jos
Thans JosAh.......Always guiding towards the right way and in time.
When I read reply posted by lipu in REPLY #24 above, I was just amazed by what lipu said. But then you came to rescue and that's where you are perfect.

[JosAh]------>"A man always there to guide me..................."

THANKS.................for your plenty of guidance.

=============
AmbrNewlearner
=============
Jun 27 '08 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Laurent Schall | last post by:
I experience a problem where starting an application compiled with visual C++ 6 SP5 does not execute the function main(int argc, char **argv) and exit immediately with code 1 (0x1). Putting main...
6
by: aneesh | last post by:
Hi all, I would like to know whether we can specify another function instead of main as entry point. Thanks Aneesh
2
by: bboule | last post by:
Hi I have developped a dll that I want to use in another program ! here is my code : using System; using System.Collections.Generic; using System.Text; namespace NameSpace
3
by: Jon Slaughter | last post by:
Is is possible to define a different entry point than Main? i.e. static void MainEntryPoint() {
5
by: Premkumar | last post by:
a.cpp ---------------------------------- #include <iostream> using namespace std; void mymain() { cout<<"my-main"<<endl; } ----------------------------------
2
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when...
1
by: mosfet | last post by:
Hi, I would like to know how usually you wrapp an ugly main fonction into a ;-) C++ method. I would like to write a simple console application that read a XML file so it starts like this : ...
1
by: nicolas.hognon | last post by:
Hello, With non CLR C++ I am used to have a static library (MyMain.lib) which defines the main entry points depending on the plateform (windows, unix, mac os x, ps3, ...). We are planning to...
1
by: Anouar Seljouki | last post by:
1) I built and application that I called contact here is the script for it using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Contacts...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.