How to split a C program in files and enable it?

I organized my program by breaking each entity in its own file. Something like that.

main.c

 #include "student.h" #include "subject.h" #include "classroom.h" #define PI 3.14 int sum(int a, int b); 

student.h

 typedef struct st student; 

student.c

 #include "student.h" 

subject.h

 typedef struct sb subject; 

subject.c

 #include "subject.h" 

classroom.h

 typedef struct cr classroom; 

classroom.c

 #include "classroom.h" 

My problem: in the classroom, and I need a disciple of the subject. How to enable this? should i include this inside classroom.h or classroom.c ?

 #include "student.h" #include "subject.h" 

Secondly, I have things on main.c that are used by everyone like sum () and PI

How is this correct, including the implementation in the header or including the header in the implementation file? and should I include header or implementation files?

If I drop everything on one file, it compiles just fine, but I do not do it right, it does not compile.

+4
source share
4 answers

Thus, organizing "xh" and "xc" (or "x.cpp") is a fairly standard way of doing things. But some things do not fit there, and sometimes you need "constants.h" or some other name for things like PI and SpeedOfLight .

Something like sum fit perfectly into "utils.h" - do you have enough to make it "utils.c"

Your .c file should contain all the header files that it needs (but no more).

For instance:

 #ifndef X_H__ #define X_H__ ... all the contents of xh goes here ... #endif 

And the header files should include all the things they need about. For example, if "xh" needs "ah" and "bh", there should be #include "ah" and #include "bh" in "xh #include "bh" , so you do not need to remember this:

If I include "xh", I have to put "ah" and "bh" before it.

At the same time, do not add more inclusions than you really need ...

+2
source

First of all. It is important to know about .h (header) files. They should have the following.

 // In the top of the file #ifndef NAME_OF_FILE_H #define NAME_OF_FILE_H // Your header code goes here // In the end of the file #endif 

Why put this? If you included your header file, say header.h , in several other files, file1.c , file2.c , you would basically repeat the code, that is, the code in header.h will be placed in both files during the compilation process.

With these instructions for the pre-processor, make sure that the header.h code will exist only once in the program.

Now. Where do you post #includes? Well, I suppose that student.h subject.h file and declare the things that are implemented in student.c and subject.c. Therefore, if the classroom.h file uses the things declared in the previous two headers, you need to put #include "student.h" and #include "subject.h" in classroom.h .

If only classroom.c also uses the things declared in the headers, put them only here, but not in classroom.h .

Finally, if both files use things declared in the headers, put #include "student.h" and #include "subject.h" in both files.

Basically you put in files that require a specific (but not implemented) resource in the header. With a heading surrounded by the above code, you can put it in many files and never repeat the code during the compilation process.

About sum() and PI . Same. Create a title with the code above and include it where necessary.

+4
source

Your question is mainly about style.
At best, people can give you a general opinion.

I think that each file should have a specific task or it is good to define a specific object and related operations.
"Work" should be described in a simple way:

  • A file for general purposes (simple) routines and / or macros and / or constants.
  • File for the string descriptor.
  • File for integer arithmetic.
  • File for input / output operations.
  • File for user interface and / or interaction.
  • etc.

The more you can separate the different types of tasks, the better your headings will be organized.

  • If in doubt, just ask yourself if these or other functions can be applied to other programs that are different in nature for your current project. If you have a list of macros / functions / data that you are likely to use in a variety of programs, regardless of the set of others, then it is very likely that the former should be grouped in the same header file.

"Object" should be described as a rule struct ,
and specific and well-understood โ€œoperationsโ€ (that is, functions) that act explicitly on this structure .

Finally, you can write one or two core files to assemble and link all the files you need.

In general, I intend to keep the main.c file as short as possible,
as the director of the orchestra overseeing the rest of the program.

By writing clear and pleasant comments in each file,
The documentation allows you to process a project with multiple files distributed anywhere.

You must explain:

  • What is the purpose of the file,
  • what data and functions exist
  • how to use functions
  • what results can be expected
  • which is the meaning of these results,
  • etc.

If you try to do this exercise by explaining to someone else what your header file should do and how, you will quickly see if it makes sense.

+2
source

You must include the header file in the implementation file.

Generally, you should include as few header files as possible in the header file. Include only the headers you need for types, etc. In the implementation file, you can include all the headers needed for the functions that you have in your header file.

So, in your case, it depends on what you need the student and the subject for. If you only need it in the implementation file, you added it there, and if you need it in the header, you can include it there, and it will be included in the implementation file if you include the header corresponding to the implementation file.

0
source

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


All Articles