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.git
Then, 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.c
cd
into the cloned directory (libcs50
) and build:
cd libcs50
make
Then, 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.c
To 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.dylib
Build test.c
as follows:
cc -o test test.c -lcs50
Place libcs50.a
in your CS50 project directory. The contents of the project directory will be:
$ ls
cs50.h test.c libcs50.a
Build test.c
as follows:
cc -o test test.c libcs50.a
Use the following flags in your compile command:
-I
for include path.
Build test.c
as follows:
cc -o test test.c -I./libcs50/build/include ./libcs50/build/lib/libcs50.a
For 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
lib
directory 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.