2017-02-23 9 views
5
objects = hello.o name.o printing.o 
exename = himake 

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

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

Próbuję użyć pospolitych przyrostków, więc nie muszę kompilować 3 plików w .o pierwszy. To ma zrobić wszystkie trzy z% wildcard.Bardzo podstawowe: Dlaczego mój plik Makefile nie będzie działał ze wspólnym przyrostkiem?

Działa dobrze, gdy robię to na dłuższą metę, ale nie to.

Running powyższy makefile daje mi ten błąd poniżej:

[[email protected] 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()' 

i bardziej że nie obejmują

pliki: 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); 
+0

Użyj '$ CXX', aby skompilować kod C++. –

+0

Powinieneś także przemyśleć swoje użycie często uznawanych za złe praktyki: ["using namespace std;'] (http://stackoverflow.com/q/1452721/1171191) (szczególnie w nagłówkach) i ['endl'] (http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (są to linki do wyjaśnień). – BoBTFish

Odpowiedz

5

Ponieważ używasz programu frontend kompilatora C, a nie C++ programu frontend.

Zmień $(CC) na $(CXX).

+0

oh więc gcc nie jest taki sam jak $ (CC)? – fman

+0

Właśnie to zbadałem. tak Myślę, że działa z cxx nie cc – fman

+0

Wygląda na to, że zadziałało! –

Powiązane problemy