C++ 非常非常基本:为什么赢';我的makefile不工作吗
运行上面的makefile会出现以下错误: [alex@pcc目录]$make g++-o himake hello.o printing.o name.o 你好。o:在函数“main”中: hello.cpp:(.text+0xc8):对“printHello(std::basic_string,std::allocator>)的未定义引用 collect2:ld返回了1个退出状态 制造:**[himake]错误1 档案: hello.cpp:C++ 非常非常基本:为什么赢';我的makefile不工作吗,c++,linux,makefile,gnu-make,C++,Linux,Makefile,Gnu Make,运行上面的makefile会出现以下错误: [alex@pcc目录]$make g++-o himake hello.o printing.o name.o 你好。o:在函数“main”中: hello.cpp:(.text+0xc8):对“printHello(std::basic_string,std::allocator>)的未定义引用 collect2:ld返回了1个退出状态 制造:**[himake]错误1 档案: hello.cpp: himake: hello.o printing
himake: hello.o printing.o name.o
g++ -o himake hello.o printing.o name.o
hello.o: hello.cpp
g++ -c hello.cpp
printing.o: printing.cpp
g++ -c printing.cpp
name.o: name.cpp
g++ -c name.cpp
姓名.h
// 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
#包括
使用名称空间std;
字符串getName();
打印.cpp
// name.h
#include <iostream>
using namespace std;
string getName();
//printing.cpp
//用户定义的包含文件
#包括“printing.h”
void打印问候语(void)
{
您可以声明printHello
:
// printing.h
#include <iostream>
using namespace std;
void printGreeting();
void printHello(string);
…然后调用它,但从未定义它。如果没有定义,链接将失败。(这与makefile无关)
可能printing.cpp中定义的printHi
函数应该是printHello
的定义,如果该代码是准确的,则函数printHello没有定义。函数名需要精确匹配printHi
或printHello
?天哪,这是一个无关的注释,请阅读。不要不要在头文件中使用名称空间std;
。有很多例子说明了为什么搜索一点就不好了。我还建议阅读。
// printing.cpp
// user defined include files
#include "printing.h"
void printGreeting(void)
{
cout << "Your name: ";
return;
}
void printHi (string name)
{
cout << "Hi, " << name << endl;
return;
}
// printing.h
#include <iostream>
using namespace std;
void printGreeting();
void printHello(string);
void printHello(string);