6

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.

#include<stdio.h>
#include<stdlib.h>
#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) {
        count++;
        if (count == 1) {
            printf("This is going to be root node \n");
            scanf("%d", &n);
            root = cnode(n);
            count--;
            priv = root;
        }       //ending if
        else {
            printf
                ("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
        printf
            ("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
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

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

reality displays
  • 731
  • 3
  • 9
  • 18
  • Possible duplicate of [Good plotting library for C?](https://stackoverflow.com/questions/1275484/good-plotting-library-for-c) – Matt Mas Jul 19 '19 at 15:47

8 Answers8

16

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.

caf
  • 233,326
  • 40
  • 323
  • 462
6

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.

Jay
  • 24,173
  • 25
  • 93
  • 141
  • 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 07: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
4

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

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.

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
  • 1
    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 07 '13 at 09:48
1

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
Robert
  • 6,855
  • 4
  • 35
  • 43
0

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

small_ticket
  • 1,910
  • 5
  • 22
  • 30
0

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'.

William X
  • 6,751
  • 6
  • 31
  • 50
  • 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 X Nov 24 '10 at 08:48
0

to access the elements in stdio.h we need to have a function, a main function is a predescribed function which accepts/calls all the other functions

  • This is, at best, a very vague and inaccurate description of what the `main` function is and how it works. It really adds little or nothing to exiting answers. – Adrian Mole Mar 18 '23 at 04:04