0

I am recieving the follwoing error. I am not able to figure out why. I have tried several ways to fix, but end up with the same error

~/pset1/hello/ $ ls
hello*  hello.c
~/pset1/hello/ $ clang hello.c
/tmp/hello-b4febf.o: In function `main':
hello.c:(.text+0x19): undefined reference to `get_string'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
~/pset1/hello/ $ 

I also tried MAKE and get the follwoing error

~/pset1/hello/ $ 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
~/pset1/hello/ $ 
1
  • ~/pset1/hello/ $ ls -al total 24 drwx------ 2 ubuntu ubuntu 4096 Oct 6 20:07 ./ drwx------ 3 ubuntu ubuntu 4096 Oct 6 18:50 ../ -rwx------ 1 ubuntu ubuntu 9072 Oct 6 19:49 hello* -rw-rw-r-- 1 ubuntu ubuntu 144 Oct 6 20:07 hello.c
    – SteveF
    Oct 6, 2020 at 20:08

2 Answers 2

0

Did you remember to include the following at the start of your code?

#include <cs50.h>

Whenever you get an "undefined reference to ..." error, it almost always means that an #include statement is missing, and the linker can't link to the function in that library. Sometimes, it means that there's a spelling error in the function name itself or in the library name in the include statement.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Yes I did include:#include <stdio.h> #include <cs50.h> int main(void) { string name = get_string("What is your name?\n"); printf("hello, %s\n", name); }
    – SteveF
    Oct 6, 2020 at 20:58
  • Then there's another error, something subtle and most likely, a typo. Please edit the question and paste the entire code from the IDE so we can look at and test it.
    – Cliff B
    Oct 6, 2020 at 21:06
  • #include <stdio.h> #include <cs50.h> int main(void) { string name = get_string("What is your name?\n"); printf("hello, %s\n", name); }
    – SteveF
    Oct 7, 2020 at 18:35
  • I getting output: ~/pset1/ $ cd hello ~/pset1/hello/ $ ls hello* hello.c ~/pset1/hello/ $ clang hello.c /tmp/hello-57015e.o: In function main': hello.c:(.text+0x19): undefined reference to get_string' clang-7: error: linker command failed with exit code 1 (use -v to see invocation) ~/pset1/hello/ $ ./hello What is your name? steve hello, steve ~/pset1/hello/ $
    – SteveF
    Oct 7, 2020 at 18:36
0

When using external libraries, you have to link them in your clang argument. Using the CS50 IDE, the make command will do that for you.

You said

I also tried MAKE and get the follwoing error

~/pset1/hello/ $ 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
~/pset1/hello/ $

There is no error there. When you run make, it runs clang with all of those arguments (including -lcs50 which links the cs50 library). Your program has been compiled and you can run

~/pset1/hello/ $  ./hello

to run it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .