By srujan


2008-10-24 07:03:00 8 Comments

Is it OK (or even recommended/good practice) to #include a .c file in another .c file?

10 comments

@sumitroy 2016-08-21 11:36:33

You can use the gcc compiler in linux to link two c file in one output. Suppose you have two c files one is 'main.c' and another is 'support.c'. So the command to link these two is

gcc main.c support.c -o main.out

By this two files will be linked to a single output main.out To run the output the command will be

./main.out

If you are using function in main.c which is declared in support.c file then you should declare it in main also using extern storage class.

@milad 2016-08-15 10:32:03

you should add header like this

#include <another.c>

note : both file should placed in same place

@MikeyPro 2014-07-10 01:02:28

You can properly include .C or .CPP files into other source files. Depending on your IDE, you can usually prevent double-linkage by looking at the source files properties you want to be included, usually by right clicking on it and clicking properties, and uncheck/check compile/link/exclude from build or whatever option it may be. Or you could not include the file in the project itself, thus the IDE wont even know it exists and wont try to compile it. And with makefiles you simply just wouldn't put the file in it for compiling and linking.

EDIT: Sorry I made it an answer instead of a reply onto other answers :(

@Matthew Alford 2010-10-05 18:45:59

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.

@forgot_my_OpenId 2009-07-10 12:31:32

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.

@u0b34a0f6ae 2010-07-10 09:59:37

+1 this is still the reality, while better compilers will make this method obsolete with time. GCC 4.5 with link-time optimization is a big step on the way.

@Joey Adams 2011-07-19 08:15:50

I've seen so many programs do this, and it gets on my nerves when I'm trying to reuse their code. Thanks for explaining why they do it, and suggesting the use of a guard (which often isn't done).

@mahesh 2017-02-23 21:23:55

In embedded development for C51, use of extern & multiple C files has caused nothing but headaches. I'm switching over to having one C file include all others to get back my time.

@Twonky 2018-05-05 15:51:16

For the records: GCC supports optimization across different translation units, see this SO thread and GCC manual section on link time optimization.

@Ilya 2008-10-24 07:28:21

Including C file into another file is legal, but not advisable thing to do, unless you know exactly why are you doing this and what are you trying to achieve.
I'm almost sure that if you will post here the reason that behind your question the community will find you another more appropriate way to achieve you goal (please note the "almost", since it is possible that this is the solution given the context).

By the way i missed the second part of the question. If C file is included to another file and in the same time included to the project you probably will end up with duplicate symbol problem why linking the objects, i.e same function will be defined twice (unless they all static).

@HS. 2008-10-24 07:28:17

The extension of the file does not matter to most C compilers, so it will work.

However, depending on your makefile or project settings the included c file might generate a separate object file. When linking that might lead to double defined symbols.

@Windows programmer 2008-10-24 07:15:14

The C language doesn't prohibit that kind of #include, but the resulting translation unit still has to be valid C.

I don't know what program you're using with a .prj file. If you're using something like "make" or Visual Studio or whatever, just make sure that you set its list of files to be compiled without the one that can't compile independently.

@Steven A. Lowe 2008-10-24 07:12:11

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

@Nick Meyer 2009-07-10 12:50:39

Also worth noting that even if it compiles, it might not link if the #included .c file is also compiled and the two object files are linked together -- you could end up with multiply defined symbols.

@stdout 2019-03-09 13:23:10

A small question. I've a header file declaring a struct + method and also the corresponding .c file to define them. If that method takes the struct as a parameter , how would I avoid including the .c file in another .c file where the main method is defined?

@Andrew Edgecombe 2008-10-24 07:10:10

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.

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] Include another HTML file in a HTML file

  • 2012-01-24 14:51:16
  • lolo
  • 1330352 View
  • 576 Score
  • 33 Answer
  • Tags:   html include

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

58 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

18 Answered Questions

[SOLVED] Where do the Python unit tests go?

16 Answered Questions

[SOLVED] "#include" a text file in a C program as a char[]

24 Answered Questions

[SOLVED] Difference between require, include, require_once and include_once?

27 Answered Questions

17 Answered Questions

[SOLVED] How do I use extern to share variables between source files?

13 Answered Questions

[SOLVED] How to include() all PHP files from a directory?

  • 2009-03-01 11:36:28
  • occhiso
  • 215219 View
  • 224 Score
  • 13 Answer
  • Tags:   php include

10 Answered Questions

[SOLVED] C/C++ include header file order

  • 2010-05-04 03:09:22
  • Anycorn
  • 98135 View
  • 259 Score
  • 10 Answer
  • Tags:   c++ c

Sponsored Content