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

A library's header file (the one that ends with the .h extension) doesn't contain function definitions. Instead, it contains the library's resources that are available for you to use. These resources include

  1. Function declarations (aka prototypes).
  2. Variables.
  3. Structs.
  4. Type definitions.

For more information, you may have a look at this answer and watch the short on libraries - week 1!

To use a library, you have to do 2 things

  1. To include the library's header file using

    a. In case the header file is in the default include directory (i.e., /usr/include/) we use

    #include <filename.h>
    

    b. In case the header is not in the default include directory (e.g., in case it's in the same directory as the source code file or in another directory), we use

    #include "filename.h"
    
  2. To link the library's binary code with the binary code of your program using the -l[library name]. For example, to link the cs50 library, you may to compile your program like that

    clang prog.c -lcs50 -o prog
    

Notice that make on the appliance links the cs50 library by default. So you may just run

make prog
| improve this answer | |
  • Thanks. I also found the actual file "cs50.c" which contain the functions code itself. – Kirill Kovalevskiy Jun 30 '14 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 Oct 6 '15 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 Sep 26 '19 at 6:25
8

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?

| improve this answer | |
  • 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". – Kirill Kovalevskiy Jun 30 '14 at 3:08
6

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.

| improve this answer | |
  • 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 Jan 6 '15 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 – muhammad tayyab Jan 6 '15 at 14:34
  • Same problem. Thanks for this. – user3123975 Jan 9 '15 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 Jan 12 '15 at 13:03
  • Yes @Jonny you're right a close and re-opening of Gedit is also required after update50. – muhammad tayyab Jan 13 '15 at 5:52
4
  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 ".

If you need help you can ask me at faizan4427@gmail.com and if this worked please let me know by voting it.

All the Best!!!

| improve this answer | |
  • SEARCHED FOREVER!! Best answer, thanks worked instantly. – devsat Apr 22 '17 at 21:19
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.

| improve this answer | |
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();

| improve this answer | |
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.

| improve this answer | |
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

| improve this answer | |
  • 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. – Ashen Gunaratne Jul 14 at 5:05
  • Thanks for advice, but nothing happened still I have to link -lcs50 to run program. I typed – Javohir Abdullayev Jul 15 at 11:15
  • export LDLIBS="-lcryp -lcs50 -lm" nothing happened – Javohir Abdullayev Jul 15 at 11:16

You must log in to answer this question.

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