This guide assumes that:
- You can use command line.
Command line crash course - Learn web development | MDN - Git is installed.
Git - Installing Git
Before starting, make sure that you are in the project directory containing test.c (or some C code to compile).
First, clone the libcs50 git repository:
git clone https://github.com/cs50/libcs50.gitThen, your project directory will be like (ommitted unnecessary ones):
$ tree
.
├── README.md
├── build.sh
├── libcs50
│ ├── LICENSE
│ ├── Makefile
│ ├── README.md
│ ├── src
│ │ ├── cs50.c
│ │ └── cs50.h
│ └── tests
└── test.ccd into the cloned directory (libcs50) and build:
cd libcs50
makeThen, your build directory will be like:
$ cd build
$ tree
.
├── include
│ └── cs50.h
├── lib
│ ├── libcs50-11.0.2.dylib
│ ├── libcs50.a
│ └── libcs50.dylib -> libcs50-11.0.2.dylib
└── src
└── cs50.cTo build a C program, you need cs50.h and either libcs50-11.0.2.dylib (for Dynamic Linking) or libcs50.a (for Static Linking).
Place libcs50-11.0.2.dylib in your CS50 project directory. The contents of the project directory will look like:
$ ls
cs50.h test.c libcs50-11.0.2.dylibBuild test.c as follows:
cc -o test test.c -lcs50Place libcs50.a in your CS50 project directory. The contents of the project directory will be:
$ ls
cs50.h test.c libcs50.aBuild test.c as follows:
cc -o test test.c libcs50.aUse the following flags in your compile command:
-Ifor include path.
Build test.c as follows:
cc -o test test.c -I./libcs50/build/include ./libcs50/build/lib/libcs50.aFor an enhanced version, see Build Script.
After building, you can run the test binary file with:
./test- A minimal setup will lower the barrier to C programming.
- You don't want to mess up the system's
libdirectory with a library that will never be reused. - In macOS, there are some problems with
dyld(Dynamic Link Editor). For example, if you have disabled SIP (System Integrity Protection), you might encounter issues when following the methods described in official guides.