非常基本:为什么我的makefile不能使用普通后缀

Saj*_*jas 5 c++ makefile gnu-make

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

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

%.o: %.cpp
    $(CC) -c  $^
Run Code Online (Sandbox Code Playgroud)

我试图使用常见的后缀,所以我不需要先将3个文件编译成.o.这应该是使用%通配符完成所有三个.

我做的很长,但不是这样.

运行上面的makefile给出了以下错误:

[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()'
Run Code Online (Sandbox Code Playgroud)

还有更多,我没有包括

文件: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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

name.h

// name.h

#include <iostream>
using namespace std;

string getName();
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

printing.h

// printing.h

#include <iostream>
using namespace std;

void printGreeting();
void printHello(string);
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 5

因为您使用的是C编译器前端程序,而不是C++前端程序.

更改$(CC)$(CXX).