How to add a c++ header file in "gedit" editor?

1.4k views Asked by At

I am a newbie in 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!

I am using linux and using gedit as the editor and running the program through terminal.

3

There are 3 answers

0
djo On BEST ANSWER

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". Make sure you use the gcc or any other compiler to link the header files. Execute gcc your_cpp_file in the terminal. And then run normally.

0
Marcus Müller On

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.

I am using linux and using gedit as the editor and running the program through terminal.

You don't directly "run" C++ programs -- you compile them first. 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.

By the way #include really just looks for a file with that name, and inserts its contents where the #include line was -- nothing magic.

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. However, as soon as you need features like header management, you might want to move to something like an IDE. CodeBlocks is quite popular these days!

0
Rakholiya Jenish On

To add a header file, simply add the #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.

Something like:

g++ file1.cpp main.cpp

Or you can compile them separately and link all .o files.

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.

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. CMake actually makes the 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. In case of IDE's it actually handles within themselves and you won't get some to know of some important concepts.