How to install custom c library?

I just wanted to add a c library to Ubuntu which was created by Harvard University for cs50 course. They provided instructions for how to install the library which is listed below.


Debian, Ubuntu

First become root, as with:

sudo su -

Then install the CS50 Library as follows:

apt-get install gcc
wget http://mirror.cs50.net/library/c/cs50-library-c-3.1.zip
unzip cs50-library-c-3.1.zip
rm -f cs50-library-c-3.1.zip
cd cs50-library-c-3.1
gcc -c -ggdb -std=c99 cs50.c -o cs50.o
ar rcs libcs50.a cs50.o
chmod 0644 cs50.h libcs50.a
mkdir -p /usr/local/include
chmod 0755 /usr/local/include
mv -f cs50.h /usr/local/include
mkdir -p /usr/local/lib
chmod 0755 /usr/local/lib
mv -f libcs50.a /usr/local/lib
cd ..
rm -rf cs50-library-c-3.1

I did exactly as directed. But the compiler reported “Undefined reference to a function”--the function was Get String. So, I searched for a solution and found one. It said to use the -l switch.

Now when I compile I use something like:

gcc –o hello.c hello –lcs50

(I don’t remember the exact command.)

However, I cannot use the make command, which is easier to use.

I understand that there is some problem with linking the library. What is a good solution to this problem?


I am taking CS50 as well, and I have the same problem. After following Cs50's instructions, I get this error when compiling code using gc

/tmp/ccvUiSKS.o: In function `main':
Goodmario.c:(.text+0x21): undefined reference to `GetInt'
collect2: error: ld returned 1 exit status

I have followed the instructions to the exact. I believe that you should just continue using -lcs50, it's not any harder. eg. The command I use to compile a program

gcc Goodmario.c -o Goodmario -std=c99 -lcs50

Not very hard : D


you should add the following command to make GetInt work:

$ gcc hello.c -I/usr/local/include -L/usr/local/lib -lcs50 -o hello

rename "hello.c# and "hello" with your input file and your desired output name. The option -I/usr/local/include tells gcc to also look in /usr/local/include for header files. The option -L/usr/local/lib tells gcc (in the linking phase) to also look for libraries in /usr/local/lib, and the option -lcs50 tells gcc which libraries you need.

source: ubuntuforum.org