【发布时间】:2020-12-16 16:28:59
【问题描述】:
长话短说,我想将我的 .h 和 .cpp 文件放在子文件夹中(分别为 include 和 src)并在我的 main.cpp 文件中引用它们,但我收到以下错误:
main.cpp:(.text+0x47): undefined reference to `Kmer::Kmer()'.
编译时使用:
g++ -I /path/to/MyFolder/include main.cpp.
我的文件结构如下:
- 我的文件夹
- main.cpp
- 包括
- Kmer.h
- 源
- Kmer.cpp
//main.cpp
#include <iostream>
#include "Kmer.h"
using namespace std;
int main() {
Kmer k;
return 0;
};
//Kmer.h
#pragma once
class Kmer{
public:
Kmer();
protected:
private:
};
//Kmer.cpp
#include "Kmer.h"
#include <iostream>
using namespace std;
Kmer::Kmer(){
// code here
cout << "Kmer created" << endl;
}
感谢您的帮助!
【问题讨论】:
-
#include "Kmer.h"也许你想要#include "include/Kmer.h" -
感谢您的回复!我这样做只是为了测试它,我收到了相同的错误消息。如果可能的话,我也更喜欢在 g++ 中使用 -I 命令,而不是在我的代码中使用相对路径。
标签: c++ compiler-errors g++ subdirectory