简体   繁体   English

对C ++ #include感到困惑

[英]Confused about C++ #include

My project has this folder structure: 我的项目具有以下文件夹结构:

Project/
--Classes/
----Class1.h
----Class1.cpp
--main.cpp

"Class1.h" contains method definitions, "Class1.cpp" is the source code for "Class1.h". “ Class1.h”包含方法定义,“ Class1.cpp”是“ Class1.h”的源代码。

The source code of "Class1.h" is like this: “ Class1.h”的源代码如下:

class Class1 {
  public:
    void do_something();
};

The source code of "Class1.cpp" is like this: “ Class1.cpp”的源代码如下:

#include "Class1.h"

void Class1::do_something() {
  //
} 

The source code of "main.cpp" is like this: “ main.cpp”的源代码如下:

#include "Classes/Class1.h"

int main(int argc,char** args) {
  Class1* var = new Class1();
  var->do_something();
  return 0;
}     

However, when compiling "main.cpp", the compiler doesn't know where the implementation of methods in Class1 is, so it shows linking error about undefined reference. 但是,在编译“ main.cpp”时,编译器不知道Class1中方法的实现在哪里,因此它显示有关未定义引用的链接错误。

Do I have to add any path into the command line so the compiler knows what source files it has to compile? 我是否必须在命令行中添加任何路径,以便编译器知道必须编译哪些源文件? How to tell the compiler that it must compile "Class1.cpp" also? 如何告诉编译器还必须编译“ Class1.cpp”?

You need to feed all files in your project to the compiler, not just "main.cpp". 您需要将项目中的所有文件送入编译器,而不仅仅是“ main.cpp”。 Here you can read about the basics of compiling multiply files together with Gcc. 在这里,您可以了解与Gcc一起编译乘法文件的基础知识。

Another option would be to compile your classes as a dynamic or static library, but you should start with simply compiling them together if you're not quite familiar with libraries. 另一种选择是将您的类编译为动态或静态库,但是如果您不太熟悉库,则应从简单地将它们一起编译开始。

The correct way to do it is doing the header inclusion in the Class1.cpp file. 正确的方法是将标头包含在Class1.cpp文件中。 That way if the Class1.cpp is compiled as a library, you can use the header file to get the declarations. 这样,如果将Class1.cpp编译为库,则可以使用头文件来获取声明。

The other way around, if you will directly use the Class1.cpp, compiling it with your project. 反之,如果您将直接使用Class1.cpp,则将其您的项目一起编译。 You should include Class1.cpp in your main.cpp. 您应该在main.cpp中包括Class1.cpp

You need to know about building (compiling and linking) C++ applications. 您需要了解有关构建(编译和链接)C ++应用程序的知识。 This topic usually don't describe in programming books about C++ and only way to do it - google and programming community sites with articles. 这个主题通常不会在有关C ++的编程书籍中进行描述,而仅是实现该主题的唯一方法-谷歌和编程社区网站(含文章)。

Fast answer is: 快速的答案是:

g++ -c Classes/Class1.cpp -o Class1.o
g++ -c main.cpp -o main.o
g++ Class1.0 main.0 -o ProjectName

It's a simple set of commands to compiling and linking program. 这是一组用于编译和链接程序的简单命令。 Usually it would be done by build system (make, qmake, cmake, waf, scons, ant etc). 通常它将由构建系统(make,qmake,cmake,waf,scons,ant等)完成。 Also, IDE can build program without additional configuration, Visual Studio for example. 此外,IDE可以在不进行其他配置的情况下(例如Visual Studio)构建程序。

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

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