【问题标题】:C - cs50.h GetString errorC - cs50.h GetString 错误
【发布时间】:2014-02-26 20:24:21
【问题描述】:

您好,我对编程世界完全陌生,我正在尝试在线学习哈佛的 CS50 课程。 在制作“Hello World”程序时,我下载了“cs50.h”来定义GetStringstring(至少我认为)。所以这是我写的代码:

文件.c:

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

int main(int argc, string argv[])
{
    string name;
    printf("Enter your name: ");
    name = GetString();
    printf("Hello, %s\n", name);
}

但是,每当我尝试make file 时,就会发生这种情况:

cc     file.c   -o file
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
  _main in file-JvqYUC.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [file] Error 1

如果有帮助,这里是 cs50.h 文件的链接:http://dkui3cmikz357.cloudfront.net/library50/c/cs50-library-c-3.0/cs50.h

我想知道为什么会出现此错误以及如何解决它。请帮忙。

【问题讨论】:

  • 这是 C,而不是 C++(string 是 typedef,而不是 std::string)。请仅标记适当的语言(C 和 C++ 不同)。删除 C++ 标签。
  • cc file.c cs50.c -o filecc file.c cs50.o -o file

标签: c string cs50


【解决方案1】:

您似乎忘记从http://dkui3cmikz357.cloudfront.net/library50/c/cs50-library-c-3.0/cs50.c下载并链接到项目cs50.c文件

*.h 通常只包含声明。 *.c(用于 C)和 *.cpp(用于 C++)包含实现。

这个类有GetSting函数实现:

string GetString(void)
{
    // growable buffer for chars
    string buffer = NULL;

    // capacity of buffer
    unsigned int capacity = 0;

    // number of chars actually in buffer
    unsigned int n = 0;

    // character read or EOF
    int c;

    // iteratively get chars from standard input
    while ((c = fgetc(stdin)) != '\n' && c != EOF)
    {
        // grow buffer if necessary
        if (n + 1 > capacity)
        {
            // determine new capacity: start at 32 then double
            if (capacity == 0)
                capacity = 32;
            else if (capacity <= (UINT_MAX / 2))
                capacity *= 2;
            else
            {
                free(buffer);
                return NULL;
            }

            // extend buffer's capacity
            string temp = realloc(buffer, capacity * sizeof(char));
            if (temp == NULL)
            {
                free(buffer);
                return NULL;
            }
            buffer = temp;
        }

        // append current character to buffer
        buffer[n++] = c;
    }

    // return NULL if user provided no input
    if (n == 0 && c == EOF)
        return NULL;

    // minimize buffer
    string minimal = malloc((n + 1) * sizeof(char));
    strncpy(minimal, buffer, n);
    free(buffer);

    // terminate string
    minimal[n] = '\0';

    // return string
    return minimal;
}

【讨论】:

  • 所以为了让我的程序正常工作,我需要#includecs50.c ?
  • 不包括!您应该将其添加到您的项目中!如果您正在使用一些 IDE(MS Visual Studio、Xcode、Eclipse ... 有数百个),总有可能通过菜单“将文件添加到项目”。如果你正在使用 make 文件,你应该在 make 文件中注意到这个文件。
  • 更准确地说,您必须将对 GetString() 的调用链接到其在 cs50.c 的编译版本中的实现
【解决方案2】:

查看您的第一个包含语句。您使用的是“”而不是 。

【讨论】:

  • 这也是他应该使用的; &lt;&gt; 几乎可以肯定在这里使用是错误的。
【解决方案3】:

在 CS50 课程的视频中,讲师使用插入符号 () 而不是引号 (" ")。

【讨论】:

    【解决方案4】:

    上CS50的同学,又不想每次都粘贴.c代码,也可以在编译的时候链接CS50代码。

    将cs50.h和cs50.c和file.c放在同一目录下,然后在命令行中输入:

    clang file.c -lcs50 -o <file name>
    

    “-l”将 cs50.c 和 cs50.h 文件链接到您的 c 文件(编译为目标文件之后),“-o”指定将编译后的输出放在哪里。

    更多信息here

    【讨论】:

      【解决方案5】:

      在您的#include"cs50.h" 标题中,您应该这样输入:#include。另外,尝试做:

      #include<cs50.h>
      #include<stdio.h>
      
      
      int main(void)
      {
      
      string name = get_string("Enter your name: ");
      printf("%s\n", name);
      
      }
      

      而不是这个:

      #include "cs50.h"
      #include <stdio.h>
      
      int main(int argc, string argv[])
      {
          string name;
          printf("Enter your name: ");
          name = GetString();
          printf("Hello, %s\n", name);
      }
      

      这应该消除错误消息。

      附言 在第 2 周,他们会告诉您有关 help50 的信息,但如果您愿意,现在可以使用它。 我自己发现它非常有用。它是这样工作的:在你的终端窗口(你执行 ./hello 和 clang 的那个)你应该输入:“help50 make hello”(不带引号)然后它会输入:asking for help... in yellow .然后它将破译错误消息并用更简单的语言输入。例如:

      #include <stdio.h>
      #include <cs50.h>
      
      int main(void)
      {
       
      
      
      
      
      string name = get_string("Enter your name: ");
      printf("%s\n", name)
      
          
      } 
      

      我确实打了个招呼,然后出现了:

      clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    hello.c  -lcrypt -lcs50 -lm -o hello
      hello.c:13:21: error: expected ';' after expression
      printf("%s\n", name)
                          ^
                          ;
      1 error generated.
      <builtin>: recipe for target 'hello' failed
      make: *** [hello] Error 1
      

      但是当我使用 help50 make hello 时,会出现:

      clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    hello.c  -lcrypt -lcs50 -lm -o hello
      hello.c:13:21: error: expected ';' after expression
      printf("%s\n", name)
                          ^
                          ;
      1 error generated.
      <builtin>: recipe for target 'hello' failed
      make: *** [hello] Error 1
      
      Asking for help...
      
      hello.c:13:21: error: expected ';' after expression
      printf("%s\n", name)
                          ^
                          ;
      
      Are you missing a semicolon at the end of line 13 of hello.c?
      

      如您所见,现在我知道我的问题并且可以解决它。 Help50 将错误消息解读为您可以理解的语言。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 2011-10-22
        • 2021-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多