【问题标题】:Compilation Error in i/o redirection in C program in linuxlinux中C程序中i/o重定向的编译错误
【发布时间】:2015-04-22 19:30:43
【问题描述】:

我正在尝试进行简单的 I/O 重定向(ls to sort)(ls|sort>f1),然后我的下一步是将 sort 的输出定向到 C 中的文件,但是在编译 gcc 时给出以下错误..请帮助我:)

代码

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int a,b,c,d,pfd[2],kk,i,j;
FILE *fp;

i=fork();
if(i==0)
{
    pipe(pfd);
    j=fork();
    if(j==0)
    {
        close(1);
        dup(pfd[1]);
        close(pfd[0]);
        close(pfd[1]);
        excel("/bin/ls","/ls",0);
    }
    else
    {
        close(0);
        dup(pfd[0]);
        close(pfd[0]);
        close(pfd[1]);
        /*close(1);
        kk=open(f.txt,O_WRONLY);
        dup(kk);
        */
        excel("/usr/bin/sort","/sort",0);
    }
}
else
    wait();
/*char k[100],pp[100],ll[]="/bin/";
printf("Enter the cmd to execut");
scanf("%s",k);
strcpy(pp,k);
strcat(ll,k);
printf("%s",ll);
system(ll);*/
    return 0;
}

错误:

/tmp/cc6wIvoJ.o:

In function **main**:
j.c:(.text+0x81): undefined reference to **excel**

j.c:(.text+0xcf): undefined reference to **excel**

collect2: error: ld returned 1 exit status

【问题讨论】:

  • 错误:/tmp/cc6wIvoJ.o: In function main': j.c:(.text+0x81): undefined reference toexcel' j.c:(.text+0xcf): undefined reference to 'excel' collect2:错误:ld 返回 1 个退出状态

标签: c linux system-calls


【解决方案1】:

这里有一些虚假的答案,我无法评论,因为我没有“50 声望”积分。因此,一个全新的答复改为。

正如人们正确指出的那样,您显然有错字,您需要 execl 而不是 excel

但是,人们声称 execl 是一个系统调用。它不是。这是您的 libc 中的一个便利功能。在一天结束时执行的系统调用是 execve,您可以通过例如确认使用 strace。

您的代码有几个问题,您在编译时是否启用了警告? -Wall -Wextra 到 gcc 肯定会有所帮助。

示例问题: - wait() 接受一个参数,如果你有正确的头文件,你会收到警告 - 缺少错误检查(fork、pipe、wait)

一般来说,你的编码风格会给你带来麻烦。

始终包含相关标题(可在联机帮助页中找到)并检查返回值。

【讨论】:

    【解决方案2】:

    没有称为excel 的系统调用。因此,您会收到未定义的错误。尝试使用exec() 系列调用之一,如execl。 -

    http://linux.die.net/man/3/execl

    【讨论】:

      【解决方案3】:

      它的 execl 不是 excel 。 execl 是您要查找的调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-22
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 2016-07-15
        • 2010-10-22
        • 2013-11-19
        相关资源
        最近更新 更多