7

I am currently taking CS50 online via edX. I would like to install the CS50 Library. I followed these instructions for installing on a Mac and everything seemed to work out fine.

However, anytime I try to run a program that references cs50.h, I receive an error message. Do these files need to be saved somewhere specific? I am new to this, so I need a very detailed explanation.

2
  • Please provide more details - what error message do you see? How exactly are you referencing the file?
    – Air
    Sep 25, 2014 at 16:43
  • you can put cs50.h in the same location as stdio.h (you may need to use search to locate it) or you can put it in same location as your .c file (but you need to use "include cs50.h" instead of <include cs50.h> in that last case. Feb 22, 2015 at 20:15

3 Answers 3

9

Follow the steps below, and see if it helps...

  1. [Optional, you may choose to not install Xcode as long as you have any text editor] On a Mac, make sure you have installed Xcode first of all. It's available on the Mac App Store for free, but is a hefty download. So, once downloaded, move further.

  2. Now, download the CS50 library from here.

Now are the important steps, so, pay attention from here on

  1. Open up a new terminal window, from the Launchpad(or from Applications). Just type cd ie. Type "cd" and leave a space

  2. Open the downloads folder, there you'll see the "library50-c-5" folder. Drag that folder from there to the terminal window. Once dragged successfully, you should be able to see cd /Users/yourname/Downloads/library50-c-5 where the path may vary according to your Username and downloads folder... Now, press enter.

  3. Now execs cute the following commands(as instructed by CS50 manual) step by step:

    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 mkdir -p /usr/local/include

    sudo mv -f cs50.h /usr/local/include

    sudo mkdir -p /usr/local/lib

    sudo mv -f libcs50.a /usr/local/lib

    cd ..

    rm -rf library50-c-5

    Whoops, a lot of work, right, I know... :) just know that you'll have to enter your password(your Mac user password) once, or maybe twice in the process. Also, in your question, you mention facing some errors, what are those errors ? Any that you face during execution of these commands ? If you still face those, consider detailing the errors.

Plus, that's it.

To test out the CS50 library, open a text editor(or even Xcode), and create a new file called hello.c and type a simple program that uses the CS50 library. Include it to your program using #include <cs50.h>. Now, close the file and either try compiling it on Xcode(if you downloaded it at the first place) or open up a terminal window, change your directory to the one the file hello.c is (using cd) and then enter the following command:

gcc -g hello.c -o hello -lcs50 -lm

And now, if you face no errors, congrats, you have installed the CS50 library on your Macintosh. Try out your program using ./hello to see if all works correct. It must do so if you followed all the steps above correctly.

Best of luck installing the CS50 library on your Mac, and happy coding...

10
  • did anyone try this? sucess? @Nib, seems quite thorough! i just finished watching lectures, walkthroughs, shorts (CS50x wk 0, wk 1) — can actually follow thread (linking to libary, etc.). i'd like to understand gcc vs. clang thing, tho. i've read few posts & understand modern xcode deploys clang. as well, 2014 lectures refer to clang in detail. so why compile in gcc? one thread, elsewhere, expressed trouble attempting to compile CS50 libary in xcode w/ gcc. not sure why current instructions for libray50 employ gcc. i'd like to graduate to xcode, eventually.. but will wait for clarity.
    – mazal
    Dec 28, 2014 at 21:36
  • UPDATE: i'm able to install CS50 library on mac os 10.10 without installing Xcode. in the terminal i'm able to compile via gcc or clang, each with #include <cs50.h> header; plus 'make' but w/out #include <cs50.h> header. @Nib, or anyone, is there insight how to tweak make command to accomodate the CS50 library? the entire gcc (or clang) string is: gcc -ggdb3 -O0 -std=c99 -Wall -Werror hello-03.c -lcs50 -lm -o hello-03.
    – mazal
    Jan 1, 2015 at 20:49
  • Can you please tell how to make 'make' use CS50 by default without needing to link it
    – Anjan
    Feb 24, 2015 at 10:31
  • make hello clang -ggdb3 -O0 -std=c11 -Wall -Werror -o hello hello.c -lcs50 -lm hello.c:2:10: error: 'cs50.h' file not found with <angled> include; use "quotes" instead #include <cs50.h> ^~~~~~~~ "cs50.h" 1 error generated. make: *** [hello] Error 1: Here's the error @Nib, please help. OS X El Capitan. Jul 11, 2016 at 21:05
  • FYI - I've just installed cs50.h, using Nibs instructions, on Mac OSX Sierra and it worked like a charm.
    – Android1
    Mar 15, 2017 at 8:31
0

Download the latest release from https://github.com/cs50/libcs50/releases

Extract libcs50-. // cd libcs50-* // sudo make install

==>> Be sure to run: <==

clang -o code code.c -lcs50

More info (installing) here: https://github.com/cs50/libcs50

More info (issues) here: https://github.com/cs50/libcs50/issues/123

-3

While running source code do you link cs50 library ??
you should run those source code on terminal and link Cs50 library at command line in terminal.

1
  • Hmmm, please consider posting that as a comment than an answer. That isn't a precise answer to the users question, and rather asks for more details :)
    – Nib
    Sep 25, 2014 at 17:13

You must log in to answer this question.

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