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.
我做了一个还在开发的项目。我没有故意在程序中声明main。因为我正在开发一个用于绘制图形的库和其他我在程序中使用的算法。用C语言开发这样一个库的目的是解决《算法导论》中提到的问题托马斯·H·科尔曼,如果有人想看的话,这里是代码。
#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?
这个错误意味着什么?为什么我要在程序中声明main ?
7 个解决方案
#1
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).
托管C环境需要主函数(独立的环境可以按照自己喜欢的方式启动)。
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.
换句话说,您的库应该有一个大型的测试套件,它是由主函数控制的(很可能是在一个单独的源文件或文件中),这样您就可以测试任何新的工作和累进测试,以确保它没有塞满旧的工作。
#2
14
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.
默认情况下,gcc(和大多数C编译器)编译并链接到一个独立的可执行文件。main()函数是必需的,以便启动代码知道应该从何处开始执行代码。
To compile your library code without linking, use gcc -c graph.c
. In this case, graph.c
does not require a main()
function.
要编译您的库代码而不需要链接,请使用gcc -c graph.c。在这种情况下,图。c不需要main()函数。
#3
5
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.
如果要将代码构建到应用程序中,main函数作为应用程序的入口点,则必须出现。
But, if your code is being built as lib, then main is not needed.
但是,如果您的代码是作为lib构建的,则不需要main。
EDIT: Check this for information on static and shared libraries.
编辑:在静态和共享库中检查这些信息。
#4
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.
可以指定另一个函数作为入口点,但是通过使用main,每个读取代码的人都将知道程序从哪里开始。
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:
通常,在开发库时,您将把main放在单独的程序中,并在测试库时使用它作为起点。是这样的:
gcc -o library.o -c library.c
gcc -o main.o -c main.c
gcc -o testprogram library.o main.o
#5
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.
main()通常是自己启动的。如果忽略main(),则需要任何启动器来执行程序。main是编译器在执行程序时识别的标识符。
#6
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
主程序是执行程序所必需的。当您尝试执行用C编写的程序时,它将转到main函数并从这里执行。如果您正在编写一个库,那么您最好编写一个简单的测试代码来调用库函数,并编译并运行测试程序来测试库
#7
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'.
正式地说,这是来自装入器的需求,而不是编译器的需求。程序员应该知道每个程序在执行之前都应该加载到一个上下文中。这是装入器的责任,但是如果没有定义“入口点”的标准,装入器不知道哪一行代码是入口点。
#1
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).
托管C环境需要主函数(独立的环境可以按照自己喜欢的方式启动)。
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.
换句话说,您的库应该有一个大型的测试套件,它是由主函数控制的(很可能是在一个单独的源文件或文件中),这样您就可以测试任何新的工作和累进测试,以确保它没有塞满旧的工作。
#2
14
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.
默认情况下,gcc(和大多数C编译器)编译并链接到一个独立的可执行文件。main()函数是必需的,以便启动代码知道应该从何处开始执行代码。
To compile your library code without linking, use gcc -c graph.c
. In this case, graph.c
does not require a main()
function.
要编译您的库代码而不需要链接,请使用gcc -c graph.c。在这种情况下,图。c不需要main()函数。
#3
5
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.
如果要将代码构建到应用程序中,main函数作为应用程序的入口点,则必须出现。
But, if your code is being built as lib, then main is not needed.
但是,如果您的代码是作为lib构建的,则不需要main。
EDIT: Check this for information on static and shared libraries.
编辑:在静态和共享库中检查这些信息。
#4
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.
可以指定另一个函数作为入口点,但是通过使用main,每个读取代码的人都将知道程序从哪里开始。
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:
通常,在开发库时,您将把main放在单独的程序中,并在测试库时使用它作为起点。是这样的:
gcc -o library.o -c library.c
gcc -o main.o -c main.c
gcc -o testprogram library.o main.o
#5
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.
main()通常是自己启动的。如果忽略main(),则需要任何启动器来执行程序。main是编译器在执行程序时识别的标识符。
#6
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
主程序是执行程序所必需的。当您尝试执行用C编写的程序时,它将转到main函数并从这里执行。如果您正在编写一个库,那么您最好编写一个简单的测试代码来调用库函数,并编译并运行测试程序来测试库
#7
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'.
正式地说,这是来自装入器的需求,而不是编译器的需求。程序员应该知道每个程序在执行之前都应该加载到一个上下文中。这是装入器的责任,但是如果没有定义“入口点”的标准,装入器不知道哪一行代码是入口点。