【问题标题】:cs50 library wont link to file in cs50 appliancecs50 库不会链接到 cs50 设备中的文件
【发布时间】:2014-05-20 00:14:21
【问题描述】:

我对编程并不陌生,但我也远不是专家。我正在从哈佛在线获取 CS50,我正在尝试使用 cs50 库中应该自动工作在 cs50 设备(Fedora 虚拟机版本 19-2)内的函数。我的问题是当我#include <cs50.h> 并像他在讲座中那样编译时,我收到一条错误消息。

这是一个来自演讲幻灯片的简单程序。

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // ask user for input
    printf("Give me an integer: ");
    int x = GetInt();
    printf("Give me another integer: ");
    int y = GetInt();

    // do the math
    printf("The sum of %i and %i is %i!\n", x, y, x + y);
}

这是我收到的错误消息:

jharvard@appliance (~/Dropbox/pset-1): ls
adder.c  even-odd.c  hello
jharvard@appliance (~/Dropbox/pset-1): clang -o adder adder.c
/tmp/adder-iuV3am.o: In function `main':
adder.c:(.text+0x19): undefined reference to `GetInt'
adder.c:(.text+0x32): undefined reference to `GetInt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
jharvard@appliance (~/Dropbox/pset-1): 

我的猜测是由于某种原因它没有找到库;有没有人知道需要做什么才能让一切都说清楚?

我在其他问题中搜索了有关使用 cs50.c 文件的答案,但我认为这些问题来自试图在自己的机器上编译而不是在一体机中编译的人。

【问题讨论】:

    标签: c cs50


    【解决方案1】:

    你必须告诉编译器通过运行任何一个来链接库

    clang -lcs50 -o adder adder.c
    

    或者干脆

    make adder
    

    因为他们已经为你配置好了。

    【讨论】:

      【解决方案2】:

      您不妨创建一个从现在开始自动链接 cs50 库的 Makefile。 为此,制作一个没有扩展名的新文件并将其命名为 Makefile; 在 Makefile 中简单地写下这一行:LDLIBS += -lcs50。 此外,当您将来需要链接更多库时,也可以这样做,例如LDLIBS += -lm添加数学库等。 希望能帮到你

      【讨论】:

        【解决方案3】:
        #include <stdio.h>
        #include <cs50.h>
        int main (void)
        {
          printf ("Get me integer: " );
          int x = get_int("");
          printf ("Get me another integer: ");
          int y = get_int("");
          printf (" the sum of %i and %i is %i \n ", x,y,x+y);
        }
        

        我们从clang -lcs50 sum.c -o sum开始

        【讨论】:

        • 您好,欢迎来到 stackoverflow,感谢您的回答。您能否简单地解释一下您解决了什么问题以及您是如何解决的,而不是仅仅发布一段代码?这将有助于以后发现这个问题的人更好地理解这个问题以及如何处理它。您还可以通过使用 ``` 标签将其格式化为代码来正确格式化您的代码吗?这将使它更加清晰。
        【解决方案4】:

        这解决了我在 Ubuntu 上的问题

        clang -o adder adder.c -lcs50
        

        由于某种原因 ma​​ke 命令没有正确链接 -lcs50

        【讨论】:

          猜你喜欢
          • 2020-11-03
          • 2019-12-17
          • 1970-01-01
          • 2022-11-24
          • 1970-01-01
          • 1970-01-01
          • 2022-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多