1

How can I change the entry point "main" of my program ?

#include<stdio.h>       
int main(int argc, char *argv[])  
{ 
 printf("%s\n","Hello world!");    
 return 0;
} 
Stack Over
  • 425
  • 2
  • 8
  • 18
  • What do you mean by change? What do you want to change? If you want another entry point, you cant. Whatever your program will be, there will always be a 'main-function' – Simon Apr 05 '15 at 21:28
  • 1
    possible duplicate of [Avoiding the main (entry point) in a C program](http://stackoverflow.com/questions/3379190/avoiding-the-main-entry-point-in-a-c-program) – David C. Rankin Apr 05 '15 at 21:28
  • Just to understand how the compiler generates the output file. Certainly, my program uses the default linker file. Can I change it? – Stack Over Apr 05 '15 at 21:33
  • @StackOver, you should click the `√` check mark next to BLUEPIXY answer below to give him credit and points for an excellent esoteric answer. I've tested his answer, and it works perfectly. Just out of curiosity, why do you want to do this? – clearlight Apr 05 '15 at 21:42
  • @Allaboutthatbase2, I'm wondering if i want change the entry point like the software for embedded system. – Stack Over Apr 05 '15 at 21:45
  • @StackOver, I see some people have up-voted BLUEPIXY's answer but you are the only one who can accept it by clicking the √. He gets points for you doing it, and he did help you so--- that's how this site works win-win to keep everyone extra motivated to provide good service :-) – clearlight Apr 05 '15 at 21:50
  • @StackOver - What embedded system? – clearlight Apr 05 '15 at 21:50

2 Answers2

4
gcc -o entry_test -Wl,-eother entry_test.c

#include<stdio.h>       

int other(void){//can't use argc, argv
    printf("Bye-Bye world!\n");
    return 0;
}

int main(int argc, char *argv[]){
    printf("%s\n","Hello world!");
    return 0;
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • never heard of that. Does that actually work? I am going to try that! – clearlight Apr 05 '15 at 21:33
  • I just tried to compile that with gcc on Mac OS X Yosemite and got this error: `gcc -Wl,-eother x.c ld: unknown option: -eother clang: error: linker command failed with exit code 1 (use -v to see invocation)` – clearlight Apr 05 '15 at 21:34
  • Next I will try on Linux and Solaris – clearlight Apr 05 '15 at 21:35
  • It is the gcc compiler on Mac OS X. I don't know if it's a variant or clang under the hood, but that gets back to the question, is this a portable answer and what OS and compiler was the asker using? – clearlight Apr 05 '15 at 21:37
  • I use gcc version 4.6.3 on windows 7. – BLUEPIXY Apr 05 '15 at 21:39
  • Wow! Cool! I did it on Solaris 11 and it works! Had to to add -o to the gcc line. Learn something new every day. Will delete my answer. – clearlight Apr 05 '15 at 21:39
  • I got this error: /usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crt1.o: dans la fonction « _start »: (.text+0x18): référence indéfinie vers « main » collect2: erreur: ld a retourné 1 code d'état d'exécution – Stack Over Apr 05 '15 at 21:47
  • sorry, I have no idea. – BLUEPIXY Apr 05 '15 at 21:51
  • What embedded system are you using? What is the target platform? What was the gcc command you used and what do you usually use? – clearlight Apr 05 '15 at 21:51
  • It worked for me on Solaris, I will also try on Linux now. – clearlight Apr 05 '15 at 21:52
  • @BLUEPIXY I compiled and tested this on Linux RHEL and it compiled, ran, printed Bye-Bye world and then segv'd. – clearlight Apr 05 '15 at 21:54
1

If you're using gcc, I found a thread that said you can use the -e command-line parameter to specify a different entry point; as BLUEPIXY stated see also :

Avoiding the main (entry point) in a C program

-see the following link for more details about "-e" option :

http://gcc.gnu.org/ml/gcc/2001-06/msg01959.html

Another way is to change the starting function is in the linker "start up" file.. linker may include some pre-main startup code in a file like start.o and it is this piece of code which runs to set up the C environment then call main (as in all embedded tool-chains). There's nothing to stop you replacing that with something that calls another function instead.

here is a terrific explanation for startup files:

What is the bootloader and startup code in embedded systems?

I voted this question up as it really may be useful for some embedded c developers.

Community
  • 1
  • 1