简体   繁体   English

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

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

Long story short I want to put my.h and.cpp files in subfolders (include and src respectively) and reference them in my main.cpp file but I am receiving an error of:长话短说,我想将 my.h 和 .cpp 文件放在子文件夹中(分别是 include 和 src)并在我的 main.cpp 文件中引用它们,但我收到以下错误:

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

when compiling using:编译时使用:

g++ -I /path/to/MyFolder/include main.cpp . g++ -I /path/to/MyFolder/include main.cpp

My files are structured like below:我的文件结构如下:

  • MyFolder我的文件夹
    • main.cpp主文件
    • include包括
      • Kmer.h Kmer.h
    • src源代码
      • Kmer.cpp 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;
}

I appreciate the help!感谢您的帮助!

You are not compiling Khmer.cpp.您没有编译 Khmer.cpp。 You need to add it to your g++ compile line您需要将其添加到您的 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