objects = hello.o name.o printing.o
exename = himake
$(exename): $(objects)
$(CC) -o $(exename) $(objects)
%.o: %.cpp
$(CC) -c $^
Tôi đang cố gắng sử dụng các hậu tố chung nên tôi không cần phải biên dịch 3 tệp thành .o trước tiên. Đây là nghĩa vụ phải làm cả ba với ký tự đại diện%.Rất cơ bản: Tại sao tệp makefile của tôi không hoạt động với hậu tố chung
Nó hoạt động tốt khi tôi làm điều đó một chặng đường dài nhưng không phải điều này.
Chạy makefile trên mang lại cho tôi lỗi này dưới đây:
[[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()'
và nhiều hơn nữa mà tôi không bao gồm
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);
Sử dụng '$ CXX' để biên dịch mã C++. –
Ngoài ra, hãy xem xét lại việc bạn sử dụng những gì thường được coi là thực hành xấu: ['using namespace std;'] (http://stackoverflow.com/q/1452721/1171191) (đặc biệt là trong tiêu đề) và ['endl'] (http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (đó là các liên kết đến giải thích). – BoBTFish