Question

Ok so, im supposed to define a variable HASH_TABLE_SIZE at link time, so I would put that in my makefile.

my Makefile is as follows:

  1 CC = g++
  2 CFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -D HASH_TABLE_SIZE=10
  3 LDFLAGS = -lrt
  4
  5 myhash : main.o hash.o hash_function.o
  6     $(CC) $(LDFLAGS) main.o hash.o hash_function.o -o myhash
  7
  8 main.o : main.cpp hash.h
  9     $(CC) $(LDFLAGS) main.cpp
  10
  11 hash.o : hash.cpp hash.h
  12     $(CC) $(LDFLAGS) hash.cpp
  13
  14 hash_function.o : hash_function.cpp hash.h
  15     $(CC) $(LDFLAGS) hash_function.cpp
  16
  17 clean :
  18     rm *.o myhash

my Makefile seems correct to me, and I am putting -D HASH_TABLE_SIZE=10. However, when I do make, I get this error:

 In file included from main.cpp:3:0:
 hash.h:24:27: error: 'HASH_TABLE_SIZE' was not declared in this scope
 list<string> hashTable[HASH_TABLE_SIZE];

my Hash.h file is as follows:

 1 /* This assignment originated at UC Riverside. The hash table size
 2  should be defined at link time. Use -D HASH_TABLE_SIZE=X */
 3
 4 #ifndef __HASH_H
 5 #define __HASH_H
 6
 7 #include <string>
 8 #include <list>
 9
 10 using namespace std;
 11
 12 class Hash {
 13
 14 public:
 15   Hash();
 16   void remove(string word);
 17   void print();
 18   void processFile(string fileName);
 19   bool search(string word);
 20   void output(string fileName);
 21   void printStats();
 22
 23 private:
 24    list<string> hashTable[HASH_TABLE_SIZE];
 25    int collisions;
 26    int longestList;
 27    double avgLength;
 28
 29 private:
 30    int hf(string ins);
 31    double newAvgListLen;
 32
 33 // put additional variables/functions below
 34 // do not change anything above!
 35
 36 };
 37
 38 #endif

Why is this happening? Any help would be greatly appreciated.

Était-ce utile?

La solution

The quick solution

You are not using CFLAGS in your recipes so it is having no effect.

It should work if you change lines 8 to 15 to:

8 main.o : main.cpp hash.h
9     $(CC) $(CFLAGS) main.cpp
10
11 hash.o : hash.cpp hash.h
12     $(CC) $(CFLAGS) hash.cpp
13
14 hash_function.o : hash_function.cpp hash.h
15     $(CC) $(CFLAGS) hash_function.cpp

Some extra details

-D is a compile time flag not a link time option.

CFLAGS is conventionally used to pass compile time flags to the compiler for a C program. Normally CXXFLAGS would be used for a C++ program such as this and CXX to specify the C++ compiler. Make doesn't really mind though but it can make it easier to follow when conventions are used.

LDFLAGS is normally used for passing link time flags like -lrt so it only makes sense to use that on line 6, not in the compiling steps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top