Very simple: Why does my makefile not work with a common suffix

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 in .o first. It is assumed that all three will be executed using the% pattern.

It works great when I do it a long way, but not that.

Running the above makefile gives me the following error:

 [ 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 moreover, what 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); 
+5
source share
1 answer

Since you are using an external C compiler program, not an external C ++ program.

Change $(CC) to $(CXX) .

+5
source

Source: https://habr.com/ru/post/1264610/


All Articles