0

I have a simple C program I have to make (Which can't use a main function).

As such:

test.c

#include <stdio.h>

int getUserID(int id);

int getUserID(int id) {
    return 0;
}

Where my current function does nothing as of now: However, I'm trying to compile this through a makefile, as such:

all:
    gcc test.c 

Which didn't work because I didn't have a main so then I added the -c command within it.

all:
    gcc -c test.c

Which now compiles with make, but gives me an object file (but without a working executable), the executable I get when I try to run it: i.e ./test tells me permission denied

Any help would be greatly appreciated.

Tes3d
  • 13
  • 1
  • 2
  • 2
    @CollinD He already tried that. – Baum mit Augen Jan 12 '16 at 23:37
  • 2
    @BaummitAugen That doesn't change the fact that this question has already been asked. There's also a good explanation there of why he can't do what he's asking, and some other suggestions that he has not in fact tried. – CollinD Jan 12 '16 at 23:38
  • 2
    Why can't it have a main function? – user253751 Jan 12 '16 at 23:49
  • *"That doesn't change the fact that this question has already been asked."* This is not correct. The dupe wants to compile source files without a `main`, which is possible and nothing out of the ordinary, while OP wants a program without a `main`, which is at least very dodgy. – Baum mit Augen Jan 12 '16 at 23:49
  • Why do you want a C program without a `main` function? That's sort of like asking for an ice cream cone without the cone. – Steve Summit Jan 12 '16 at 23:59
  • One example of a C program w/o "main" might be the kernel. – John Hascall Jan 13 '16 at 00:06
  • @JohnHascall: Even then, it needs to have a well-defined start address. For example, old DOS COM files started at 0x100, so programs built for it needed a linker script that defined some kind of start symbol at that address. That's a much more complex topic than the questioner seems to be asking. – greyfade Jan 13 '16 at 00:08

3 Answers3

5

You cannot have a C++ or C program without a main (without evil, non-standard hacks at least). You can build a library without a main, but not a standalone executable.

For standalone applications, the main function is crucial because it defines where to start execution.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
3

You can create an image where "main" is not the entry point.

Here is one such way (using gcc on a RHEL6 system):

$ cat sample.c
#include <stdio.h>
#include <unistd.h>

int notmain ( void ) {
        printf("hello, world\n");
        exit(0);
}
$ cc -ffreestanding -c sample.c
$ ld -enotmain -o sample sample.o -I/lib64/ld-linux-x86-64.so.2 -lc
$ ./sample
hello, world

And, BTW, this is not an "evil, non-standard hack" -- the C Standard defines two execution environments: (1) "hosted", which is the one most people are familiar with, and (2) "freestanding", which is typically some sort of embedded system (a toaster or whatnot).

Community
  • 1
  • 1
John Hascall
  • 9,176
  • 6
  • 48
  • 72
1

You can separately compile this c file into an object file, but it will need to be linked with another object file containing the main function to become an executable program.

If you are not allowed to have a main function in this file, there must be another file you are expected to link with it.