Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make without a main function

Status
Not open for further replies.

Hoda81

Programmer
Apr 25, 2005
1
DE
Hello everyone :)
This is my first post here.. I am completely stuck with this 'make' problem:

I have to make a binary from 2 .c files where the main() function is not called main() but flash()! (and yes it is necessary that it has another name)
Any idea how to tell make that instead of looking for main(), it should look for flash()?

First .c file contains the 'main' flash() function, and the other .c file is just function definitions and so..

any ideas?
 
It's the C Standard rule for main name.
If you want to start your work with flash(), you must have
Code:
int main()
{
...
  flash();
...
}
Compiler's generated code and RTL call main() (and only main in common console application case) after initialization run-time environment.
The only binary usable form for codes is a library. Make it then link (with main;)...
 
Or...
Code:
#include <stdio.h>

#define flash main

flash()
{
    printf("FLASH!\n");

    return(0);
}
Hope this helps. [bigsmile]
 
ArkM said:
It's the C Standard rule for main name.
Not necessarily.
5.1.2.1 Freestanding environment
1 In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.
Hoda81: if that's the case, perhaps your implementation's documentation has some answers.
 
[tt]Make[/tt] doesn't have anything to do with what symbol is marked as the "start" symbol in your executable. Your linker takes care of that.


On Linux using GCC and GNU Binutils, the platform I use, the linker marks the executable to start at a [tt]start[/tt] symbol which does general setup required for all C programs. This then calls [tt]main[/tt].

You can use a linker script to make it start somewhere other than [tt]start[/tt], but that means you don't get the setup required for your C program.

As far as I can tell, making the [tt]start[/tt] code call a symbol other than [tt]main[/tt] would require rewriting a piece of the compiler.


Why "must" the starting function be called [tt]flash[/tt]?
 
The reason it must be called FLASH is Probably because that is the home work assignment and requesting help for home work is strictly forbidden on Tek-Tips.

Also beyond this discussion could be that they were really supposed to make a LIBRARY of routines whose start point is FLASH so your teacher can then run their program from his mainline by pointing it at their library.

Maybe they weren't supposed to make an executable but a shared library.



Anyway.... Linux LD command has an option to change the MAIN entry point name. Other linkers probably have a similiar feature.

Code:
  -e entry
  --entry=entry
      Use  entry  as  the explicit symbol for beginning 
      execution of your program, rather than the default
      entry point.  If there is no  symbol  named  entry,
      the linker will try to parse entry as a number,
      and use that as the entry address (the number will
      be  interpreted in  base  10;  you may use a leading
      0x for base 16, or a leading 0 for base 8).

 
I think the problem with a linker entry point definition is that true (from system point of view) entry point of C program as usually is not main (or _main or what else with main). In most of C/C++ implementations it's a name of RTL bootstrap module (do a stack and a heap initial settings, prepare argc/argv stuff etc).

This starting RTL module has a (permanent) link to a user-defined main-derivative name.

Of course, some implementations have another user source entry names (and corresponding RTL bootstrap modules). So it's a hardly implementation-dependent (uninteresting;) problem...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top