Zev*_*eso 7 c++ g++ undefined-reference
似乎无法让错误消失.错误如下.我已经看过谷歌仍然无法搞清楚.这不像我是Cpp的新手,但有一段时间没有被它愚弄.
奇怪的是它在Windows中使用G ++ ...
错误:
main.cpp中
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Help.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
Run Code Online (Sandbox Code Playgroud)
Help.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
Run Code Online (Sandbox Code Playgroud)
您应该将help.o添加到您的g ++行:
g++ -c help.cpp -o help.o
g++ help.o main.cpp
Run Code Online (Sandbox Code Playgroud)
通过将其拆分为两行,您可以节省编译时间(如果是较大的项目),因为help.cpp只有在更改时才可以编译.make并且Makefile用得好将为您节省很多头痛:
#Makefile
all: main
main: help main.cpp
g++ -o main help.o main.cpp
help: help.cpp
g++ -c -o help.o help.cpp
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18237 次 |
| 最近记录: |