CS50: "undefined reference to `GetString' " and other errors

I was going through the CS50 lectures of Fall 2011. CS50 recommends using the CS50 Appliance which is a VirtualBox installation of Fedora with libraries and software needed for the CS50 course. However, if you are already running a flavour of Linux (Ubuntu 12.04 in the case of this article) and want to do the examples natively, read on.

On Linux, the CS50 manual gives instructions on how to install the CS50 Library for C  for Linux (Fedora / Ubuntu).

After installing the CS50 Library for C, I tried running some samples. While making pointers.c in the Week 5 lecture, I came across this error

$make pointers
cc     pointers.c   -o pointers
pointers.c: In function ‘main’:
pointers.c:27:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
pointers.c:27:5: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [pointers] Error 1


If instead of using make, you use gcc pointers.c you will get a similar error. To correct this, I ran

$gcc pointers.c -std=c99 

and got the following error

/tmp/cckKXdXr.o: In function `main':
pointers.c:(.text+0xb): undefined reference to `GetString'
collect2: ld returned 1 exit status


Doing a Google search for "getstring cs50 undefined" gives a link to 2010's Week1 lecture transcript where David Malan says
It can't find GetString right?...Someone else wrote GetString namely CS50. I have included it using the cs50.h but that only gives me access in the source code to actually using it by name. The bits, the zeros and ones that compose the library are actually in that file called cs50.c but those bits live or--even that's a white lie. They technically live in a file, as we'll eventually see called cs50.o....So it turns out that there's a two-step process. When you want to use a library like CS50's, you can't just run GCC on your program. You also have to tell the compiler at the very end of your command--you know what, let me use the library with the so called switch/l so that's an L not a 1. So a dash lowercase L and then what library?...so you'll have to explicitly say dash L CS50. Why is there no space? Stupid convention...So now let's hit Enter.
So, I ran the following

$gcc pointers.c -std=c99 -lcs50

And the program compiled.

However, there are some inefficiencies still, for example the above code will give you the output as a.out. If you want to specify a name for the binary, you can use the -o switch, as in below.

$gcc pointers.c -std=c99 -lcs50 -opointers

A look at the video lecture however will tell you that the make on the CS50 Appliance also uses additional switches. A screen capture is shown below
A screen capture from CS50 Fall 2011 Lecture series Week 5, Monday
Screencap from the Final lecture of CS50 in Fall 2011 showing gcc switches that make uses


So  future proofing for CS50 2011 Fall Lecture series, let us add the other switches as well.

$gcc -ggdb  -std=c99 -Wall -Werror pointers.c -lcrypt -lcs50 -lm -o pointers

(In the above text replace pointers with the name of the current .c file in the two places.)

(CS50 is a introductory course at Harvard aimed at beginners and is available for free under a CC BY-NC-SA 3.0 license)

Sources & Further Reading:
1. CS50 OpenCourseWare
2. CS50 Fall 2011 Lectures
3. CS50 Manual : Library
4. GCC

Comments

Post a Comment

Popular posts from this blog

CPU Temperature Guide for Intel Core 2 Duo : Range of Normal CPU Temperatures

Appengine: Custom Error Handlers using webapp2 - Python 2.7