user3357419 user3357419 - 1 year ago 358
C Question

C - cs50.h GetString error

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

(at least I think). So this is the code I wrote:


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

However, whenever I try to
make file
, this happens:

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

Here is a link to the cs50.h file if it can help:

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

Avt Avt
Answer Source

It seems that you forgot to download and link to project cs50.c file from

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

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;
                return NULL;

            // extend buffer's capacity
            string temp = realloc(buffer, capacity * sizeof(char));
            if (temp == NULL)
                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);

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

    // return string
    return minimal;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download