Menu
Is free
check in
the main  /  BY / Source file C. Online including one source file C in another? What can be in the header file

Source file C. Online including one source file C in another? What can be in the header file

Source files

The text of the SI program can be divided into several source files. The source file is a text file that contains either the entire program or part of it. When compiling the source program, each of the components of its source files must be compiled separately, and then associated with other files by the linker. Separate source files can be combined into one source file, compiled as a single integer, through the Preprocessor Directive #Include..

The source file may contain any holistic combination of directives, indicating the compiler, ads and definitions. Under integrity it is understood that such objects as definitions of functions, data structures or a set of conventional compilation directives must be fully located in one file, i.e. they cannot start in one file, but continue in the other.

The source file does not have to contain the executed operators. Sometimes it is convenient to place variable definitions in one file, and in other files to use these variables by ad. In this case, the definitions of variables become easily accessible to search and modify. Of the same considerations, named constants and macro definitions are usually collected in separate files and include them through a preprocessor directive. #Include. In those source files in which they are required.

Note Compiler usually apply only for individual sections of the source file. The specific actions of the compiler specified by the instructions are determined by the specific implementation of the SI language compiler.

In the following example, the source program consists of two source files. Functions main. and max Presented in separate files. Function main. uses the function max in the process of its execution.

/ * Source File 1 - Main * /

eXTERN INT MAX (INT, INT); / * feature function * /

main () / * function definition * /

int w \u003d one, x \u003d two, y \u003d three;

/ * Source File 2 - Max * /

iNT MAX (A, B) / * Function Definition * /

In the first source file function max declared, but not defined. Such an ad function is called preliminary; It allows the compiler to control the appeal to the function before it is defined. Definition of function main. Contains function calls max.

Rows starting with the symbol # are the directives of the preprocessor. Directives indicate the preprocessor on the need to replace in the first source file of the One, Two, Three identifiers to the corresponding values. The directive area does not apply to the second source file.



Depending on your assembly environment (you do not specify) you can detect that it works exactly as you want.

However, there are many environments (both IDE and many manually machined makefile), which are waiting for compilation * .c - if this happens, you are likely to come across the linker errors due to duplication of characters.

As a rule, this practice should be avoided.

If you have to need to turn on the source (and usually it should be avoided), use another file for the file.

Enabling the C file to another file is legal, but not appropriate if you definitely do not know why you do it and what are trying to achieve.
I am almost confident that if you publish a reason here, according to which your question will be communicated to the community, you will find another suitable way to achieve your goal (pay attention to "almost", since it is possible that this decision, given the context).

By the way, I missed the second part of the question. If the C file is enabled in another file and at the same time included in the project, you will probably encounter the problem of duplicating characters, why object binding, that is, the same function will be defined twice (if only they are not static).

Language C does not prohibit this type of #include, but the resulting translation unit must still be valid C.

I do not know which program you use S.Prj file. If you use something like "make" or Visual Studio or something else, just make sure that you install its list of files that must be compiled without that cannot be compiled independently.

You can use the GCC compiler in Linux to bind two files with one output. Suppose you have two C-files, one of which is "main.c", and the other is "support.c.". Thus, the command for connecting these two

GCC Main.c support.c -o main.out

These two files will be associated with one Main.out output. To start the output command will be

./main.out.

If you use the Main.C function, which is declared in the support.c file, then you must declare it mostly also using the External Storage class.

Used correctly, it can be a useful method.

Suppose you have a complicated critical subsystem with a rather small public interface and a lot of unrealized implementation code. The code works up to several thousand rows, hundreds of private functions and quite a bit of private data. If you work with nontrivial built-in systems, you probably often cope with this situation.

Your decision is likely to be multi-level, modular and unleashed, and these aspects can be conveniently represented and reinforced by encoding various parts of the subsystem in different files.

With C, you can lose a lot by making it. Almost all tools provide decent optimization for a single compilation unit, but very pessimistically refer to something declared extern.

If you put everything in one source module C, you will get -

    Improvement of productivity and size code - In many cases, the function calls will be built. Even without inlay, the compiler has opportunities for creating a more efficient code.

    Channel level data and functions are hidden.

    Avoiding the namespace pollution and its consequence - you can use less bulky names.

    Faster compilation and communication.

But you also get a wicked mess when it comes to editing this file, and you lose the implied modularity. This can be overcome by dividing the source code into several files and turning them into a single compilation unit.

However, you need to impose some agreements to cope with this. To some extent it will depend on your instrumental chain, but some general pointers -

    Place the open interface in a separate header file - you still have to do it.

    Have one main file.c, which includes all child files. It may also include the code for the open interface.

    Use the compiler protesters so that private headlines and source modules are not included with external compilation modules.

    All personal data and functions must be declared static.

    Maintain a conceptual difference between each.h.h files. It uses existing agreements. The difference is that you will have a lot of static ads in your headlines.

    If your tool chain does not impose any bases, you should not specify private implementation files as .C I.H. If you are using enabled guards, they will not generate code and will not enter any new names (as a result you may encounter some empty segments). A huge advantage is that other tools (for example, IDE) will process these files accordingly.

The file extension does not matter for most C compilers, so it will work.

However, depending on the settings of your file or project, the C file included can generate a separate object file. When binding, this can lead to double certain characters.

you must add a title similar to this.

#Include.

note: Both files must be placed in one place.

You can correctly enable files.c or.cpp to other source files. Depending on your IDE environment, you can usually prevent dual binding by viewing the properties of the source files that you want to enable, usually right-clicking on them and click on properties, and uncheck the box / check compilation / link / exclude from the assembly or any other option . may be. Or you cannot enable the file in the project itself, so the IDE environment does not even know that it exists, and will not try to compile it. And with make-files, you just just did not put the file into it for compilation and layout.

Edit: Sorry, I gave an answer instead of an answer to other answers :(



vote Online (8)

Enabling the C file to another file is legal, but not appropriate if you definitely do not know why you do it and what are trying to achieve.
I am almost confident that if you publish a reason here, according to which your question will be communicated to the community, you will find another suitable way to achieve your goal (pay attention to "almost", since it is possible that this decision, given the context).

By the way, I missed the second part of the question. If the C file is enabled in another file and at the same time included in the project, you will probably encounter the problem of duplicating characters, why object binding, that is, the same function will be defined twice (if only they are not static).

The file extension does not matter for most C compilers, so it will work.

However, depending on the settings of your file or project, the C file included can generate a separate object file. When binding, this can lead to double certain characters.

Depending on your assembly environment (you do not specify) you can detect that it works exactly as you want.

However, there are many environments (both IDE and many manually machined makefile), which are waiting for compilation * .c - if this happens, you are likely to come across the linker errors due to duplication of characters.

As a rule, this practice should be avoided.

If you have to need to turn on the source (and usually it should be avoided), use another file for the file.

You can use the GCC compiler in Linux to bind two files with one output. Suppose you have two C-files, one of which is "main.c", and the other is "support.c.". Thus, the command for connecting these two

GCC Main.c support.c -o main.out

These two files will be associated with one Main.out output. To start the output command will be

./main.out.

If you use the Main.C function, which is declared in the support.c file, then you must declare it mostly also using the External Storage class.

I thought I would share the situation when my team decided to enable files. Our architect mainly consists of modules that are unleashed through the message system. These message handlers are publicly available and cause many local static operating functions to perform their work. The problem arose when I was trying to get a coating for our single test cases, since the only way to implement this particular implementation code was indirectly through the general message interface. With some features of the worker in the knees in the stack it turned out to be a nightmare to ensure proper coating.

Enabling files.c gave us the opportunity to get to the screw in the car, we were interested in testing.

Language C does not prohibit this type of #include, but the resulting translation unit must still be valid C.

I do not know which program you use S.Prj file. If you use something like "make" or Visual Studio or something else, just make sure that you install its list of files that must be compiled without that cannot be compiled independently.

Used correctly, it can be a useful method.

Suppose you have a complicated critical subsystem with a rather small public interface and a lot of unrealized implementation code. The code works up to several thousand rows, hundreds of private functions and quite a bit of private data. If you work with nontrivial built-in systems, you probably often cope with this situation.

Your decision is likely to be multi-level, modular and unleashed, and these aspects can be conveniently represented and reinforced by encoding various parts of the subsystem in different files.

With C, you can lose a lot by making it. Almost all tools provide decent optimization for a single compilation unit, but very pessimistically refer to something declared extern.

If you put everything in one source module C, you will get -

    Improvement of productivity and size code - In many cases, the function calls will be built. Even without inlay, the compiler has opportunities for creating a more efficient code.

    Channel level data and functions are hidden.

    Avoiding the namespace pollution and its consequence - you can use less bulky names.

    Faster compilation and communication.

But you also get a wicked mess when it comes to editing this file, and you lose the implied modularity. This can be overcome by dividing the source code into several files and turning them into a single compilation unit.

However, you need to impose some agreements to cope with this. To some extent it will depend on your instrumental chain, but some general pointers -

    Place the open interface in a separate header file - you still have to do it.

    Have one main file.c, which includes all child files. It may also include the code for the open interface.

    Use the compiler protesters so that private headlines and source modules are not included with external compilation modules.

    All personal data and functions must be declared static.

    Maintain a conceptual difference between each.h.h files. It uses existing agreements. The difference is that you will have a lot of static ads in your headlines.

    If your tool chain does not impose any bases, you should not specify private implementation files as .C I.H. If you are using enabled guards, they will not generate code and will not enter any new names (as a result you may encounter some empty segments). A huge advantage is that other tools (for example, IDE) will process these files accordingly.

This is normal? Yes, he will compile

is it recommended? No - .c files are compiled into files.obj that are connected after compilation (linker) in the executable file (or library), so there is no need to include one file.c to another. Instead, you will most likely want to make a file.h, which lists the functions / variables available in another.c file, and enable file.h

This kind of question I recently asked one colleague, starting to program in Si. And I thought it was a good reason to share my understanding of this issue. Because even experienced programmers do not always have similar points of view on this.

Partly this is a matter of taste, so who is interested as I do, welcome to Cat.

Despite the fact that "the whole truth" about H-files is contained in the appropriate section of the description of the GCC preprocessor, allow itself some explanations and illustrations.

So, if a literally, the header file (H-file) is a file containing a declaration and macro definition intended for use in several source files (C-files). We illustrate it.

It is easy to notice that functions 1 and 2, as well as the macro 2, are mentioned in both files. And because, the inclusion of header files leads to the same results as copying the contents to each SI file, we can do the following:

In this way, we simply allocated a common part of two files and placed it in the header file.
But is the header file with the interface in this case?

  • If we need to use the functionality that functions 1 and 2 are implemented somewhere else, then yes
  • If the macro 2 is intended only for use in unit1.c and unit2.c files, then it does not place in the interface file
Moreover, do we really need to have two SI files to implement the interface defined in the header file? Or is one?
The answer to this question depends on the details of the implementation of interface functions and from their implementation site. For example, if you make diagrams more detailed, you can submit an option when the interface functions are implemented in different files:


This embodiment leads to high codes, low tests and the complexity of reuse of such modules.
In order to not have such difficulties, I always consider a C-file and a header file as one module. In which,
  • the header file contains only those declarations of functions, types, macros that are part of the interface of this module.
  • The C-file, in turn, must contain the implementation of all functions declared in the H-file, as well as private types, macros and functions that are needed to implement the interface.
Thus, if I had the opportunity to implement the code that the diagram was applied above, I would try to achieve the following (End of _C and _H in file names Added due to the impossibility of using the point in the tool that I used to create diagrams):


The diagram shows that in fact we are dealing with two independent modules, each of which has its own interface in the form of a header file. This makes it possible to use only that interface that is really necessary in this particular case. Moreover, these modules can be tested independently of each other.
The reader, probably, noticed that the macro 2 from the header file returned again as a copy in both SI files. Of course, it is not very convenient to support. But also to make this macro part of the interface is not correct.
In such cases, I prefer to make a separate header file containing types and macros needed by several SI files.

I hope I managed to identify those entities that need to be placed in header files. As well as, show the difference between interfaces and files containing declarations and macros needed by several SI files.

Thank you for your attention to the material.

support.microsoft.

When changing the source files in Visual C ++ and save them, the strings must complete the characters "CR / LF" [the carriage return symbol, row translation] combination. In UNIX systems, strings are completed using "LF". Thus, when viewing the files that have been changed in the Windows group in UNIX systems, the set "^ M" characters can appear in lines. This happens only when using the editor does not know how to interpret the Windows file. Visual C ++ You can open files, in strings ending with the creation of UNIX LF. If you change this file and save it from Visual C ++, and then it is saved in Windows format (you will see CR / LF and not LF, which was previously in the system).

This article describes the procedures for saving the changed file created on the Windows platform in a format that can be used in UNIX systems.

NOTE: Visual C ++. Net IDE contains functions available to save the file in UNIX format. In an integrated development environment, save the file using Save as..., select Save from the drop-down list Save with encoding ...and click the Thrn button Yes. Select from the drop-down list the row encoding UNIX (LF)and then click OK.

You can use the following steps to create a Win32 console application project that converts a file containing "CR / LF" to complete the string for "LF":

  1. To create a new Win32 empty project with the name DOS2Unix using the Win32 Console Applications.
  2. Of File Menu, click Newand then click Files Tab.
  3. Choose Source file C / C ++ And enter the name of the new dos2unix.cpp file.
  4. Insert the following code in dos2unix.cpp:

    #Include. #Include. #Include. Using Namespace STD; Int Main (Int Argc, Char * ArgV) (if (argc! \u003d 2) (Cout<< "Please specify: dos2unix filename" << endl; return 0; } char ch; char temp="\0"; //Open the file for reading in binarymode. ifstream fp_read(argv, ios_base::in \ / ios_base::binary); sprintf(temp, "%s.temp", argv); //Create a temporary file for writing in the binary mode. This //file will be created in the same directory as the input file. ofstream fp_write(temp, ios_base::out \ / ios_base::trunc \ / ios_base::binary); while(fp_read.eof() != true) { fp_read.get(ch); //Check for CR (carriage return) if((int)ch == 0x0D) continue; if (!fp_read.eof())fp_write.put(ch); } fp_read.close(); fp_write.close(); //Delete the existing input file. remove(argv); //Rename the temporary file to the input file. rename(temp, argv); //Delete the temporary file. remove(temp); return 0; }

  5. Of Building Menu, click Creating dos2unix.exe. To create an exe file.

It may be necessary to check this exe file to see if it works correctly. To do this, open the file in the Visual C ++ binary editor when selecting Open in a group File Menu by selecting dos2unix.ex, setting Open as KOM Binaryand then clicking Open. For example, if the file contains "HellocrlfWorld", the binary file data (hexadecimal) will look like this:

48 65 6 C 6 C 6F 0 D 0A 57 6F 72 6 C 64

This is equivalent to:

Hello
Peace

In the command prompt, execute the dos2unix.exe command . Next, open the file in the Visual C ++ binary editor. You will see that 0x0d s is deleted. Until you change the file and save it in Visual C ++ 0x0d s will not appear.

This can be used in combination with Visual C ++ automation model automate the entire process. Simple Microsoft Visual Basic Macro Scenario can be written to call this fund, but first need to add Service The menu is as follows:

  1. Of Service Menu, click Settingand then click Service Tab.
  2. Specify the name, such as DOS2Unix, and specify the full path to the dos2unix.exe file in Team Editing field.
  3. Set for argument, $ (filename) $ (FileExt).
  4. Specify the $ (WKSPDIR) source directory (specify your own path).

To check the program, open the file in Visual C ++ editor, and then from Service Launch menu Dos2unix means. You will see that all its CR characters were deleted in the editor.

If you need to automate this process to process in such a way that each time you save the open file in the Visual C ++ editor, the DOS2Unix.exe tool is called to remove 0x0d s, and then use the following VBScript macro:

"This Document IS Fired Every Time The Document IS Saved in The VC ++ Editor. Sub Application_documentSave (TheDocument)" This Will Call The User Tool In The Tools Menu. "Change The Number Depending Upon What You Have. By Default You Only" Have 6 Tools Under the Tools Menu, So The Dos2unix Tool Will Be The 7th. Executecommand "USERTOOL7" END SUB

This VBScript code will work only if you have files open to the Visual C ++ editor. This is the only way to call an EXE file from a VBScript macro (VBScript macro cannot be transferred to the parameters). You can write in instead and it will be more flexible. Call the "dos2unix.exe" tool from the add-in without having to add it to Service menu.

In Visual C ++ using the macros provided by VBScript:

  1. Open an existing file with extension.dsm or create one.
  2. Insert the code previously shown in the file.
  3. In Visual C ++, follow these steps.
    1. Of Service Menu, click Setting.
    2. Press the button Macro files and add-ons Tab.
    3. Press the button Overview Download file.dsm containing macro. Once in the file.dsm was chosen in Overviewdialog box, file will appear in Supplements and macros List of files using the selected checkbox next to it.
    4. Press the button Close to continue.

Now, if you open the file in the Visual C ++ editor and save from the file File The menu called macro and all 0x0d s will be deleted from the open file. As it affects any file, save from now on and apply to any project to open in the future, make sure to turn off the macro from Service Menu with help Setting (Uncheck the checkbox next to the macro).