5
objects = hello.o name.o printing.o
exename = himake

$(exename): $(objects)
    $(CC) -o $(exename) $(objects)

%.o: %.cpp
    $(CC) -c  $^

I am trying to use common suffixes so I do not need to compile 3 files into .o first. This is supposed to do all three with the % wildcard.

It works fine when I do it the long way but not this.

Running the above makefile gives me this error below:

[alex@pcc Dir]$ make
cc -o himake hello.o name.o printing.o
hello.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
hello.o: In function `__tcf_0':
hello.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()'

and more that I did not include

Files: hello.cpp:

// hello.cpp

// standard library
#include <iostream>
#include <string>
using namespace std;

// user defined header files
#include "name.h"
#include "printing.h"

int main ()
{
    string name;

    name = getName();   // getName is in name.h
    printHello(name);  // printHello is in print.h

    return 0;
}

name.cpp

// name.cpp

// user defined header files
#include "name.h" 
#include "printing.h"

string getName()
{
    string name;
    printGreeting();    // printGreeting is from print.h
    getline(cin, name);  
    return name;
}

name.h

// name.h

#include <iostream>
using namespace std;

string getName();

printing.cpp

// printing.cpp

// user defined include files
#include "printing.h"

void printGreeting(void)
{
    cout << "Your name: ";
    return;
}

void printHello (string  name)
{
    cout <<  "Hi, " << name << endl;
    return;
}

printing.h

// printing.h

#include <iostream>
using namespace std;

void printGreeting();
void printHello(string);
Sajo Ojas
  • 51
  • 2
  • Use `$CXX` to compile c++ code. – πάντα ῥεῖ Feb 23 '17 at 07:40
  • Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) (especially in headers) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Feb 23 '17 at 07:43

1 Answers1

5

Because you use the C compiler frontend program, and not the C++ frontend program.

Change $(CC) to $(CXX).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • oh so gcc is not same as $(CC)? – fman Feb 23 '17 at 07:44
  • I just researched it. yes I think it works with cxx not cc – fman Feb 23 '17 at 07:46
  • 1
    @fman "gcc" is the *Gnu Compiler Collection*. It is also the name used to access the C compiler in that collection. To access the C++ compiler in that collection, you use `g++`. Within a makefile, there are macros for each of the compiler names, because you might use a different compiler on a different platform (for instance, `clang++`). The macro for a C compiler is `CC`, and the macro for a C++ compiler is `CXX`. – BoBTFish Feb 23 '17 at 07:47
  • 1
    @fman On a typical Linux system the command `cc` is an alias for `gcc`, and `c++` is an alias for `g++`. The variable `$(CC)` defaults to `cc`, and the variable `$(CXX)` defaults to `c++`. Thus if you use `$(CXX)` you will be using the `c++` command which is really `g++` which is the GCC frontend program for C++. – Some programmer dude Feb 23 '17 at 07:48
  • 1
    Thanks, got confused with all that. so cc ->> gcc and cxx ->g++, thanks for the detail – fman Feb 23 '17 at 07:50
  • @fman The `make` *variable* is named `CXX`. The *command* is named `c++`. – Some programmer dude Feb 23 '17 at 07:50