Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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
C:链接器命令失败,退出代码为1_C_Linker - Fatal编程技术网

C:链接器命令失败,退出代码为1

C:链接器命令失败,退出代码为1,c,linker,C,Linker,我在编译代码时遇到了一个问题。我没有收到任何错误消息。然而,我得到了以下信息: Undefined symbols for architecture x86_64: "_lookup", referenced from: _main in sixteen2-85c27c.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (us

我在编译代码时遇到了一个问题。我没有收到任何错误消息。然而,我得到了以下信息:

Undefined symbols for architecture x86_64:
  "_lookup", referenced from:
      _main in sixteen2-85c27c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我以前从未遇到过这样的事情,而且我在网上能找到的大多数信息都是Objective-C,而不是普通的C。如果有任何帮助,我将不胜感激

这是我的密码:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {
    struct entry {
        char word[15];
        char definition[100];
    };

    const struct entry dictionary[100] = 
    {{"aardvark", "a burrowing African mammal"},
    {"abyss", "a bottomless pit"},
    {"acumen", "mentally sharp; keen"},
    {"addle", "to become confused"},
    {"aerie", "a high nest"},
    {"affix", "to append; attach"},
    {"agar", "a jelly made from seaweed"},
    {"ahoy", "a nautical call of greeting"},
    {"aigrette", "an ornamental cluster of feathers"},
    {"ajar", "partially opened"}};

    int entries = 10;
    int entryNumber;
    int lookup (const struct entry dictionary[], const char search[], const int entries);

    if (argc != 2)
    {
        fprintf (stderr, "No word typed on the command line.\n");
        return EXIT_FAILURE;
    }

    entryNumber = lookup (dictionary, argv[1], entries);

    if (entryNumber != -1)
        printf("%s\n", dictionary[entryNumber].definition);
    else
        printf("Sorry, %s is not in my dictionary.\n", argv[1]);

    return EXIT_SUCCESS;
}
#包括
#包括
int main(int argc,char*argv[]){
结构条目{
字符字[15];
字符定义[100];
};
常量结构条目字典[100]=
{{“土豚”,“非洲穴居哺乳动物”},
{“深渊”,“无底洞”},
{“敏锐”,“精神敏锐;敏锐”},
{“混乱”,“变得混乱”},
{“aerie”,“高巢”},
{“附加”、“附加;附加”},
{“琼脂”,“由海藻制成的果冻”},
{“你好”,“航海问候语”},
{“艾格莱特”,“一簇装饰性的羽毛”},
{“半开”、“半开”};
整数项=10;
int入口编号;
整数查找(常量结构条目字典[],常量字符搜索[],常量整数条目);
如果(argc!=2)
{
fprintf(stderr,“命令行上没有键入单词。\n”);
返回退出失败;
}
entryNumber=查找(字典,argv[1],条目);
如果(入口编号!=-1)
printf(“%s\n”,字典[entryNumber]。定义);
其他的
printf(“对不起,%s不在我的字典中。\n”,argv[1]);
返回退出成功;
}

您有以下代码行:

int lookup (const struct entry dictionary[], const char search[], const int entries);
内部的主要功能。这有两个主要问题

  • 函数声明不能出现在另一个函数中。我引用的行需要出现在主功能之外,如下所示:

    int lookup(.....)
    //code here
    int main(...)
    {
        //more code here
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int lookup (const struct entry dictionary[], const char search[], const int entries)
    {
        //lots of code belongs in here, but that's for you to figure out :)
    }
    
    int main (int argc, char *argv[]) {
        struct entry {
            char word[15];
            char definition[100];
        };
    
        const struct entry dictionary[100] = 
        {{"aardvark", "a burrowing African mammal"},
        {"abyss", "a bottomless pit"},
        {"acumen", "mentally sharp; keen"},
        {"addle", "to become confused"},
        {"aerie", "a high nest"},
        {"affix", "to append; attach"},
        {"agar", "a jelly made from seaweed"},
        {"ahoy", "a nautical call of greeting"},
        {"aigrette", "an ornamental cluster of feathers"},
        {"ajar", "partially opened"}};
    
        int entries = 10;
        int entryNumber;
        //**REMOVE THIS LINE**  Move it up top....
        //int lookup (const struct entry dictionary[], const char search[], const int entries);
    
        if (argc != 2)
        {
            fprintf (stderr, "No word typed on the command line.\n");
            return EXIT_FAILURE;
        }
    
        entryNumber = lookup (dictionary, argv[1], entries);
    
        if (entryNumber != -1)
            printf("%s\n", dictionary[entryNumber].definition);
        else
            printf("Sorry, %s is not in my dictionary.\n", argv[1]);
    
        return EXIT_SUCCESS;
    }
    
  • 即使您声明函数“lookup”,您也从未定义它。这可能是导致链接器错误的原因。编译器编译所有函数,然后链接器将这些函数链接在一起(可以这么说),并发现,尽管函数“lookup”被声明并随后被调用,但它实际上并不存在于任何地方!(据我所知,stdlib.h或stdio.h中不存在函数“lookup”)。这会阻止链接器执行其工作

  • 总而言之,您的代码应按如下方式重新组织:

    int lookup(.....)
    //code here
    int main(...)
    {
        //more code here
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int lookup (const struct entry dictionary[], const char search[], const int entries)
    {
        //lots of code belongs in here, but that's for you to figure out :)
    }
    
    int main (int argc, char *argv[]) {
        struct entry {
            char word[15];
            char definition[100];
        };
    
        const struct entry dictionary[100] = 
        {{"aardvark", "a burrowing African mammal"},
        {"abyss", "a bottomless pit"},
        {"acumen", "mentally sharp; keen"},
        {"addle", "to become confused"},
        {"aerie", "a high nest"},
        {"affix", "to append; attach"},
        {"agar", "a jelly made from seaweed"},
        {"ahoy", "a nautical call of greeting"},
        {"aigrette", "an ornamental cluster of feathers"},
        {"ajar", "partially opened"}};
    
        int entries = 10;
        int entryNumber;
        //**REMOVE THIS LINE**  Move it up top....
        //int lookup (const struct entry dictionary[], const char search[], const int entries);
    
        if (argc != 2)
        {
            fprintf (stderr, "No word typed on the command line.\n");
            return EXIT_FAILURE;
        }
    
        entryNumber = lookup (dictionary, argv[1], entries);
    
        if (entryNumber != -1)
            printf("%s\n", dictionary[entryNumber].definition);
        else
            printf("Sorry, %s is not in my dictionary.\n", argv[1]);
    
        return EXIT_SUCCESS;
    }
    
    它需要“跳转”到机器代码的不同部分。“int main()”可能从地址100开始,“int addnums()”可能从地址50开始。当计算机到达此行时,需要在地址50处执行机器代码

    编译器不关心函数的地址。它只是编译代码,然后让另一个程序将正确的地址放入机器代码中。如果链接器说

    symbol(s) not found
    
    这意味着链接器不知道某个函数的地址。这通常是因为所讨论的函数从未声明或定义过。如果链接器要从该代码生成一个可执行文件,那么您的计算机将到达该行

    int total = addnums(mynums, 5);
    
    然后说“嗯???我该去哪里?”“int addnums()”函数如果没有跳转地址,您的cpu将完全丢失

    你的程序会神秘地崩溃

    链接器通过删除错误并停止编译过程,为您省去了这一心痛。在我业余编程嵌入式系统的日子里,我有过几次这样的问题,调试起来简直是一场噩梦


    希望这有帮助。告诉我我的解释是否错误。

    查找功能在哪里定义?不,我不知道链接器是什么。谢谢。我会编辑我的评论来解释什么是链接器。我希望这是有意义的:)