-4

I've searched some but only found subjects based on C and not C++, here.

So I was wondering whether there is a way to remove the main function using some weird function or something to shorten one's code.

The purpose of this is for code-golf, which is shortening one's code to the absolute shortest. I found the int main{} particularly annoying when code-golfing as it adds an additional 10 characters to my char count. Unlike other languages, e.g. python, C languages require this unfortunately. So is there a way to remove this to lower my char count?

Not only that, I think this can be applicable for things other than code-golf, especially if users do not want to use the entry point of int main{} for some reason.

DialFrost
  • 1,610
  • 1
  • 8
  • 28
  • 5
    main is the entry point of the application. Libraries don't have a main. What are you trying to achieve and why? – JHBonarius Dec 27 '21 at 10:37
  • 5
    What is the goal exactly? Shortening the code to what end? – StoryTeller - Unslander Monica Dec 27 '21 at 10:37
  • "main" is standard name of "entry point". Depending on linker, you may be able to choose any function name as entry point. Possible duplicate of https://stackoverflow.com/questions/7494244/how-to-change-entry-point-of-c-program-with-gcc – Karol T. Dec 27 '21 at 10:39
  • Hosted environments (such as on windows, unix variants, etc) require a `main()` function as the entry point (and the standard forbids an implementation from defining the `main()` function). It is possible on freestanding environments to have an entry point other than `main()` - but an entry point is still needed. – Peter Dec 27 '21 at 10:58

1 Answers1

3

There are two ways.

The first is to build a library instead of an application. You can't run it, but it will build your code to a library that can be linked by other applications. In g++, for instance, you do this with:

g++ -c my_file.cc -o my_file.o
g++ -shared -o libmy_file.so my_file.o

The other way is to modify your linker script so that it uses an entry point other than main. It's not possible to do this in a way that conforms with the C++ standard, but most linkers will give you a way of doing it. See here for how to do it with the GNU linker, for instance.

Tom
  • 7,269
  • 1
  • 42
  • 69
  • Having a library without a `main()` function is all well and good, but it is still necessary to have a program that connects/links to that library and calls functions in it. If that program is written in C++ (on a hosted environment) that program will need a `main()` function. In a freestanding environment, a C++ implementation is permitted to have an entry point other than `main()` - but it is still necessary to have an entry point. – Peter Dec 27 '21 at 10:55
  • 2
    The main problem here is that it's not really clear what the OP is trying to achieve. Judging by the other answer they link, it might be trying to avoid the four bytes of writing the `int ` in `int main`. Unless this is connected to an attempt at one of those "write the best demo in 256 bytes" competitions or something similar, it doesn't seem to be a useful thing to try. – Tom Dec 27 '21 at 11:22