简体   繁体   English

如何在“ gedit”编辑器中添加c ++头文件?

[英]How to add a c++ header file in “gedit” editor?

I am a newbie in C++. 我是C ++的新手。 I would like to add the following as a header file. 我想将以下内容添加为头文件。

#include "std_lib_facilities.h"

I have surfed through the internet and have found few tutorials how to add them in visual basics and others but not gedit! 我浏览了互联网,发现了一些教程,如何将它们添加到视觉基础知识和其他教程中,但没有找到gedit!

I am using linux and using gedit as the editor and running the program through terminal. 我正在使用linux,并使用gedit作为编辑器,并通过终端运行程序。

A header file is just a normal source code file. 头文件只是普通的源代码文件。 Open up a new file, and safe it with the name you specify in #include . 打开一个新文件,并使用您在#include指定的名称对其进行保护。

I am using linux and using gedit as the editor and running the program through terminal. 我正在使用linux,并使用gedit作为编辑器,并通过终端运行程序。

You don't directly "run" C++ programs -- you compile them first. 您不会直接“运行” C ++程序,而是先对其进行编译。 There's nothing special you have to do if you put your header file into the same directory as your C++ code -- the compiler will know that it has to look for the header file and include it. 如果将头文件与C ++代码放在同一目录中,则无需做任何特别的事情-编译器将知道它必须查找头文件并将其包括在内。

By the way #include really just looks for a file with that name, and inserts its contents where the #include line was -- nothing magic. 顺便说一下, #include实际上只是查找具有该名称的文件,并将其内容插入到#include行所在的位置–没什么了不起的。

If I may note this: As a C++ beginner, it might actually be a good idea to start off with writing things with a simple editor as gedit to understand how things work. 如果我可能注意到这一点:作为一个C ++初学者,从一个简单的编辑器(如gedit)开始编写东西以了解其工作原理实际上可能是一个好主意。 However, as soon as you need features like header management, you might want to move to something like an IDE. 但是,一旦您需要诸如标头管理之类的功能,就可能想要移至类似IDE的地方。 CodeBlocks is quite popular these days! 如今,CodeBlocks非常受欢迎!

To add a header file, simply add the #include "header_file.h" . 要添加头文件,只需添加#include "header_file.h"

For compilation, if the header file contains the function declaration of some other file say file1.cpp then during compilation, you will have to mention the file1.cpp too. 对于编译,如果头文件包含其他文件(例如file1.cpp的函数声明,则在编译期间,您也必须提及file1.cpp

Something like: 就像是:

g++ file1.cpp main.cpp

Or you can compile them separately and link all .o files. 或者,您可以分别编译它们并链接所有.o文件。

The above answer is assuming that you have all your header files and .cpp files in same directory, else you will have to give relative path to the required files while including header and during compilation and linking. 上面的答案假设您所有的头文件和.cpp文件都位于同一目录中,否则您将不得不在包括头文件以及编译和链接期间提供所需文件的相对路径。

To handle a header file and .cpp file in different directory, when you have large number of files to handle, it would be preferable to use Makefile or CMake files. 要处理不同目录中的头文件和.cpp文件,当您要处理大量文件时,最好使用MakefileCMake文件。 CMake actually makes the Makefile . CMake实际上制作了Makefile This tools make the code easy to compile and link. 该工具使代码易于编译和链接。

Also, for starters using a simple text editor like gedit , sublime text to write a code and terminal to compile and run it, clearly helps in understanding how the large project written into multiple files actually works. 同样,对于使用简单的文本编辑器(例如gedit初学者来说, sublime text编写代码并terminal编译和运行它的terminal ,显然有助于理解写入多个文件的大型项目的实际工作方式。 In case of IDE's it actually handles within themselves and you won't get some to know of some important concepts. 在使用IDE的情况下,它实际上是在自己内部处理的,您不会对某些重要概念有所了解。

It's the same way as you specify any other header file. 与您指定任何其他头文件的方式相同。 At the beginning of your source code (.cpp), just use include "PATH_OF_HEADER_FILE" . 在源代码(.cpp)的开头,只需使用include "PATH_OF_HEADER_FILE" Make sure you use the gcc or any other compiler to link the header files. 确保使用gcc或任何其他编译器链接头文件。 Execute gcc your_cpp_file in the terminal. 在终端中执行gcc your_cpp_file And then run normally. 然后正常运行。

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

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