What should be the structure of the project in C ++?

I recently started to learn C ++ and, based on the Ruby environment, it was very difficult for me to structure the project so that it still compiles correctly, I use Code :: Blocks, which is brilliant, but the disadvantage is that when I add new header file or C ++ source file, it generates some code, and although it is only 3 or 4 lines, I do not know what these lines do. First of all, I would like to ask this question:

What do these lines do?

#ifndef TEXTGAME_H_INCLUDED #define TEXTGAME_H_INCLUDED #endif // TEXTGAME_H_INCLUDED 

My second question is: I need to #include both the .h file and the .cpp file and in what order.

My third question is: where can I find the GNU GCC compiler, which I believe was packaged using Code :: Blocks and how to use it without Code :: Blocks? I would rather develop in notepad ++, because this is what I'm used to in Ruby, but since C ++ is compiled, you can think differently (please give advice and opinions on this)

Thanks in advance, ell.

EDIT: I am on Windows XP and thank you for quickly covering the answers!

+4
source share
4 answers

To answer your questions:

  • Lines include protective devices. They prevent the inclusion of a header file more than once in any given translation unit. If it has been turned on several times, you will probably get several definition errors.

  • Header files # are included in .cpp files and in other headers ..cpp files are usually not included.

  • The C ++ compiler that ships with Code :: Blocks is called MinGW GCC and can be found in the bin directory of the MinGW installation. To find it, do a Windows search through explorer for "g ++". To use it, you will need to place the directory in which it is located on your search path. Please note that the version of the compiler that ships with Code :: Blocks is quite old - you can get a newer version from here .

+3
source

This is an inclusion protection to prevent the inclusion of the .h file twice. In addition to saving time, it is often required to avoid doubling things up.

You should include only .h. .C file associated with your program in one form or another. For smaller programs, you can simply transfer all .c files to gcc, but larger programs will contain intermediate .o files or even libraries ( static or dynamic ).

You can work without an IDE. There are many ways to install the gcc compiler on Windows, including Cygwin and MinGW . I think you're right that Code :: Blocks comes with the gcc executable, but I don’t know where it is or what version.

+3
source

These lines make it so that if the file contains # twice, everything will work. This, in turn, allows us to treat header-file dependencies as a simple directed graph, which, of course, is the easiest.

You are not #include.cpp files. (Well, if you are not an evil programmer, do not do this!)

I will let others (or google!) Tell you about gcc, but this can help if you want to describe which platform you are using.

+1
source

All of your questions had answers other than this:

I would rather develop in notepad ++ because this is what I use in Ruby, but since C ++ is compiled, you may think differently (please give tips and opinions on this subject as well)

I think this is a very bad idea. Full-fledged IDE with integrated debugger, transition to character definitions, refactoring capabilities, profiler, intellisense, etc. It is almost a must for any real project.

And the best is Visual Studio * with Visual Assist X **. Code :: Blocks fade in comparison;)


* If you study at a university, you can get it for free through MSDNAA; otherwise, there is a version of Visual Studio Express that is free
** 30-day evaluation period

-1
source

Source: https://habr.com/ru/post/1307933/


All Articles