0

こんにちは、私はプログラミングの世界にまったく慣れていないので、ハーバードの CS50 コースをオンラインで受講しようとしています。「Hello World」プログラムを作成しているときに、定義するために「cs50.h」をダウンロードGetStringstringました (少なくとも私はそう思います)。だから、これは私が書いたコードです:

file.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 ファイルへのリンクを次に示します

このエラーが発生する理由と修正方法を知りたいです。助けてください。

4

5 に答える 5

6

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;
}
于 2014-02-26T20:27:37.167 に答える
0

最初の include ステートメントを見てください。< > の代わりに " " を使用しています。

于 2014-02-27T00:16:08.450 に答える
0

#include"cs50.h" ヘッダーには、#include<cs50.h> のように入力する必要があります。また、やってみてください:

#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);
}

これにより、エラーメッセージが取り除かれます。

PS 2 週目に help50 について説明しますが、必要に応じて今すぐ使用できます。私自身、とても便利だと思いました。仕組みは次のとおりです: ターミナル ウィンドウ (./hello と clang を実行するウィンドウ) で、「help50 make hello」(引用符なし) と入力すると、次のように入力されます。 . 次に、エラー メッセージを解読し、より単純な言語で入力します。例えば:

#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 は、エラー メッセージを理解できる言語に解読します。

于 2020-08-11T09:37:45.500 に答える