Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GCC编译问题-致命错误:找不到';ld';_C_Gcc - Fatal编程技术网

GCC编译问题-致命错误:找不到';ld';

GCC编译问题-致命错误:找不到';ld';,c,gcc,C,Gcc,当我运行以下代码时,我得到 collect2:致命错误:找不到“ld” 编译已终止。作为输出。我的GCC版本是GCC(ubuntu7.3.0-27ubuntu1~18.04)7.3.0。似乎无法找到ld模块。我不知道如何继续前进 #define _GNU_SOURCE #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<

当我运行以下代码时,我得到
collect2:致命错误:找不到“ld”
编译已终止。
作为输出。我的GCC版本是
GCC(ubuntu7.3.0-27ubuntu1~18.04)7.3.0
。似乎无法找到
ld
模块。我不知道如何继续前进

#define _GNU_SOURCE
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(){
    char *stderr = "/home/vs/Desktop/test/output.txt";
    char *args[] = {"/usr/bin/gcc",
                    "-Wall",
                    "-O2",
                    "-std=gnu11",
                    "-fomit-frame-pointer",
                    "/home/vs/Desktop/test/Solution.c",
                    "-o",
                    "/home/vs/Desktop/test/Solution",
                    "-lm",
                    NULL};
    char *env[]  = {"LANG=en_US.UTF-8", "LANGUAGE=en_US:en", "LC_ALL=en_US.UTF-8", NULL};

    int fd;
    if(0 > (fd = open(stderr, O_WRONLY|O_TRUNC))) perror("open");
    if(0 > dup2(fd, 2)) perror("dup");
    if(fd != 2) close(fd);

    int x = execve("/usr/bin/gcc", args, env);
    printf("%d\n", x);
    return 0;
}
定义GNU源
#包括
#包括
#包括
#包括
#包括
int main(){
char*stderr=“/home/vs/Desktop/test/output.txt”;
char*args[]={”/usr/bin/gcc“,
“-墙”,
“-O2”,
“-std=gnu11”,
“-fomit帧指针”,
“/home/vs/Desktop/test/Solution.c”,
“-o”,
“/home/vs/Desktop/test/Solution”,
“-lm”,
空};
char*env[]={“LANG=en_US.UTF-8”,“LANGUAGE=en_US:en”,“LC_ALL=en_US.UTF-8”,NULL};
int-fd;
如果(0>(fd=open(stderr,O|WRONLY | O|u TRUNC)))或(“open”);
如果(0>dup2(fd,2))或(“dup”);
如果(fd!=2)关闭(fd);
int x=execve(“/usr/bin/gcc”,args,env);
printf(“%d\n”,x);
返回0;
}

由于同一个编译命令在通过shell发出时有效,但在以编程方式发出时失败(如图所示),因此您提供给
execve()
的环境很可能存在问题。请特别注意,提供给该函数的环境数组表示命令的整个环境,而不仅仅是额外的条目

在这方面特别相关的是,所提供的环境不包括
PATH
变量。因此,exec的进程需要使用完全限定的路径来依次启动它想要启动的任何命令,例如
ld
。如果它不这样做,那么您报告的错误就会发生。将
路径添加到指定的环境应该可以解决此问题。您可以从程序自己的环境中复制,或者更容易地插入默认路径。比如说,

    // ...

    char *env[]  = {
        "PATH=/usr/local/bin:/usr/bin:/bin",  // <--- this
        "LANG=en_US.UTF-8",
        "LANGUAGE=en_US:en",
        "LC_ALL=en_US.UTF-8",
        NULL
    };

    // ...
/。。。
char*env[]={

“路径=/usr/local/bin:/usr/bin:/bin”,//
ld
是UNIX链接器的传统名称,GCC系统确实提供了一个同名的链接器。因此,您报告的诊断表明您的GCC安装不完整或损坏。我建议完全删除它,然后通过系统的包管理工具重新安装。@JohnBollinger它不起作用。但我尝试过手动编译,但效果很好。正是这个程序不起作用。为什么,@John?这会要求链接名为
libd
的库。尽管这会给链接器带来额外的工作(可能正在寻找并未能找到这样的库),我看不出有任何理由认为这会对是否找到链接器产生影响。@JohnBollinger也许我的评论不恰当。我没有完全阅读这个问题。collect2实用程序(由gcc使用)报告的缺少的ld意味着在/bin或/usr/bin/locations中找不到链接器(ld)。