1

I am enrolled in CS50. I am working on the week 1 project. #include at the top of the program is supposed to have certain programs already defined, such as get_string, get_int, etc... Before I get into the project I ran a simple test to make sure everything was working.

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

int main(void)
    {
        printf("What is your name?\n");
        string x = get_string("x");
        printf("Hello, %x!");
    }  

My code is above and below is the error I keep receiving.

undefined reference to `get_string'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

Aren't those errors already referenced when I put in #include ?

  • When I try to compile this program in the sandbox (and the ide) it complains about the invalid syntax in the `printf` command. More correct syntax would be: `printf("Hello, %s!",x);` – DinoCoderSaurus Sep 16 '19 at 03:07

1 Answers1

3

That's a linker error, not a compiler error. The problem is that you included cs50.h, which has the declaration of get_string, but you didn't include the library that has its definition. To fix it, pass -lcs50 on the command line.

  • Below is what I typed in the command line: $ -lcs50 -lcs50: command not found $ lcs50 lcs50: command not found – user3553965 Sep 16 '19 at 02:05
  • @user3553965 I meant to add that as an argument to clang, not to run it on its own. – Joseph Sible-Reinstate Monica Sep 16 '19 at 02:06
  • Sorry. This is my first week of computer science class. So now I ran $ clang -lcs50 and I got the error below /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' clang-7: error: linker command failed with exit code 1 (use -v to see invocation) – user3553965 Sep 16 '19 at 02:26
  • @user3553965 To the clang command you're running to compile your code, not in a new clang command by itself. – Joseph Sible-Reinstate Monica Sep 16 '19 at 02:49
  • https://cs50.readthedocs.io/libraries/cs50/c/ this should give you step-by-step instructions as well as a troubleshooting guide. – Apps 247 Oct 17 '20 at 07:32