/cs50-minimal-setup

CS50 Minimal Setup for macOS and Linux

Primary LanguageShell

CS50 Minimal Setup for macOS and Linux

Prerequisites

This guide assumes that:

Getting started

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).

Option 1. Dynamic 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

Option 2-1. Static linking

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

Option 2-2. Static linking - Using flags to link libraries

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.

Running the program

After building, you can run the test binary file with:

./test

Why?

  1. A minimal setup will lower the barrier to C programming.
  2. You don't want to mess up the system's lib directory with a library that will never be reused.
  3. 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.

References

See also