Making a Makefile

Hi!

I'm trying to learn programming and I'm having some trouble with a makefile;

I'm trying to make a makefile but when I go to compile it this error comes up:


$ g++ -o Makefile Makefile.cpp
Makefile.cpp:2:9: error: stray ‘@’ in program
g++ -o $@
^
Makefile.cpp:1:1: error: ‘line’ does not name a type
line.exe: line.cpp

Also, my program is not called line, if that has anything to do with it!


when I tried to make a new makefile all that poped up was that the command was not found

Also, can someone please tell me what is a makefile with debugging info in it?! I know what it means literally, it's a makefile with debugging information but I mean how do you put this debugging into it

Thanks so much!
What do you have in the "Makefile.cpp"?
I have:

all:

g++ -o string string.cpp
Um, there shouldn't be any file named "Makefile.cpp". A makefile is not C++ code.

Here's a simple example to help get started.

parity.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Header file for the parity module

#pragma once
#ifndef PARITY_HPP
#define PARITY_HPP

namespace physicsgirl
{

  bool parity( unsigned long long n );
  // returns TRUE if there are an odd number of bits in n
  // returns FALSE if there are an even number of bits in n

}  // namespace physicsgirl

#endif 

parity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "parity.hpp"

namespace physicsgirl
{

  bool parity( unsigned long long n )
  {
    unsigned p = 0;
    while (n != 0)
    {
      p++;
      n &= n - 1;
    }
    return p & 1;
  }

}  // namespace physicsgirl 

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <sstream>
#include <string>
#include "parity.hpp"
using namespace std;
using namespace physicsgirl;

int main()
{
  while (true)
  {
    cout << "Enter a whole number (or nothing to quit)> " << flush;
    string s;
    getline( cin, s );
    if (s.empty()) break;

    istringstream ss( s );
    unsigned n;
    ss >> n;

    if (!ss.eof())
    {
      cout << "That's not a number! Try again.\n\n";
      continue;
    }

    if (parity( n )) cout << "odd bitcount!\n\n";
    else             cout << "even bitcount!\n\n";
  }
  return 0;
}

Ok, so now we have two source files and a header. You want to make it easy to compile your program (just type 'make' at the command line). We'll write a makefile to do it:

makefile
1
2
3
4
5
6
7
8
all, oddeven, oddeven.exe: parity.o main.o
	g++ main.o parity.o -o oddeven

parity.o: parity.cpp parity.hpp
	g++ -c parity.cpp

main.o: main.cpp parity.hpp
	g++ -c main.cpp

Left of the colon (:) is the target name. Right of the colon is the files required to make the target. The instructions to make the target come underneath. (Notice how that indent is a HT character, not spaces!)

There is a lot more to learn about how to write makefiles. Google around them for help. One of the best places to learn what you can do is by reading the GNU make documentation.

(Your directory should now contain the files:

    main.cpp
    makefile
    parity.cpp
    parity.hpp

Type 'make' or 'gmake' [as appropriate] to build your program.)

Hope this helps.
Topic archived. No new replies allowed.