Take the 2-minute tour ×

I made a program which is still in development. I did not declared main in my program deliberately.As I am developing a library for making graph and other algorithm which I would use in my programs.The purpose to develop such a library in C is to work upon the problems given in book Introduction to Algorithms Thomas H Coreman Here is the code if some one wants to see.

#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node *graph;

graph cnode(int data);      //cnode is to create a node for graph
void cgraph(void);
struct node {
    int data, color;
    struct node *LEFT, *RIGHT, *TOP, *DOWN;

graph root = NULL;

void cgraph(void)
    int n, choice, dir, count;

    choice = 1;
    count = 1;
    graph priv, temp;

    printf("Printf we are making a graph the first is root node\n");
    while (choice == 1) {
        if (count == 1) {
            printf("This is going to be root node \n");
            scanf("%d", &n);
            root = cnode(n);
            priv = root;
        }       //ending if
        else {
                ("Enter direction you want to go LEFT 1 RIGHT 2 TOP 3 DOWN 4\n");
            scanf("%d", &dir);
            printf("Enter the data  for graph node\n");
            scanf("%d", &n);
            temp = cnode(n);
            if (dir == 1) {
                priv->LEFT = temp;
            if (dir == 2) {
                priv->RIGHT = temp;
            if (dir == 3) {
                priv->TOP = temp;
            if (dir == 4) {
                priv->DOWN = temp;
            priv = temp;
        }       //ending else
            ("Enter 1 to continue adding nodes to graph any thing else would take you out\n");
        scanf("%d", &choice);
    }           //ending while
}               //ending main

graph cnode(int data)
    graph temp = (graph) malloc(sizeof(graph));

    temp->data = data;
    temp->LEFT = NULL;
    temp->RIGHT = NULL;
    temp->TOP = NULL;
    temp->DOWN = NULL;
    temp->color = -1;
    return temp;

When I compiled the above program I got following error.

cc graph.c
collect2: ld returned 1 exit status

What does this error signify and why should I declare main in my program?

share|improve this question

7 Answers 7

up vote 2 down vote accepted

Why? Because the standard says so (mostly).

The main function is required for hosted C environments (freestanding environments are allowed to start up any way they like).

If you're developing a library, you don't need a main for the library itself but you won't be able to turn it into an executable without one (other than by using non-portable trickery). And, at a bare minimum, you should have one for the test suite anyway.

In other words, your library should have a large test suite which is controlled from a main function (most likely in a separate source file or files) so that you can test any new work and regression-test to ensure it hasn't stuffed up the old work.

share|improve this answer

By default, gcc (and most C compilers) compile and link to a standalone executable. The main() function is required so that the startup code knows where execution of your code should start.

To compile your library code without linking, use gcc -c graph.c. In this case, graph.c does not require a main() function.

share|improve this answer

main is mandatory to be present if you are building your code into an application as main functions serves as an entry point for the application.

But, if your code is being built as lib, then main is not needed.

EDIT: Check this for information on static and shared libraries.

share|improve this answer
Ya I am trying to make a library for my programs so I will keep compiling to check the code so what should I do to get rid of these sort of errors? –  reality displays Nov 24 '10 at 7:46
@Bond, I have edited my answer and provided a link which explains how to build static and shared libraries. Anyways to check the code you can only compile it and avoid linking it. use gcc -c graph.c as suggested by caf for only compilation. –  Jay Nov 24 '10 at 12:48

A program needs an entry point to clarify where your program starts. Without that, it's impossible for your tools to know which function should be called first.

It is possible to specify another function as entry point, but by using main, everyone that reads your code will know where your program starts.

Usually, when developing libraries, you will put main in a separate program and use that as a starting point while testing out your library. Something like this:

gcc -o library.o -c library.c
gcc -o main.o -c main.c
gcc -o testprogram library.o main.o
share|improve this answer

normally main() is start up itself. if you ignore the main() it need any starter to execute program. basically main is an identifier which identified by compiler when a program is executed.

share|improve this answer
Welcome to SO! Before posting answers please check out the other answers, too. This Q already has an accepted answer and I believe your answer has the same content. If you provide a new answer to an old question it should add value. –  cfi Nov 7 '13 at 9:48

main is neccessary to execute your program. When you try to execute a program written in C, it goes to main function and executes from here. If you are writing a library as you said, you'd better write a simple test code to call your library functions and compile and run that test program to test your library

share|improve this answer

Formally speaking, it's the requirement from a loader, rather than compilers. Programmer would know that each program SHOULD be loaded into a context before its execution. this is loaders' responsibility, but loaders have no knowledge which line of code is the entry point if there is no standard to define the 'entry point'.

share|improve this answer
Further, if you're building object file or lib only, that is NOT linking into an executable. The 'Main' entry point will not be necessary for such case. –  William Choi Nov 24 '10 at 8:48

Your Answer


