Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organising code in c++ [closed]

Tags:

c++

I just moved from java to c++. I was little confused about organising the source code.

In Java we have packages to organize between each module .

What are some common ways to organize the c++ source code?

like image 981
Kathick Avatar asked Mar 04 '13 13:03

Kathick


People also ask

What are the two main ways a file can be organized in C?

Reading from file (fscanf or fgets) Writing to a file (fprintf or fputs)

How do C codes work?

c is called the source file which keeps the code of the program. Now, when we compile the file, the C compiler looks for errors. If the C compiler reports no error, then it stores the file as a . obj file of the same name, called the object file.

What is good style in C?

C Style Guidelines. Meaningful names for variables, constants and functions. Do not use camelcase; use underscores for multi-word variables. For example, use hot_water_temperature instead of hotWaterTemperature.


1 Answers

If you look at large open source projects in C or C++, you will find a large variety of ways to organise the source. However, a couple of common scenarios is to use a directory structure of module (or component) with src and include within each module. Alternatively, you can have it the other way around, and use src\module and include\module.

If you have test-code, that often goes into a module\test or test\module.

One of the interesting challenges with larger C++ projects is "where to put the header files that are shared between components". Sometimes there is a include\public (or public\include), sometimes they are with their respective component.

It can be quite useful to separate out the parts of a component that are "available for others to use" vs. those components that are only used internally - because something internal can be easily changed with no or minimal effect on other components, where something that is "visible" outside of the component will need more care - what happens if a component is published as a DLL and you update the DLL without recompiling the source in the parts that use the component [if you have third party clients that don't have your source code (or at least aren't likely to build it regularly), you don't necessarily want to force them to rebuild their projects every time you make some change.

For large projects, namespaces are used to prevent nameclashes.

like image 110
Mats Petersson Avatar answered Sep 20 '22 19:09

Mats Petersson