V
V
verybigbear2014-01-18 22:24:11
C++ / C#
verybigbear, 2014-01-18 22:24:11

How to move the code to different files?

Good day.
Can you please tell me how to put code into different files in c++?
I am writing a program, it has already grown to 3000+ lines of code and all this in one cpp file!
How do I properly split the code into different files?
A side question: it would be great to move the code into separate dlls, but how difficult is it and where can I read it?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
N
niosus, 2014-01-19
@niosus

The complexity of this action depends on two factors:
- on the quality of the code at the moment
- on the quality of what you want to get as an output
On the first point, if your code is correctly divided into classes and / or functions, then there should not be a problem. As already mentioned, just put all the definitions in the headers, leave everything else in .cpp. On the other hand, I have little idea how to write competent code in one file.
This brings us to the second point. In order to end up with a quality product, a logical division into subtasks is necessary. If this is done correctly, it will lead to small functions that always do one thing and / or small classes that are responsible for one entity. Such a structure is easy to move to different files.
If you have a lot of global variables, etc., then the code can also be easily split into different files, but grouping by files will be difficult, and there will not be much sense in this.
PS. Not sure if you know, but don't forget in every .hfile:

#ifndef MY_AWESOME_HEADER_H
#define MY_AWESOME_HEADER_H
// код вашего файла
#endif

this will save you from subsequent re-inclusions of this file.

K
KOLANICH, 2014-01-18
@KOLANICH

Not difficult.
Decide first on modules and interfaces.
Take out signatures and types in .h files, implementation - in cpp files, in which the necessary h file is included.
it's easy with dlls - you only need the declspec (dllexport) prefix for exported ones and declspec (dllimport) for imported ones, but there is a macro that is replaced by an automaton

D
Dmitry, 2014-01-19
@peleron

It makes sense to move the code into a separate dll only if you need to run several instances of your program, or you can put some specific code into the dll, which depends on the type of the Operating System or other factors (for example, creating a test library and a full-fledged one)
About splitting one file into a few - here @KOLANICH correctly described. I will add that it is advisable not to place anything in the header files (.h) except for the definition of types and classes, as well as descriptions of function signatures: i.e. there should be no global variables, no function implementation bodies, in general, no logic.
And also try to include header files less into each other - then you will stumble upon a rake with the compilation order.

K
Kerman, 2014-01-19
@Kerman

You need refactoring. What you described suggests that your code is far from perfect.
You need to apply extract method first , then extract class or similar methods . After that, it will be possible to calmly separate the classes into separate files.

D
Dmitry, 2014-01-19
@EvilsInterrupt

And why do you need to split into several DLLs? Maybe just re-arrange the code, i.e. refactor and leave everything as it is within the same DLL?
I would advise you to “live” within one DLL and refactor for the time being, then the external program that uses your DLL will serve as a good test bench and it will be much easier to conduct verification testing after refactoring! The second stage, if you still decide to split into several DLLs, it will be much easier for you, because. understandable code and it is tested!
Breaks down by thinking and asking yourself questions.
Each module must answer in the affirmative to the question "Does it really solve only one problem?". At the same time, it is necessary to understand not the primitive tasks "reading from a file" or "calculating entropy", "one task" means one point taken from the level of abstraction.
Example:
Level 1: Reading settings
Sub-level 1: Forming the file name with settings
Sub-level 2: Opening and reading from a file with settings Sub
-level 3: Setting the global object configurator according to the settings read
, etc. etc.
In any case, there are no ideal partitioning methods! No one will teach you how to program, this is an iterative process, today is better than yesterday, and tomorrow will be even better than today ;)

F
Fat Lorrie, 2014-01-20
@Free_ze

First you need to understand the idea of ​​dll. They need to place ready-made self-sufficient components that make sense outside the narrow specifics of one project or to support modularity (plugins, for example). Roughly speaking, you need to think, is there any great sense in this? If you just want to logically separate the program code, then separation at the source code level will be enough for this. The rest is said above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question