6

I'm having the following error

myName.c:(.text+0x18): undefined reference to `GetString'

It seems that something is wrong with the cs50 library. What is this file suppose to contain? In my case it doesn't contain any usable code. Example of the cs50.h content

char GetChar(void);

I'm using VM Player with the appliance50-19. Nothing was modified. Clean install.

My code:

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

int main(void)
{
    printf("Name:\n");
    string myName = GetString();
    printf("My name, %s\n", myName);
}

8 Answers 8

12

A library's header file (ends with the .h) doesn't contain function definitions. It contains things like:

  1. function declarations (aka prototypes)
  2. variables
  3. structs and type definitions

See this answer and watch this short for more info.

To use a library, you need to do 2 things:

  1. include header file in which the functions, types, variables, etc you'd like to use are declared

    a. if the header file is already in one of the directories that are searched during preprocessing (e.g., /usr/include/), use:

    #include <filename.h>
    

    b. otherwise (e.g., if the header file is in same directory as the source code file), use:

    #include "filename.h"
    
  2. link the library's binary code to your program using -l[library name]. E.g., to link the cs50 library, you should run:

    clang prog.c -lcs50 -o prog
    

Note that make on the CS50 Appliance automatically links the cs50 library for you. So you could just run:

make prog
6
  • Thanks. I also found the actual file "cs50.c" which contain the functions code itself. Commented Jun 30, 2014 at 16:57
  • I use Ubuntu, installed the library doing the steps in the manual (manual.cs50.net/library) but neither make nor gcc can see the library. I put the *.h file in different places like /usr/include and /usr/local/include just to see what happens but nothing did. Also get the "GetString() is not defined" error.The only thing that worked is clang, thank you!
    – sagitta
    Commented Oct 6, 2015 at 7:54
  • I also use Ubuntu and installed cs50 as per the instructions on the course manual (manual.cs50.net/library) as well, and I have the same problem reported by sagitta. I could however reconfigure Visual Studio Code for compiling using clang with the command line arguments: clang prog.c -lcs50 -o prog (but I had to install clang-8 first). It works fine, but I'd like gcc and make to work as well.
    – Lurch
    Commented Sep 26, 2019 at 6:25
  • Does gcc have options or flags equivalent to clang's -l[library name] ?
    – skytwosea
    Commented Jan 4, 2023 at 2:39
  • 1
    @skytwosea same flag (-l). See man gcc for more info.
    – kzidane
    Commented Jan 4, 2023 at 6:30
8
  1. Download the cs50 from: http://mirror.cs50.net/library50/c/library50-c-5.zip
  2. Extract it. (You will get two files cs50.c and cs50.h)
  3. Now copy both the files to your default library folder. (which includes your stdio.h file)
  4. Now while writing your program use: #include < cs50.c >
  5. You can also copy the files to the folder containing your helloWorld.c file.
  6. You have to use: #include " cs50.c ".

OR =====================================================================>

  1. Open cs50.c and cs50.h files in text editor.
  2. In cs50.h, just below #include < stdlib.h > add #include < stdio.h > and #include < string.h > both on new line.
  3. Now open cs50.c file, copy everything (from: /**Reads a line of text from standard input and returns the equivalent {from line 47 to last}) and paste it in cs50.h just above the #endif and save the files.
  4. Now you can copy the file cs50.h to either your default library folder or to your current working folder.
  5. If you copied the file to default folder then use: #include < cs50.h > and if you copied the files to current working folder then use: #include " cs50.h ".
2
  • SEARCHED FOREVER!! Best answer, thanks worked instantly.
    – devsat
    Commented Apr 22, 2017 at 21:19
  • God bless you!!! Commented Jul 11, 2023 at 18:01
7

Did you compile it using make? If not (and why not?) did you remember to include the -lcs50 clang compile flag that links the library?

1
  • 1
    Exactly! I just solve this problem too. I did used "make" at first place because I provide to make wrong file name such as "name.c" which doesn't work, instead one need to time just "name". Commented Jun 30, 2014 at 3:08
5

I have had the same problem in CS50 2015 VMware appliance. I even include CS50.h library like that:

#include<cs50.h>

But I was keep getting error 1 of Undefined reference to `GetString' or 'GetInt' etc. What I did is just updating the appliance using this command on Gedit:

update50

Update process continued for several minutes. As updates completed, I re-opened my code in Gedit where I had for example string s = GetString(); and recompiled the program using:

make programname

When I ran the program typing ./programname in Gedit, surprisingly I got no Error 1 again. Now every function included in CS50 library is working fine.

This is just my case.

6
  • Yes I had exactly the same problem. Neither the getInt or getstring functions worked even if I just copied the code from the lecture notes. Updating via gedit seemed to solve the issue. Strangely enough this, is was the second update I've done on this CS50 appliance 2014. The first was done earlier on the same day via the Terminal.
    – user3510
    Commented Jan 6, 2015 at 14:18
  • Great to hear you're second who have the same problem. By the way it solved the problem without linking -lcs50 library Commented Jan 6, 2015 at 14:34
  • Same problem. Thanks for this. Commented Jan 9, 2015 at 15:57
  • 2
    Thanks for this solution. One thing worth stressing is to close and reopen Gedit after you have run the update command. I had almost lost hope expecting the command alone to have fixed things, but when that failed I tried closing and opening the program and it worked fine.
    – Jonny
    Commented Jan 12, 2015 at 13:03
  • Yes @Jonny you're right a close and re-opening of Gedit is also required after update50. Commented Jan 13, 2015 at 5:52
0

I also had this issue. Performing an update of the CS50 appliance (by typing update50 in the terminal window) resolved it. In the instructions regarding installation of WMwareFusion and the CS50 appliance (https://manual.cs50.net/appliance/2014/#how_to_install_appliance), there was indeed mention of the possibility of updating the CS50 appliance by typing update50 in the terminal window however I didn't realize it was a necessity.

0

Getting the same error. I think the header file is not include or something. Try using scanf function to store values in a variable, which is predefined in stdio.h header file. SYNTAX--

scanf("%d",&a);

This will store an integer input from user to the address of integer variable a. Or just think of it as equivalent to a=GetInt();

0

Getting the same error. I did the update50 and stil no success. I rebooted my VM and that did the trick!? I thought that with Linux you did not need to reboot for updates, in this case it worked.

0
0

after installing cs50 lib in my ubuntu, every time when I wanna use functions from that lib I have to do this "clang prog.c -o prog -lcs50" I can't just do "make prog" it prints this error:

/usr/bin/ld: /tmp/cc8noHLN.o: in function main': loop.c:(.text+0x19): undefined reference to get_int' collect2: error: ld returned 1 exit status make: *** [: loop] Error 1

3
  • Setting the environment variable LDLIBS should allow direct invocation of make invoking the linker with delineated supplementary libraries. Amend the following line export LDLIBS="-lcrypt -lcs50 -lm" or an equivalent onto the shell initialisation script such as ~/.bash.rc to circumvent manual setup. Commented Jul 14, 2020 at 5:05
  • Thanks for advice, but nothing happened still I have to link -lcs50 to run program. I typed Commented Jul 15, 2020 at 11:15
  • export LDLIBS="-lcryp -lcs50 -lm" nothing happened Commented Jul 15, 2020 at 11:16

You must log in to answer this question.

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