在子文件夹中编译 .h 和 .cpp 时的 C++ 未定义引用

C++ Undefined Reference When Compiling .h and .cpp in Subfolders

长话短说,我想将我的 .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
#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;
}

感谢您的帮助!

您没有编译 Khmer.cpp。您需要将它添加到您的 g++ 编译行

g++ -o <YOUR APPLICATION NAME> -I /path/to/MyFolder/include main.cpp src/Khmer.cpp