【发布时间】:2017-02-23 07:37:37
【问题描述】:
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()'
还有更多我没有包含的内容
文件: 你好.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;
}
名称.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;
}
名称.h
// name.h
#include <iostream>
using namespace std;
string getName();
打印.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;
}
打印.h
// printing.h
#include <iostream>
using namespace std;
void printGreeting();
void printHello(string);
【问题讨论】:
-
使用
$CXX编译c++代码。 -
另外,请重新考虑您对通常被认为是不良做法的使用:
using namespace std;(尤其是在标题中)和endl(这些是解释的链接)。