Skip to main content

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? 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


Post a Comment

Popular posts from this blog

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

Is your CPU Temperature too High? How high is high enough? Is 60 degree Celsius high enough or is 70 degree Celsius the threshold? Hipatic covers your temperature problems right here. Below we discuss Intel Core 2 Duo.
If your CPU is overheating, visit our CPU Overheating Guide and CPU Overheating: Symptoms, Causes, & Solutions

To measure CPU Temperatures you may need Real Temp

Intel Core 2 Duo:
(If you wish to access the PDF related to your processor, please first follow the link given under sources at the end of the article instead of directly accessing the pdf link given under each sub heading. This will help you access the latest information on your processor from Intel's website & will also prevent hotlinking.)

1. Intel® Core™2 Duo Processor E8000 Series
Intel ® Core™2 Duo Processor E8600, E8500, E8400, E8300, E8200, E8190 Thermal Profile
Range: 45.1 degree Celsius (Power - 0W) to 72.4 degree Celsius (Power 65W)

CPU Overheating: Symptoms, Causes and Solutions

The most hit pages on this blog are the CPU Temperature Guides, so here is a comprehensive(work in progress) guide to everything(almost) related to CPU Overheating.

Symptoms of a Overheating CPU :

1. Random Crashes:
Your system may switch off or restart automatically while you are using it. And this may happen repeatedly. If the overheating problem is not severe enough, then your PC may only crash when you are running many CPU intensive applications or running a CPU intensive game. If your CPU temperature is very high, then the system may restart as it is booting up ending into a endless and dangerous loop.

2.Sluggish System:
An overheating CPU underperforms, result being a sluggish computing experience. The system may experience random lock-ups too.

Differential Diagnosis:
A PC may also crash due to RAM, Motherboard, Hard Disk & Power Supply Unit issues.

Causes of CPU Overheating:

1. Improper CPU + Heatsink installation:
If your system is new and you have overheating issues, or if you…

CPU Temperature Guide for Intel Pentium 4: Range of Normal CPU Temperatures

Is your CPU Temperature too High? How high is high? Is 60 degree Celsius high enough or is 70 degree Celsius the threshold? Hipatic covers your temperature problems right here. Below we discuss Intel Pentium 4.

Pentium 4 Processors:
Temperatures Specified are Maximum Processor Case Temperatures(Tc). For details refer the sources given.
For 65 nm Process in the 775-land LGA Package supporting Hyper-Threading Technology and Intel® 64 architecture

Thermal Profile for 775_VR_CONFIG_05A ProcessorsRange 44.3 degree (at Power - 0W) Celsius to 69.2 degree Celsius (at Power - 86W)
Source: Page:77 Table:28

Thermal Profile for 775_VR_CONFIG_06 Processors
Range 43.6 degree (at Power - 0W) Celsius to 64.1 degree Celsius (at Power - 64W)
Source: Page:78 Table:29

For 90 nm Process in the 775-land LGA Package supporting Intel® Extended Memory 64 Technology & Supporting Intel® Virtualization Tech…