繁体   English   中英

C++ 在子文件夹中编译.h 和.cpp 时未定义参考

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

长话短说,我想将 my.h 和 .cpp 文件放在子文件夹中(分别是 include 和 src)并在我的 main.cpp 文件中引用它们,但我收到以下错误:

main.cpp:(.text+0x47): undefined reference to `Kmer::Kmer()'.

编译时使用:

g++ -I /path/to/MyFolder/include 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;
}

感谢您的帮助!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM