Multiple c files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vishal

    #1

    Multiple c files

    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(includ ed 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
  • Mark A. Odell

    #2
    Re: Multiple c files

    vishalb@gmail.c om (vishal) wrote in
    news:2a254f80.0 407071216.4bff7 ee1@posting.goo gle.com:
    [color=blue]
    > 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.[/color]

    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.

    --
    - Mark ->
    --

    Comment

    • Emmanuel Delahaye

      #3
      Re: Multiple c files

      In 'comp.lang.c', vishalb@gmail.c om (vishal) wrote:
      [color=blue]
      > 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(includ ed 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.[/color]

      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.

      --
      -ed- get my email here: http://marreduspam.com/ad672570
      The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
      C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
      FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

      Comment

      • Mabden

        #4
        Re: Multiple c files

        "vishal" <vishalb@gmail. com> wrote in message
        news:2a254f80.0 407071216.4bff7 ee1@posting.goo gle.com...[color=blue]
        > 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(includ ed 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
        >[/color]

        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"

        --
        Mabden


        Comment

        • Malcolm

          #5
          Re: Multiple c files


          "vishal" <vishalb@gmail. com> wrote in message news:[color=blue]
          >
          > 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.
          >[/color]
          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.


          Comment

          • jacob navia

            #6
            Re: Multiple c files


            "vishal" <vishalb@gmail. com> a écrit dans le message de
            news:2a254f80.0 407071216.4bff7 ee1@posting.goo gle.com...[color=blue]
            > 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(includ ed 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[/color]

            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.







            Comment

            • jacob navia

              #7
              Re: Multiple c files


              "Emmanuel Delahaye" <emdelYOURBRA@n oos.fr> a écrit dans le message de
              news:Xns951FE58 BB127Chsnoserve rnet@212.27.42. 66...[color=blue]
              > In 'comp.lang.c', vishalb@gmail.c om (vishal) wrote:
              >[color=green]
              > > 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(includ ed 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.[/color]
              >
              > I guess you can do that with any decent text editor... What exactly is[/color]
              your[color=blue]
              > point ? You also could automate the operation with some lines of C.[/color]

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


              Comment

              • jacob navia

                #8
                Re: Multiple c files


                "Mabden" <mabden@sbc_glo bal.net> a écrit dans le message de
                news:SrZGc.7653 $UD7.31@newssvr 25.news.prodigy .com...
                [snip][color=blue]
                > Microsoft has a couple of switches you can use like this: "cl /C /EP /u
                > prog.c > combo.c"[/color]

                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.


                Comment

                • Darrell Grainger

                  #9
                  Re: Multiple c files

                  On Wed, 7 Jul 2004, Mark A. Odell wrote:
                  [color=blue]
                  > vishalb@gmail.c om (vishal) wrote in
                  > news:2a254f80.0 407071216.4bff7 ee1@posting.goo gle.com:
                  >[color=green]
                  > > 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.[/color]
                  >
                  > 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.[/color]

                  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.

                  --
                  Send e-mail to: darrell at cs dot toronto dot edu
                  Don't send e-mail to vice.president@ whitehouse.gov

                  Comment

                  • vishal

                    #10
                    Re: Multiple c files

                    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.

                    "jacob navia" <jacob@jacob.re mcomp.fr> wrote in message news:<cci2hl$tg t$1@news-reader4.wanadoo .fr>...[color=blue]
                    > "vishal" <vishalb@gmail. com> a écrit dans le message de
                    > news:2a254f80.0 407071216.4bff7 ee1@posting.goo gle.com...[color=green]
                    > > 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(includ ed 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[/color]
                    >
                    > 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.[/color]

                    Comment

                    • Mark A. Odell

                      #11
                      Re: Multiple c files

                      vishalb@gmail.c om (vishal) wrote in
                      news:2a254f80.0 407080911.6a45b d22@posting.goo gle.com:
                      [color=blue]
                      > It seems that nobody has got my question correctly.[/color]

                      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.
                      [color=blue]
                      > 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.[/color]

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

                      --
                      - Mark ->
                      --

                      Comment

                      • Minti

                        #12
                        Re: Multiple c files

                        vishalb@gmail.c om (vishal) wrote in message news:<2a254f80. 0407071216.4bff 7ee1@posting.go ogle.com>...[color=blue]
                        > 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(includ ed 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[/color]

                        <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

                        --
                        Imanpreet Singh Arora
                        isingh AT acm DOT org

                        Comment

                        • Pete Newman

                          #13
                          Re: Multiple c files

                          vishal <vishalb@gmail. com> wrote:[color=blue]
                          > 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[/color]

                          I get the feeling that you just ``switched'' from an IDE to a compiler.
                          Every compiler I've ever used compiles C files individually.
                          [color=blue]
                          > 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.[/color]

                          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.


                          --
                          Peter Newman (panewman@uiuc. edu)

                          Comment

                          • Emmanuel Delahaye

                            #14
                            Re: Multiple c files

                            In 'comp.lang.c', vishalb@gmail.c om (vishal) wrote:
                            [color=blue]
                            > 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.[/color]

                            What the heck is that tool? Some interpeter?
                            [color=blue]
                            > 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.[/color]

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

                            --
                            -ed- get my email here: http://marreduspam.com/ad672570
                            The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                            C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
                            FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

                            Comment

                            • Keith Thompson

                              #15
                              Re: Multiple c files

                              vishalb@gmail.c om (vishal) writes:[color=blue]
                              > 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.[/color]

                              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.)

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...