未定义引用gcc 4.7中的“main”错误

时间:2015-01-15 12:11:16

标签: c linux gcc

我在C中创建了一个程序,我尝试编译它。当我在Widows中使用我的gcc 4.8.1编译器时,一切正常,我的程序也是如此。

我用以下参数编译:

gcc -std=c99 -O2 -DCONTEST -s -static -lm children.c

但在linux中我收到以下错误:

/usr/lib/gcc/i486-linux-gnu/4.7/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

为什么?我的程序正在运行,我无法理解为什么我在linux中编译错误。

我的代码是:

/*---------------------*/
/* included files      */
/*---------------------*/
#include <stdio.h>
#include <stdlib.h>

/*---------------------*/
/* defined constants   */
/* for restriction     */
/*---------------------*/
#define MIN 1
#define MAX 1000000
#define IOERROR 5   // 'Input/Output Error'

/*---------------------*/
/* function prototypes */
/*---------------------*/
int main();
FILE *read_input(const char *filename_r);
int count_children(FILE *input);
int pass_heights(FILE *input, int *children, int size);
int check_tall(const int *children, int size);
void write_output(const int total,const char *filename_w);


/*---------------------*/
/* start of program    */
/*---------------------*/
int main() {

    const char *filename_r = "xxx.in";
    const char *filename_w = "xxx.out";

    FILE *input = read_input(filename_r);

    int size = count_children(input);

    int *children = malloc(size * sizeof *children);
    if (children==NULL)
    exit(1); //General application error

    pass_heights(input, children, size);

    fclose(input);

    int total = check_tall(children, size);

    free(children);

    write_output(total,filename_w);

    return 0;
}

FILE *read_input(const char *filename_r) {

    FILE *input = fopen(filename_r, "r");

    if(input == NULL)
    exit(IOERROR);

    return input;
}


int count_children(FILE *input) {

    int count = 0;
    fscanf(input, "%d",&count);

    if(count > MAX || count < MIN)
    exit(1); //General application error

    return count;
}


int pass_heights(FILE *input, int *children, int size) {

    for(int i = 0; i < size; i++)
    fscanf(input, "%d",&children[i]);

    return *children;
}


int check_tall(const int *children, int size) {

    int total = 0;
    int tmp_max = 0;
    for(int i = size - 1; i >= 0; i--)
    {
        if(children[i] > tmp_max) {
            tmp_max = children[i];
            total++;
        }
    }
    return total;
}


void write_output(const int total,const char *filename_w) {

    FILE *output = fopen(filename_w, "w");

    if(output == NULL)
    exit(IOERROR); 

    fprintf(output, "%d\n", total);
    fclose(output);
}

2 个答案:

答案 0 :(得分:0)

您使用了-static选项,该选项修改了可执行文件的链接方式。

我无法重现您的确切错误消息,但在我的Linux上,它表示无法在静态模式下与-lc链接,而在我的OSX下,它表示无法找到{{1 }}。对我而言,这意味着系统无法找到静态存根。

如果您删除-lcrt0.o,它应该有效。如果没有,你的问题就很奇怪了。

答案 1 :(得分:0)

您显示的错误表明链接器未在您的代码中找到main()函数。很明显,您已将其包含在源文件中,显然您没有使用该命令行进行编译(或者您正在其他目录中编译,其中您有一个名为children.c的非主()源。 ,如果构建系统找不到源代码,那么构建系统可能会生成touch children.c,然后编译它 - 在这种情况下它不会有main()例程。检查文件是否正确创建以及在哪里,因为我认为您还没有编译该文件。

在进入更复杂的选项之前,请尝试使用简单的选项。尝试类似:

gcc -std=c99 -o children children.c
无论如何,在尝试优化或静态链接之前

。此外,动态链接通常比静态更好,因此您将获得更小的可执行文件(8Kb与800Kb,并且每个可执行文件加载了多个libc副本)。此外,您不需要包含-lm,因为您没有使用任何<math.h>函数(无论如何它都不会受到伤害)。

我已经使用以下命令行编译了你的源代码没有任何问题,但我确实支持静态链接的可执行文件,也许你不会(我上面的命令行可以在任何linux中使用,我想)

$ make CC='gcc' CFLAGS='-std=c99 -O2 -DCONTEST' LDFLAGS='-s -static -lm' children
gcc -std=c99 -O2 -DCONTEST  -s -static -lm  children.c   -o children
children.c: In function ‘pass_heights’:
children.c:81:11: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
children.c: In function ‘count_children’:
children.c:69:11: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]