Thursday, September 16, 2010

CS106B, Libraries, and following along with the Videos

I was able to sucessfully complete the Free CS106A course - Programming-methodology- available online from Stanford University and was excited to start the second course in the series - CS106B - Programming Abstractions. The first course was fantastic and I learned a lot including learning a new programming language, Java, which I had very little experience with. I had a bit more experience with C and some C++ but not enough that I would consider myself to be a developer. Well... getting back into the C/C++ world, starting CS106B, reminded me just what a nightmare using Visual Studio could be.

For starters, when I reviewed the material I needed for the course, I had no end of trouble tracking down the text book. For the CS106A course I was able to go to and order - "The Art & Science of Java" by Eric Roberts which was expensive but invaluable when taking this class online. I was hoping something similar would be available for this course. While there was something available called "Programming Abstractions in C" it was made clear that the course would actually use a new version that had been updated for C++. I searched all over until I finally found it available for download on scribd. It cost money for me to sign up for the day to download the text but at the time I felt it was totally worth it! ($5 for a textbook... what a bargain!). However, later I found that the reader is available free for download from the CS106B website.

Now that I had the text, I was excited to dive in and start! I watched the first 4 videos online, read the first 3 chapters and got ready to boot up my development environment and dig in! Of course things didn't go smoothly. For starters, the CS106B Windows libraries that you download for the course are built into an installer which requires that you have VS2005 installed on your machine. My machine had 2008 and 2005 installed but the installer still refused to run and after various attempts to dig the files out of the installer manually I went looking elsewhere for the libraries. I tried various ways of getting the libraries and making them work with the programs downloaded with the lecture videos to no avail. Linker errors galor! Oh Visual Studio how I love thee! :)

Finally I stepped back and started fresh and came up with the following steps to get these older programs (circa 2006) with the new libraries from the 2010 class:

Steps to Make CS106B projects Work with new Libraries

Assumes you’ve already downloaded the Full set of materials and unzipped the seven programming assignments into their own folders.

1.) Download one of the latest projects from the Stanford CS106B Website:

2.) Unzip the project and find its subdirectory \CS106. This is the folder that contains all the library (CS106CPPLib.lib) and header files for the CS106 program.

3.) Copy this folder as a subdirectory to one of your assignment folders (ex. C:\Assignment1\CS106.

4.) Startup Visual Studio2008 and click File->New->Project… When given the choice choose to create a new Console program.

5.) You’ll then be presented with a new project with multiple files including stdafx.h, stdafx.cpp and similarly named files for whatever you named your project when prompted earlier. DELETE/ REMOVE all these files from the project.

6.) Right click the Source File Folder and choose Add->Existing Item… Choose the first assignment file (Assign1warmup.cpp).

7.) If you compile at this point you’ll realize that it’s still looking for the stdafx.h precompiled header. Let’s fix that. Go to Project->Assignment1 Properties… (or whatever your project might be named). Go to the option in the “tree” menu for Configuration Properties->C/C++->Pre-Compiled Headers . Under the option for Create/Use Precompiled header, choose the drop down option for Not Using Precompiled headers.

8.) While you’re in this section, take the time now to also select the location for the header files for this project. Under the Configuration Properties->C/C++->General section choose the Additional Include Directories option and use the … to browse and select the CS106 folder that you copied to your project folder earlier.

9.) One last thing you should change here is in the Configuration Properties->C/C++->Code Generation section. Change the default (which I believe is for a Multi-Threaded Debug .DLL) to just Multi-Threaded Debug (or just Multi-Threaded if you are doing your release build).

10.) Lastly, we’ll need to let the linker know where to find the CS106 library file. To do that you’ll need to make 2 additional settings. One is in the Configuration Properties->Linker->General section where you can select the location of your additional library files in the section titled appropriately enough – “Additional Library Directories”. You’ll also need to add your library to the project through the “Additional Dependencies” section which is under Configuration Properties->Linker->Input. Just type in CS106CPPLIB.lib.

Note: I found that there seems to be some difference in the menus you get and the settings that get saved depending on what your selection is at the moment in the solution Explorer Window. I originally made all my changes while I was selecting the actual source file, but there were no linker menus at that point. I had to move up to the Source File Folder level to get additional menus where I could set my linker options. Also important to note is that if you make your settings at the lower level (ie. Cpp file) they seem to supersede whatever is set in the level above it.

At this point the project should correctly build with the selection of the Build->Rebuild Solution command.

Happy programming!

Some may wonder why I didn't just use the new sample programs with the updated materials. The main reason is that I didn't have access to the online videos related to those classes. If the CS106B videos are anything like the CS106A classes there will likely be references made to the assignments during the lectures to help make certain things more clear. Also, with the new assignments I really wasn't sure at what point I should "take them on" during the course since it seemed possible that they might address different material in the newer classes or at the very least approach things in a different order or time frame. I may try some of the updated projects after the course or along the way but for now I'm satisfied that I can now use the assignments that came with the lecture videos.

Hope this helps someone else who may have similar problems.


Anonymous said...

Finally someone with teaching skills.
I struggled for days and read through like a thousand blogs. Yours is the only one that worked for me. Thank you. You did save my day, as it is christmas.. Never give up..

Anonymous said...

If you download the blank project from the new cs106b, it will work with vs2008 express with no problems. The blank project sheet has everything you need. Just open it with vstudio.
Good luck.

Mel B said...

That is true. However, as I noted at the end of my post, I wanted to use the older projects that went along with the videos for the CS106B class that were online. The latest projects were different from the ones that went along the online lectures.

nzny said...

dear mel,

i've tried several times to download the libraries from suggested links. unfortunately, i failed. what i got to see was only the documentation of the files. there's no download button. can you suggest me any alternative to get the cs106B libraries. thank you very much.

Mel B said...

Did you try the link later in the post:
I find that link still works when I try it.

Judith said...

Mel, you are a hero! Thank you for posting all the steps to take so carefully and well, so that the rest of us don't need to go through the pain! That must have taken forever to figure out...

Mel B said...

So glad that helped! Yes, it drove me crazy for a while and I couldn't find any help for it online so I figured I'd post what I learned to help anyone else that might run into it and save you some frustration! :)

-Mel B.

Anonymous said...

Thank you for this post. I am quite sure a lot of people would have benefited from this.

I am still struggling to get the code working in Eclipse IDE (with C/C++ perspective).

Anonymous said...

thanks for posting this. unfortunately after following the instructions, i'm getting "fatal error LNK1120: 4 unresolved externals". Do you have any suggestions on what I should do?

Rahul Banyal said...

Thanx for this documentation its very clear and helpful, but i still got an error error LNK2005 please help me, i am using VS2008

Nathan said...

Thanks for the post, super helpful!

a couple of edits from my experience:
- link to get the libraries
- make sure to use a win32 console application, not the CLR console application
- for the C/C++ code generation runtime library setting, I kept getting linker errors using Multi-threaded Debug (/MTd). Switching back to the default of Multi-threaded Debug DLL (/MDd) fixed it.
- when you get your program compiled to run, use ctrl+f5 to run without debugging and the console window will stay open until you close it

Thanks again!

Anonymous said...

It appears they've deleted all of the blank assignments from their websites, possibly because it seems there's a new instructor. Anyone have a mirror of the project files?

Anonymous said...

It turns out you can Google the name of the file itself and some other university has mirrored the blank project.

nikunj banka said...

great work mel B . this blog really helps!!! Hope , there were more people like you !!!

ajayu2bono said...

Hi mel,
The link you provided does not work now. do you know how to find the updated course materials from the last completed sememster?


Mel B said...


I noticed the same thing when trying to get back to these lessons. I've decided to just go ahead and use the latest from the CS106B Engineering Everywhere Stanford site here:

The course reader and most recent assignements are there. I noticed someone else said on the forums that the videos don't really reference the original assignments anyways so you should have no problem using the latest set of assignments at this location. Good Luck! :)

Christopher Chen said...


Thanks for this, it really has been helpful! The only problem I have is that the newest libraries don't include many of the classes of the classes included in previous libraries such as genlib.h, grid.h, etc., therefore the starter files which make use of those classes don't compile. Do you have any solution to this other than simply rewriting every starter file?

Sid Reddy said...

Anyone know how to do this with Xcode?

Anonymous said...

In retrospect, would doing the assignments from the latest class that uses Qt-Creator be problematic?

jobtwes said...

I too did did CS106A but I'm failing to add the CS106B libraries to Visual Studio because I have no clue how to use it....
Please send me any helpful step by step instruction on atleast setting up one project to work because I tried Mel's solution and it failed to work for me. My email is:

LSAN said...

Hi Mel,

I am using VS Express for Desktop and Windows 8.1. Your instruction worked very well. The only extra thing I have to do is to right click at Solution Explorer-->Source Files and select "add existing..." and add the ".cpp" file of the correspondent ".h" file I am including in my code. I don't know the reason but it was the way I found to make it work. Now I can follow the lectures and practice.

Clemens said...

Hello everybody. I Have a different problem. I managed to find Visual Studio 2005 now in the year 2015 and installed it on some old computer with Windows xp. I also found old stanford library (PCLibs-VS2005-ADT-Installer) which can be installed only with Visual Studo 2005 professional edition. Then I also installed this library. I did this because the 106b tutorials from 2008 on Youtube also use visal studio 2005 professional edition. The problem is that compiler still cannot recognize the #include"simpio.h" and #include"genlib.h" files.
Now I really have no idea what to do. I am unsuccesfully searching old blogs on stanford 106b libraries. Everybody have a problem with later editions of Visual Studio, nobody with VS 2005. Help me

Clemens said...

It is Clemens again. It's little ebarrassing. You probably know now that I am total beginner. I found out the solution myself. It is very simple. I Couldn't find the soluton on other blogs or other websites. So I will describe the solution in the next few lines myself.
1. Dowload and install Visual Studio2005 and after that install cs106b/x c++ library.
2. Lounch Visual Studio, select new project in file menue, then select visual c++.
3. Under Visual Studio installed templates select cs106b/x assignment wizard, type name of the project below and press enter.
4. The small window will show up. Leave checkbox - Create an empty cpp file to get me started - selected. in the namefield type the name of new file with .cpp ending and press enter.

That's it. It is simple. But without instructions it took me two long days. If you want learn the Stanford 106b programming abstraction from videos on Youtube and you have no idea how to install software, you know it now.