Multiple c files

V

vishal

Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Example if I have following files:
first.c
second.c
second.h(included in first.c)

I want a tool that would combine all the contents of these files in
one file

combined.c

Now if I compile combined.c I should get same result as original

Hope I am clear.
Thanks in advance
 
M

Mark A. Odell

(e-mail address removed) (vishal) wrote in

Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Yes, but this isn't really a question about the C language. You could use
things like 'cat' and the C pre-processor I suppose.
 
E

Emmanuel Delahaye

In said:
Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Example if I have following files:
first.c
second.c
second.h(included in first.c)

I want a tool that would combine all the contents of these files in
one file

combined.c

Now if I compile combined.c I should get same result as original

Hope I am clear.

I guess you can do that with any decent text editor... What exactly is your
point ? You also could automate the operation with some lines of C.
 
M

Mabden

vishal said:
Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Example if I have following files:
first.c
second.c
second.h(included in first.c)

I want a tool that would combine all the contents of these files in
one file

combined.c

Now if I compile combined.c I should get same result as original

You can usually output the intermediate file from the pre-processor. The
problem here is all your macros and #defines are parsed out, something like
"#define ESC 27" will be changed to 27 everywhere you had ESC in the code.

Microsoft has a couple of switches you can use like this: "cl /C /EP /u
prog.c > combo.c"
 
M

Malcolm

vishal said:
Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.
The problem is the "static" keyword. If you have static variables or
functions in the files then the tool will have to name-mangle to avoid a
clash.
 
J

jacob navia

vishal said:
Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Example if I have following files:
first.c
second.c
second.h(included in first.c)

I want a tool that would combine all the contents of these files in
one file

combined.c

Now if I compile combined.c I should get same result as original

Hope I am clear.
Thanks in advance

Merging two modules is a delicate operation. The crux of the matter is to
see
if there isn't any name-clashes, meaning if common names are used in the
same way.

1: static functions in the two modules could have the same name.
2: Macros could have the same name but different value/expansion.
3: One file could have a set of
#ifndef SOMESYMBOL
#endif
based in some symbol *not* being defined. If the other module defines
that
symbol, code could get included that it shouldn't.

Most of the time the merger is just adding the two source files, but watch
for the above problems. A simple way to check for those is to add
file a.c THEN file b.c. Compile, save the object file. Then, put first b.c
and THEN a.c. Compile and compare the object files.
They should be the same.
 
J

jacob navia

Emmanuel Delahaye said:
I guess you can do that with any decent text editor... What exactly is your
point ? You also could automate the operation with some lines of C.

This is surely not that simple. See my other post in this thread.
Just a word: "static". Doesn't that ring bells somewhere?
 
J

jacob navia

"Mabden" <mabden@sbc_global.net> a écrit dans le message de
[snip]
Microsoft has a couple of switches you can use like this: "cl /C /EP /u
prog.c > combo.c"

EP=preprocess to stdout, no #line, /u=remove all predefined macros
/C=don't strip comments

This will not cut it. The problem is "static", and other subtler
difficulties.
See my other post in this thread.
 
D

Darrell Grainger

(e-mail address removed) (vishal) wrote in


Yes, but this isn't really a question about the C language. You could use
things like 'cat' and the C pre-processor I suppose.

Don't forget to deal with things like static global variables. They are
only visible to the translation unit. It is always possible that two
files contain a variable with the same name. Just concatenating the files
together is not guaranteed to work.
 
V

vishal

It seems that nobody has got my question correctly.
Let me elaborate.
We have a project package developed from many years that has about 200
files ( c files and header files ). Now we have a tool ( not developed
by us--only executable available with us ) that does take only 1 C
file as input at a time. Now that the whole code is distributed across
files I wanted one single "C" file with all code in it. As some of you
suggested cat or other editors, If I do it manually with these many
lines of code, the order of file, static functions, name clashes
everything is to be taken care off. So I raised a question in this
group asking for a solution.
Its ok for me if the tool does all the pre-processing. But I want a
single "C" file at the end which is as good as the whole package.
 
M

Mark A. Odell

(e-mail address removed) (vishal) wrote in

It seems that nobody has got my question correctly.

We understood. I think you are missing the point. The C language, the
topic of this newsgroup, does not specify how to do this. If there is such
a weird tool out there I suggest you employ google for help.
Let me elaborate.
We have a project package developed from many years that has about 200
files ( c files and header files ). Now we have a tool ( not developed
by us--only executable available with us ) that does take only 1 C
file as input at a time.

Why you would *ever* write a tool with such an awful limitation is beyond
reason.
 
M

Minti

Is it possible using any tool to get a single C file containing a
combined code of multiple C files. The single file should include the
contents of the header file.

Example if I have following files:
first.c
second.c
second.h(included in first.c)

I want a tool that would combine all the contents of these files in
one file

combined.c

Now if I compile combined.c I should get same result as original

Hope I am clear.
Thanks in advance

<OT>
Hint: On many platforms you would be having a utility program
like @type@ or @cat@ to output the contents of the input stream to an
output stream. All you need to do is redirect them.
</OT>

HTH
 
P

Pete Newman

vishal said:
It seems that nobody has got my question correctly.
Let me elaborate.
We have a project package developed from many years that has about 200
files ( c files and header files ). Now we have a tool ( not developed
by us--only executable available with us ) that does take only 1 C

I get the feeling that you just ``switched'' from an IDE to a compiler.
Every compiler I've ever used compiles C files individually.
file as input at a time. Now that the whole code is distributed across
files I wanted one single "C" file with all code in it. As some of you
suggested cat or other editors, If I do it manually with these many
lines of code, the order of file, static functions, name clashes
everything is to be taken care off. So I raised a question in this
group asking for a solution.
Its ok for me if the tool does all the pre-processing. But I want a
single "C" file at the end which is as good as the whole package.

I strongly doubt that you've come up with a unique coding structure that
cannot be handled by conventional means. This magical solution you're
looking for sounds a lot like a linker to me. Perhaps you should try
using it to combine your C files *after* you've individually compiled
them into object files.
 
E

Emmanuel Delahaye

In said:
We have a project package developed from many years that has about 200
files ( c files and header files ). Now we have a tool ( not developed
by us--only executable available with us ) that does take only 1 C
file as input at a time.

What the heck is that tool? Some interpeter?
Now that the whole code is distributed across
files I wanted one single "C" file with all code in it. As some of you
suggested cat or other editors, If I do it manually with these many
lines of code, the order of file, static functions, name clashes
everything is to be taken care off. So I raised a question in this
group asking for a solution.
Its ok for me if the tool does all the pre-processing. But I want a
single "C" file at the end which is as good as the whole package.

And how is this a good idea It breaks all known design rules about
modularity, testability, functionnal entities, code organization etc.
 
K

Keith Thompson

It seems that nobody has got my question correctly.
Let me elaborate.
We have a project package developed from many years that has about 200
files ( c files and header files ). Now we have a tool ( not developed
by us--only executable available with us ) that does take only 1 C
file as input at a time. Now that the whole code is distributed across
files I wanted one single "C" file with all code in it. As some of you
suggested cat or other editors, If I do it manually with these many
lines of code, the order of file, static functions, name clashes
everything is to be taken care off. So I raised a question in this
group asking for a solution.
Its ok for me if the tool does all the pre-processing. But I want a
single "C" file at the end which is as good as the whole package.

Please don't top-post. Your new text should follow any quoted text
from previous articles, as I've done here. Backwards read to
difficult it's.

The first thing I'd do is complain to whoever provided this tool
you're trying to use. C programs of any reasonable size are not, and
should not be, implemented in a single source file. A tool that
requires this is badly broken.

What is the tool supposed to do? If we knew that, we might have a
better idea of how to work around its limitations.

Automatically combining multiple C source files into an equivalent
single file is probably possible, but it's not easy. For example, two
source files in the same program might define static variables or
functions with the same name; joining them into a single file would
require changing at least one of the names and all references to it.
(Detecting all references is non-trivial; there could be declarations
with the same name in nested scopes that shouldn't be changed.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top