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

Very basic: Why won't my makefile work with common suffix

本文关键字:不能 后缀 makefile 我的 为什么 非常      更新时间:2023-10-16
objects = hello.o name.o printing.o
exename = himake
$(exename): $(objects)
    $(CC) -o $(exename) $(objects)
%.o: %.cpp
    $(CC) -c  $^

我正在尝试使用常见后缀,因此我不需要首先将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()'

以及我不包括

的更多内容

文件: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);

因为您使用了C编译器前端程序,而不是C 前端程序。

$(CC)更改为$(CXX)