免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 27308 | 回复: 4
打印 上一主题 下一主题

[Linux] (.text+0x18): undefined reference to `main' [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-16 00:02 |只看该作者 |倒序浏览
本帖最后由 只为你飘的雪 于 2012-11-16 00:09 编辑

gcc -o main.c path_malloc_2_3.c open_max_2_4.c
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../crt1.o: In function `_start':
(.text+0x1: undefined reference to `main'
collect2: ld 返回 1
然后main.c就没有了。这是为什么啊?求高人赐教!
open_max.c:#include "main.h"

#ifdef OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
* if OPEN_MAX is indeterminate, we are not guaranteed that this is adequate*/
#define OPEN_MAX_GUESS 256

long open_max(void)
{
        if (openmax == 0)
        {
                errno = 0;
                if ((openmax = sysconf(_SC_OPEN_MAX)) < 0)
                        if (errno == 0)
                                openmax = OPEN_MAX_GUESS;
                        else
                                perror("sysconf error";
        }

        return (openmax);
}

path_alloc.c:#include "main.h"

#ifdef PATH_MAX
static int pathmax = PATH_MAX;
#else
static int pathmax = 0;
#endif

#define SUSV3 200112L
static long posix_version = 0;
/*if path_max indeterminate , no guarantee this is adequate */
#define PATH_MAX_GUESS 1024

char *path_alloc(int *sizep) /*also return allocated size, if nonnull */
{
        char *ptr;
        int size;

        if (posix_version == 0)
                posix_version = sysconf(_SC_VERSION);

        if (pathmax == 0)   //first time through
        {
                errno = 0;
                if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0)
                {
                        if (errno == 0)
                                pathmax = PATH_MAX_GUESS;   //it is indterminats
                        else
                                perror("pathmax error for _pc_path_max";
                }
                else
                        pathmax++;
        }
                if (posix_version < SUSV3)
                        size = pathmax +1;
                else size = pathmax;

                if ((ptr = malloc(size)) == NULL)
                        perror("malloc error";
                if (sizep != NULL)
                        *sizep = size;
                return (ptr);
}

main.h:#ifndef _MAIN_H_
#define _MAIN_H_

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>

char *path_alloc(int *);
long open_max(void);

#endif

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-11-16 11:28 |只看该作者
没有main函数.

论坛徽章:
0
3 [报告]
发表于 2012-11-16 11:50 |只看该作者
本来有main函数的,用gcc编译之后,就没有了。回复 2# linux_c_py_php


   

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
4 [报告]
发表于 2012-11-16 11:54 |只看该作者
gcc -o main.c path_malloc_2_3.c open_max_2_4.c

你这命令...

-o是指定output name, 你把编译+链接path_malloc_2_3.c open_max_2_4.c 的结果输出到main.c文件了, 并且因为path_malloc_2_3.c open_max_2_4.c 里没有main函数,链接还失败报错了。。。

论坛徽章:
0
5 [报告]
发表于 2012-11-16 12:11 |只看该作者
我擦,我想了好久,原来是命令有问题啊!谢谢了。我以为是一个.C文件必须配对一个.h 文件呢,⊙﹏⊙b汗回复 4# linux_c_py_php


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP