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?
Importance points of the main() function An operating system always calls the main() function when a programmers or users execute their programming code. It is responsible for starting and ends of the program. It is a universally accepted keyword in programming language and cannot change its meaning and name.
c does not require a main() function. Show activity on this post. 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.
A sequence of instructions that is used when a program is started and is central to its structure, being able to invoke subprograms or subroutines as required and being itself executed by the operating system.
No, the ISO C standard states that a main function is only required for a hosted environment (such as one with an underlying OS). For a freestanding environment like an embedded system (or an operating system itself), it's implementation defined.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With