开发者

Why is main necessary in a program

开发者 https://www.devze.com 2023-01-26 10:09 出处:网络
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 algorithmwhich I would use in my programs.The

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?


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.


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.


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.


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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消