Compiling in C using cs50 library

Discussion in 'Mac Programming' started by jeduni, Jul 30, 2011.

  1. macrumors newbie

    Jul 30, 2011

    I am following the CS50 online programming course on my own and I wanted to use the CS50 library which is being used in the course. I believe I finally managed to install it properly (I was lacking gc.h obviously at first). However, when I try to compile a file from one of the lectures, Terminal returns the following:

    mb:~ jdn$ gcc h1.c -lcs50
    Undefined symbols:
      "_GC_malloc", referenced from:
          _GetString in libcs50.a(cs50.o)
      "_GC_free", referenced from:
          _GetString in libcs50.a(cs50.o)
          _GetString in libcs50.a(cs50.o)
          _GetString in libcs50.a(cs50.o)
      "_GC_realloc", referenced from:
          _GetString in libcs50.a(cs50.o)
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    I am new to the world of programming, but I hope you will be able to point me towards a solution. Does it have something to do with the compiler not knowing what the function "GetString" does?
    Anyway, the file I am trying to compile is this one (called h1.c):

    #include <cs50.h>
    #include <stdio.h>
    printf("State your name: ");
    string name = GetString();
    printf("O hai, %s!\n", name);
    Hope you can help,
  2. macrumors regular

    Jan 27, 2011
    Where did you get the libcs50.a file? Did you compile that yourself?

    If I do this order of steps for Lecture 5 source code, I get the expected results:
    $ gcc -c -ggdb -std=c99 cs50.c -o cs50.o
    $ ar rcs libcs50.a cs50.o
    $ gcc [COLOR="Red"]-I "."[/COLOR] h1.c [COLOR="red"]-L "."[/COLOR] -lcs50
    $ ./a.out
    State your name: bla
    O hai, bla!
    Note my changes in red are because I am keeping the libcs50.a and cs50.h file in the same directory as the h1.c and this tells the compiler how to find those files. If you do the above does it work?
  3. jiminaus, Jul 30, 2011
    Last edited: Jul 30, 2011

    macrumors 65816


    Dec 16, 2010
    GetString relies on libgc so you need to link it to your executable as well. Assuming libgc is as equally accessible as the cs50 library, then your compile command needs to be:
    gcc h1.c -lcs50 lgc
    @ehoui Did you use version 1 of the library that didn't require libgc?
  4. thread starter macrumors newbie

    Jul 30, 2011
    Thanks for the answers!

    @jiminaus I added -lgc to the compile command, however, Terminal returned

    mb:~ jdn$ gcc h1.c -lcs50 -lgc
    ld: library not found for -lgc
    collect2: ld returned 1 exit status
    libgc resided in /usr/lib/gc but I guess it wasn't able to find it in the gc-subfolder (I would have thought it searched subfolders too?)? Anyway I tried moving the contents of the above-mentioned folder to /usr/local/lib (the same folder as the cs50.a library) and now it seems to work!
    It's just that when showing how to compile using the cs50 library in one of the lectures the teacher only added -lcs50 (not -lgc), but I guess his system must be configured in another way.

    @ehoui I am using version 2.0 which I compiled following the guidelines on

    Again, thanks a lot for the replies :)

  5. macrumors newbie

    Sep 23, 2009

    I'm no expert but I got it working on my Macbook pro (snow leopard) by using these commands. Basically they are the same from the site that you listed but you don't place them in the "local" folder.

    gcc -c -ggdb -std=c99 cs50.c -o cs50.o
    ar rcs libcs50.a cs50.o
    rm -f cs50.o
    chmod 0644 cs50.h libcs50.a
    sudo mv cs50.h /usr/include
    sudo cp libcs50.a /usr/lib
  6. macrumors regular

    Jan 27, 2011
    I downloaded my version from and it is less than 2.0. I think the sole difference is that the 1.x libraries leaked memory. I doubt this matters for running through the exercises. You won't be using cs50.c in a production environment.

    Also, I gave up after spending 30 minutes trying to get libgc to compile on Lion. Moving to Lion has set me back a bit as I try to figure out how to get what used to compile to actually compile and work. Lion/Xcode 4 have apparently changed things...arg.
  7. macrumors 6502a

    Jan 23, 2010
    San Diego, CA USA
    With gcc you can use the -L flag to specify a directory to search for libraries and the -I flag to tell is where to search for include files.

    If you are in the same directory as the header files and libraries you want to include, then

    gcc -L. -I. h1.c -lcs50 -lgc
    "." means the current directory. Or you can give another path. This way you don't have to go copying files into the system areas of your disk.
  8. captainvera, Aug 19, 2012
    Last edited: Aug 19, 2012

    macrumors newbie

    Apr 1, 2012
    sry to reopen this again..

    sry to reopen this again, but i've tried everything and i still can't get them to work... i'm on lion and i always get this error:

    Undefined symbols for architecture x86_64:
    "_GetString", referenced from:
    _main in main.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
  9. macrumors regular

    Jun 5, 2010
    dude why are you asking here, go ask the harvard prof/TA
  10. macrumors 6502a


    Jan 4, 2002
    Austin, TX
    The error tells you exactly what happened:

    Undefined symbols for architecture x86_64:
    The linker (the thing that makes an executable from compiled code) could not find a reference to some function in some compiled 64 bit code its working with. What function is it?

    "_GetString", referenced from:
    Ah, some function named GetString()! The code linker can't find a 64 bit version of this. But where is the function GetString() used?

    _main in main.o
    Ah hah! In the function main() in the compiled object named main.o - most likely compiled code from the source file main.c.

    And then a little summary of the problem:

    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    Now you know what happened. There is a function called GetString() that your code is trying to use in the function main() in the source file named main.c. The linker can't find it so it gives you that error and stops since there no way it can make an executable if it doesn't know where GetString() is.
  11. macrumors 604

    Aug 9, 2009
    Post the exact command-line that produced the error. "I've tried everything" is impossible for us to debug. We need to see exactly what you did. We can't read your mind, see your screen, or look at your files. If you don't give details, we can't guess them.

    If you're not using the command-line, you need to tell us exactly what you're using.
  12. macrumors 6502

    Madd the Sane

    Nov 8, 2010
    If I'd have to guess, you either forgot to link to the cs50 library, or the cs50 library isn't 64-bit. But if cs50 wasn't 64-bit, I think the linker would have mentioned that.

Share This Page