Page 1 of 1

Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-28 07:15pm
by Dave
Alright,so I'm trying to learn more C++ via Stanford's SEE online courses.
Specificaly, I am working on CS106B -- Programming Abstractions.

I downloaded the course materials, including the assignments and stuff. But it didn't have any of the ".h" libraries that were called by their program. I found them on the class website, but they were packaged for Apple's Xcode and Microsoft's Visual Studio. A friend of mine was kind enough to unpack them into the bare .h files from Xcode and email them to me.

My programing experience is limited to a course in C++ that did not include anything about multi-file programs. We barely covered functions.

So I tried the warmup assignment for the course, and it will not compile.

When I try to compile (using the command below -- I'm using Linux)

Code: Select all

$ g++ -Wall -W -s -pedantic-errors assign1warmup.cpp 
I get:

Code: Select all

assign1warmup.cpp: In function ‘int Hash(std::string, int)’:
assign1warmup.cpp:37: warning: comparison between signed and unsigned integer expressions
/tmp/ccvo6keq.o: In function `main':
assign1warmup.cpp:(.text+0x100): undefined reference to `GetLine()'
collect2: ld returned 1 exit status
(If I compile without the flags for g++ I don't get the warning about signed and unsigned integers)


If you ask me, it looks like the header file "simpio.h" never actually defines GetLine() (it just states that it exists) but can someone with more experience point out what the problem is? (even better, how to fix it!)


assign1warmup.cpp

Code: Select all

/*
 * Program: warmup.cpp
 * --------------------
 * Program used to generate a hash code based on user's name.
 * As given, this code has two compiler errors you need to track down 
 * and fix in order to get the program up and running.
 *
 * jzelenski Thu Apr  1 12:27:53 PST 2004
 */

#include <iostream>
#include "genlib.h"
#include "simpio.h"

using namespace std;

#define MAX_HASH_CODE	10000	// Upper bound for hash codes generated by program

/* Function prototypes */

int Hash(string s, int maxCode);


/*
 * Function: Hash
 * Usage: hash_number = Hash(key, max_hash);
 * -----------------------------------------
 * This function takes the key and uses it to derive a hash code,
 * which is an integer in the range [0, maxCode - 1].  The hash
 * code is computed using a method called linear congruence.
 */
#define Multiplier -1664117991L		// Multiplier used in Hash function

int Hash(string s, int maxCode)
{
    unsigned long hashcode = 0;
    for (int i = 0; i < s.length(); i++) 
        hashcode = hashcode * Multiplier + s[i];
   return (hashcode % maxCode);
}

int main ()
{
	cout << "Please enter your name: ";
	string name = GetLine();
	
	int hashcode = Hash(name, MAX_HASH_CODE);
	cout << "The hash code for your name is " << hashcode << "." <<endl;

	return 0;
}

genlib.h

Code: Select all

#ifndef GENLIB_H_
#define GENLIB_H_

/* 
 * File: genlib.h
 * Version: 1.0CPP
 * Last modified on Wed Sep 18 13:41:31 PDT 2002 by jzelenski
 * ----------------------------------------------------------
 * This header file is indicated to be included in
 * all the programs written for CS106B/X and provides a few
 * common definitions. Note this header has a "using namespace std" 
 * clause. If a file includes this header, it can then use
 * features from the std namespace without qualifying by scope. 
 */

#include <string>
using namespace std;

/*
 * Function: Error
 * Usage: Error(msg)
 * ------------------
 * Error outputs an error string to the cerr stream and
 * then throws a string exception corresponding to the error.
 */

void Error(string str);


/*
 * Function macro: main
 * --------------------
 * The purpose of this macro definition is to rename the student
 * main to Main in order to allow a custom main defined in our
 * libraries to configure the application before passing control
 * back to the student program.
 */
#ifdef __APPLE__
#define main Main
#endif

#endif /*GENLIB_H_*/
simpio.h

Code: Select all

#ifndef SIMPIO_H_
#define SIMPIO_H_
/*
 * File: simpio.h
 * Version: 1.0CPP
 * Last modified on Wed Sep 18 13:34:29 PDT 2002 by jzelenski
 * ----------------------------------------------------------
 * This interface provides access to a simple package of
 * functions that simplify the reading of console input.
 */

#include "genlib.h"

/*
 * Function: GetInteger
 * Usage: n = GetInteger();
 * ------------------------
 * GetInteger reads a line of text from standard input and scans
 * it as an integer.  The integer value is returned.  If an
 * integer cannot be scanned or if more characters follow the
 * number, the user is given a chance to retry.
 */
 
int GetInteger();


/*
 * Function: GetLong
 * Usage: n = GetLong();
 * ---------------------
 * GetLong reads a line of text from standard input and scans
 * it into a long integer.  The long is returned.  If the 
 * number cannot be scanned or if extra characters follow it,
 * the user is given a chance to retry.
 */
 
long GetLong();

/*
 * Function: GetReal
 * Usage: x = GetReal();
 * ---------------------
 * GetReal reads a line of text from standard input and scans
 * it as a double.  If the number cannot be scanned or if extra
 * characters follow after the number ends, the user is given
 * a chance to reenter the value.
 */

double GetReal();


/*
 * Function: GetLine
 * Usage: s = GetLine();
 * ---------------------
 * GetLine reads a line of text from standard input and returns
 * the line as a string.  The newline character that terminates
 * the input is not stored as part of the string that is returned.
 */
 
string GetLine();

#endif /*SIMPIO_H_*/

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-28 11:35pm
by Beowulf
You're going to need the associated library for the .h file before you can actually use them. .h files simply specify what's in a library, and the prototypes associated with them, not the functions themselves. The functions are declared in the library.

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 08:52am
by Dooey Jo
As Beowulf said, you need the actual library, not just the headers, because this is actually a linking error, not a compile error. You will either need to get the library files (usually they have the extension .a or .lib), or the source code for the libraries and compile that too. Then you have to link your program to that, and I know g++ has commands for that, but I don't know what they are.

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 12:07pm
by Sarevok
It seems like a very superfluous and trivial problem

Rewrite this -

Code: Select all

   cout << "Please enter your name: ";
   string name = GetLine();
remove GetLine() and write it as.

Code: Select all

   cout << "Please enter your name: " << endl;
string name;
   cin >> name

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 04:18pm
by Dave
Beowulf, Dooey Jo:
Ok, I found a "libcs106.a" file in with the stuff my friend sent me, so I assume that is the actual library. Now I just need to figure out how to link it in. Thanks for the help!

Sarevok: I knew I could do that, I used it in my simple C++ class. But I wanted to try all the assignments mentioned in the SEE course, which include some random number generation, basic point and click graphics, and other stuff thats all done conveniently in the library. If I had just done a "cin", I would have been stuck on how to work the rest of the assignments (or, all the assignments that don't involve inputing strings). Since Beowulf and Dooey Jo explained what else was needed, I now know what I need to research to make this compile the way it was supposed to in the original assignment.

Hopefully I'll figure it out.

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 04:54pm
by Dave
Ok, so it seems, for g++, the "-L" flag allows you to specify custom directories to look for libraries in, and the "-l" option allows you to call libraries from those directories.

The flag "-Llib" represents the directory "./lib/" and the flag "-lfoo" would call the library "libfoo.a".

Now, with the file libcs106.a in the same directory as the code I'm trying to compile,

when I run

Code: Select all

$ g++ assign1warmup.cpp -L. -lcs106
I get

Code: Select all

./libcs106.a: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Now what?

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 10:54pm
by Beowulf
It means that the file libcs106.a isn't a valid library (for g++ on your system). You'll need to either get a copy that does work, or get a copy of VC++ (express edition should work fine, I think)(and a copy of windows to run it on).

Re: Learning C++ online with SEE CS106B -- Compile errors

Posted: 2008-12-29 11:50pm
by Dave
Fan-tastic. </sarcasm>

Thanks for the help!