Ç - cs50.h GetString的错误 [英] C - cs50.h GetString error

查看:169
本文介绍了Ç - cs50.h GetString的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是完全新的编程世界中,我试图把哈佛的CS50课程在线。
同时使我的Hello World程序,我下载了cs50.h定义的GetString 字符串(至少我认为)。因此,这是code我写的:

Hello I am completely new to the world of programming an I am attempting to take Harvard's CS50 course online. While making my "Hello World" program, I downloaded 'cs50.h' to define GetString and string (at least I think). So this is the code I wrote:

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

然而,每当我试图使文件,出现这种情况:

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

Here is a link to the cs50.h file if it can help: http://dkui3cmikz357.cloudfront.net/library50/c/cs50-library-c-3.0/cs50.h

我想知道为什么我得到这个错误,我怎么能解决这个问题。请帮助。

I would like to know why I get this error and how I can fix it. Please help.

推荐答案

看来你忘了下载和链接到从项目的 http://dkui3cmikz357.cloudfront.net/library50/c/cs50-library-c-3.0/cs50.c

It seems that you forgot to download and link to project cs50.c file from http://dkui3cmikz357.cloudfront.net/library50/c/cs50-library-c-3.0/cs50.c

*的.h通常只包含声明。 * .C(为C)和*的.cpp(对于C ++)中包含的实现。

*.h usually contain only declarations. *.c (for C) and *.cpp (for C++) contains implementations.

有从这个类GetSting功能实现:

There is GetSting function implementation from this class:

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

这篇关于Ç - cs50.h GetString的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆