It is possible to do. You may include a C source in another C source, just as you would include a C header file inside a C source file.
use "system" a in-built function. Say you want to invoke another C program with name abc.exe. system("abc.exe"); // provide absolute path if exe place at other directory. In Linux system apart of system() function call, you can use following exec() library functions to execute new program.
The reason for having two files per "module" is, that you can distinguish between public parts in the . h file and private parts in the . c file. This allows the developer to restrict publications to what is needed.
Used properly, this can be a useful technique.
Say you have a complex, performance critical subsystem with a fairly small public interface and a lot of non-reusable implementation code. The code runs to several thousand lines, a hundred or so private functions and quite a bit of private data. If you work with non-trivial embedded systems, you probably deal with this situation frequently enough.
Your solution will probably be layered, modular and decoupled and these aspects can be usefully represented and reinforced by coding different parts of the subsystem in different files.
With C, you can lose a lot by doing this. Almost all toolchains provide decent optimisation for a single compilation unit, but are very pessimistic about anything declared extern.
If you put everything into one C source module, you get -
Performance & code size improvements - function calls will be inlined in many cases. Even without inlining, the compiler has opportunities to produce more efficient code.
Link level data & function hiding.
Avoidance of namespace pollution and its corollary - you can use less unwieldy names.
Faster compilation & linkage.
But you also get an unholy mess when it comes to editing this file and you lose the implied modularity. This can be overcome by splitting the source into several files and including these to produce a single compilation unit.
You need to impose some conventions to manage this properly though. These will depend on your toolchain to some extent, but some general pointers are -
Put the public interface in a separate header file - you should be doing this anyway.
Have one main .c file that includes all the subsidiary .c files. This could also include the code for the public interface.
Use compiler guards to ensure that private headers and source modules are not included by external compilation units.
All private data & functions should be declared static.
Maintain the conceptual distinction between .c and .h files. This leverages existing conventions. The difference is that you will have a lot of static declarations in your headers.
If your toolchain doesn't impose any reason not to, name the private implementation files as .c and .h. If you use include guards, these will produce no code and introduce no new names (you may end up with some empty segments during linkage). The huge advantage is that other tools (e.g. IDEs) will treat these files appropriately.
is it ok? yes, it will compile
is it recommended? no - .c files compile to .obj files, which are linked together after compilation (by the linker) into the executable (or library), so there is no need to include one .c file in another. What you probably want to do instead is to make a .h file that lists the functions/variables available in the other .c file, and include the .h file
No.
Depending on your build environment (you don't specify), you may find that it works in exactly the way that you want.
However, there are many environments (both IDEs and a lot of hand crafted Makefiles) that expect to compile *.c - if that happens you will probably end up with linker errors due to duplicate symbols.
As a rule this practice should be avoided.
If you absolutely must #include source (and generally it should be avoided), use a different file suffix for the file.
I thought I'd share a situation where my team decided to include .c files. Our archicture largely consists of modules that are decoupled through a message system. These message handlers are public, and call many local static worker functions to do their work. The problem came about when trying to get coverage for our unit test cases, as the only way to exercise this private implementation code was indirectly through the public message interface. With some worker functions knee-deep in the stack, this turned out to be a nightmare to achieve proper coverage.
Including the .c files gave us a way to reach the cog in the machine we were interesting in testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With