tags:

views:

67

answers:

3

This is the first time I am writing a makefile. This doesn't work (for example, if I modify ClientSocket.cc it just says "uptodate". Also throws up lots of dependency errors if I run make myprog). Can you please tell what is wrong in this? Thank you

myprog: myprog.o Client.o ClientSocket.o Socket.o  
     g++  -Wall -g   Socket.o ClientSocket.o Client.o myprog.o -o myprog

myprog.o: myprog.cc  
    g++ -Wall -g -c myprog.cc

Client.o:  Client.cc Client.h
    g++ -Wall -g -c Client.cc

ClientSocket.o: ClientSocket.cc ClientSocket.h
    g++ -Wall -g -c ClientSocket.cc


Socket.o: Socket.cc Socket.h
    g++ -Wall -g -c Socket.cc

Errors when running make myprog:

   cc   mprog.o   -o myprog
   myprog.o: In function `std::__verify_grouping(char const*,   unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
   myprog.cc:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'

There are many others. Like:

          myprog.cc:(.text+0x1a4): undefined reference to `std::cout'
          myprog.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
          collect2: ld returned 1 exit status
+2  A: 

Make sure you are using tabs to indent the command lines (it's impossible to tell whether you are or not from the post, since they would probably be converted to spaces).

EDIT:

From the cc line and the error messages, it looks like make is using its implicit rule for linking, completely ignoring the one you provided (the first one). I can't see anything in the makefile that would cause this. A few more things to check:

  1. Try running other rules by typing, e.g., make Client.o and seeing if they work.
  2. Check that all the files are in your current directory, and that the makefile is named Makefile
  3. Again, I'm sure that it's just the formatting in the post, but your first rule is indented one more space than the other ones. You said you checked it, and I trust you, but I really can't see anything else wrong here.

I know these suggestions are like tech support asking you whether your computer is plugged in, but everything really looks fine to me. I even copied it, fixed the whitespace, and ran it on my machine. Everything seemed to work.

Tim Yates
I checked again. All g++ lines are preceded by one tab and one tab only.
snk
Wow. Thank you Tim.I tried 1) earlier but not 2) The file name was "MakeFile" instead of "Makefile". Thats it. It worked. My bad. I should have checked the name properly. thanks again for your time.
snk
No problem. The frustrating problems always end up smacking you between the eyes in the end.
Tim Yates
A: 

It doesn't look like a makefile problem (because the compiler is being invoked). It looks like a C++ source code problem.

Specifically, it looks like you are missing a #include in myprog.cc of the header that defines std::basic_string.

Have you tried executing the compiler commands directly without using make, to see if your source code compiles correctly?

Addendum

Perhaps your include path is different when you run make. There is a debug/trace option for make that dumps all of its macros (but I don't remember what it is off the top of my head), which might help.

Loadmaster
Hi Loadmaster, The program compiles and works properly if I compile individual files and link them manually.
snk
A: 

Did you name your file "Makefile" with a capital M?

You could also try running make -d for some debug information.

Harold L
The makefile can be named `makefile` or `Makefile`, it doesn't matter.
Loadmaster