Is it legal to include source file?

Discussion in 'C Programming' started by mr.polik@gmail.com, Jun 3, 2007.

  1. Guest

    Is it legal to do something like this:
    #include "x.c" in other source file?
    And is it good idea?
    Thank you in advance for your answers,
    Igor.
    , Jun 3, 2007
    #1
    1. Advertising

  2. said:

    > Is it legal to do something like this:
    > #include "x.c" in other source file?


    Yes.

    > And is it good idea?


    No.

    > Thank you in advance for your answers,
    > Igor.


    That'th all right, Igor. The uthual way to incorporate multiple
    thourtheth into one program ith to compile them theparately, and then
    link them together. For ecthample, if you are uthing the GNU compiler
    (which begth to be lithped, but I lack thuffithient thpittle), you
    might do thith:

    gcc -W -Wall -ansi -pedantic -c -o x.o x.c
    gcc -W -Wall -ansi -pedantic -c -o y.o y.c
    gcc -W -Wall -ansi -pedantic -c -o z.o z.c
    gcc -W -Wall -ansi -pedantic -o foo x.o y.o z.o

    Might I altho thuggetht that you invethtigate your implementathion'th
    third-party library fathilitieth?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 3, 2007
    #2
    1. Advertising

  3. On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    <> wrote:

    > said:
    >
    >> Is it legal to do something like this:
    >> #include "x.c" in other source file?

    >
    >Yes.
    >
    >> And is it good idea?

    >
    >No.
    >
    >> Thank you in advance for your answers,
    >> Igor.

    >
    >That'th all right, Igor. The uthual way to incorporate multiple


    What in the original post prompted you to be this snide? Any value in
    your response will be lost on all the participants who struggle
    through this group in a second language. Are you having a running
    battle with this poster in a different group?


    Remove del for email
    Barry Schwarz, Jun 3, 2007
    #3
  4. Thad Smith Guest

    Richard Heathfield wrote:
    > said:
    >
    >>Is it legal to do something like this:
    >>#include "x.c" in other source file?

    >
    > Yes.
    >
    >>And is it good idea?

    >
    > No.
    >
    > That'th all right, Igor. The uthual way to incorporate multiple
    > thourtheth into one program ith to compile them theparately, and then
    > link them together.


    Been to the dentist? ;-)

    There was a project several years ago in which I included a code file.
    I was writing serial I/O drivers for multiple ports, but using a single
    function and pointers/indexes to the data area associated with the port
    significantly increased the interrupt processing time. I wrote one
    procedure as a template, then had separate wrapper functions for each
    port which included the code file after setting a defined constant for
    the port value. For each driver the port was constant, so no run-time
    indexing or dereferencing was needed. The I/O ports were also directly
    addressed.

    Bottom line was that the code was defined in one place, and was
    instantiated in multiple places to optimize speed at the expense of code
    space. Come to think of it, that's similar to what happens with
    function-like macro usage, and inline functions, just a different way to
    invoke it.

    It's a technique, though, which I use only in special circumstances.

    --
    Thad
    Thad Smith, Jun 3, 2007
    #4
  5. Tim Prince Guest

    Thad Smith wrote:
    > Richard Heathfield wrote:
    >> said:
    >>
    >>> Is it legal to do something like this:
    >>> #include "x.c" in other source file?

    >>
    >> Yes.
    >>
    >>> And is it good idea?

    >>
    >> No.


    >
    > Bottom line was that the code was defined in one place, and was
    > instantiated in multiple places to optimize speed at the expense of code
    > space. Come to think of it, that's similar to what happens with
    > function-like macro usage, and inline functions, just a different way to
    > invoke it.
    >
    > It's a technique, though, which I use only in special circumstances.
    >

    I've seen it done where the same code is instantiated with multiple data
    types. As it was done that way by people more expert than the average
    of those whose code I inherit, I work with it, rather than pass judgment.
    Tim Prince, Jun 3, 2007
    #5
  6. Ben Pfaff Guest

    Thad Smith <> writes:

    > There was a project several years ago in which I included a code
    > file. I was writing serial I/O drivers for multiple ports, but using a
    > single function and pointers/indexes to the data area associated with
    > the port significantly increased the interrupt processing time. I
    > wrote one procedure as a template, then had separate wrapper functions
    > for each port which included the code file after setting a defined
    > constant for the port value. For each driver the port was constant,
    > so no run-time indexing or dereferencing was needed. The I/O ports
    > were also directly addressed.


    This technique sometimes makes sense, but I'd suggest that giving
    an included code file a .c extension is misleading. When I do
    something like this, I use a .inc or .def or other extension that
    is less likely to surprise programmers reading the code later.
    --
    "Welcome to the wonderful world of undefined behavior, where the demons
    are nasal and the DeathStation users are nervous." --Daniel Fox
    Ben Pfaff, Jun 3, 2007
    #6
  7. Barry Schwarz said:

    > On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    > <> wrote:
    >
    >> said:
    >>

    <snip>
    >>
    >>> Thank you in advance for your answers,
    >>> Igor.

    >>
    >>That'th all right, Igor. The uthual way to incorporate multiple

    >
    > What in the original post prompted you to be this snide?


    What maketh you think I'm being thnide? Have you never read any
    Dithcworld bookth?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 3, 2007
    #7
  8. On Sun, 03 Jun 2007 21:50:10 +0000, in comp.lang.c , Richard
    Heathfield <> wrote:

    >Barry Schwarz said:
    >
    >> On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    >> <> wrote:
    >>
    >>> said:
    >>>

    ><snip>
    >>>
    >>>> Thank you in advance for your answers,
    >>>> Igor.
    >>>
    >>>That'th all right, Igor. The uthual way to incorporate multiple

    >>
    >> What in the original post prompted you to be this snide?

    >
    >What maketh you think I'm being thnide? Have you never read any
    >Dithcworld bookth?


    I guess the problem is that your post could be construed as an
    insulting joke.

    Imagine if we insisted on comparing you to Richard the Third, hump and
    all, or invariably responded in Bronte-esque language and complained
    about how you'd treated Cathy?

    Igor is after all a real name as well as a running joke in Discworld.

    (and yes, I know you won't read this, you killfiled me because you
    thought I kept insulting you. Hmm, curiouser and curiouser..)
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jun 3, 2007
    #8
  9. Don Bruder Guest

    In article <>,
    Barry Schwarz <> wrote:

    > On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    > <> wrote:
    >
    > > said:
    > >
    > >> Is it legal to do something like this:
    > >> #include "x.c" in other source file?

    > >
    > >Yes.
    > >
    > >> And is it good idea?

    > >
    > >No.
    > >
    > >> Thank you in advance for your answers,
    > >> Igor.

    > >
    > >That'th all right, Igor. The uthual way to incorporate multiple

    >
    > What in the original post prompted you to be this snide?


    So I'm not alone?

    (I thought the same thing.)

    --
    Don Bruder - - If your "From:" address isn't on my whitelist,
    or the subject of the message doesn't contain the exact text "PopperAndShadow"
    somewhere, any message sent to this address will go in the garbage without my
    ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
    Don Bruder, Jun 3, 2007
    #9
  10. Don Bruder said:

    > In article <>,
    > Barry Schwarz <> wrote:
    >
    >> On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    >> <> wrote:
    >>
    >> > said:
    >> >

    <snip>
    >> >
    >> >> Thank you in advance for your answers,
    >> >> Igor.
    >> >
    >> >That'th all right, Igor. The uthual way to incorporate multiple

    >>
    >> What in the original post prompted you to be this snide?

    >
    > So I'm not alone?
    >
    > (I thought the same thing.)


    Then I apologise unreservedly to the OP. Silly joke gone wrong. Sorry.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 3, 2007
    #10
  11. In article <>,
    <> wrote:
    >Is it legal to do something like this:
    >#include "x.c" in other source file?


    Whether it will work or not is implementation defined. If my memory
    serves me correctly this evening, the preprocessor is only -required-
    to provide a token-to-filename mapping mechanism for the .h extension
    and for one directory.

    The C preprocessor is -allowed- to require the user to write the
    filename token in a manner quite different than the host operating system
    uses to name files.

    For example, there are operating systems that do not support lowercase
    letters, and do not support dots in filenames; the operating system
    might also happen to require a "drive letter" for each filename access
    (with no equivilent of a "current working directory".) The preprocessor
    could define (documenting it of course) that <foo.h> (for any foo)
    shall map to filename FOO (uppercaseing), file extension INC, drive Z,
    and could define that "foo.h" shall first try filename FOO file extension
    INC drive A, followed by FOO INC Z -- and the preprocessor could leave
    the transformation of all other filename forms undefined -- perhaps
    even looking in its public dataset catalog for a dataset named x.c rather
    than trying to find filename X file extension C drive A.
    --
    Prototypes are supertypes of their clones. -- maplesoft
    Walter Roberson, Jun 3, 2007
    #11
  12. Thad Smith Guest

    Ben Pfaff wrote:
    > Thad Smith <> writes:
    >
    >>There was a project several years ago in which I included a code
    >>file. I was writing serial I/O drivers for multiple ports, but using a
    >>single function and pointers/indexes to the data area associated with
    >>the port significantly increased the interrupt processing time. I
    >>wrote one procedure as a template, then had separate wrapper functions
    >>for each port which included the code file after setting a defined
    >>constant for the port value. For each driver the port was constant,
    >>so no run-time indexing or dereferencing was needed. The I/O ports
    >>were also directly addressed.

    >
    > This technique sometimes makes sense, but I'd suggest that giving
    > an included code file a .c extension is misleading. When I do
    > something like this, I use a .inc or .def or other extension that
    > is less likely to surprise programmers reading the code later.


    God point, Ben. I think I used an extension of .ci.

    --
    Thad
    Thad Smith, Jun 3, 2007
    #12
  13. In article <>,
    Richard Heathfield <> wrote:

    >> Is it legal to do something like this:
    >> #include "x.c" in other source file?


    >Yes.


    Agreed.

    >> And is it good idea?


    >No.


    But this depends entirely on what you're trying to achieve.

    I have a program that performs almost identical operations either on
    sequences of characters - represented as C strings - or on sequences
    of XML elements - represented as counted arrays of struct pointers.
    The low-level operations are different (for example, copying a
    sequence, determining when we have reached the end of a sequence), but
    the control structure is the same. I find it convenient to implement
    this by defining a few macros in two files and #including the main
    body of the code in each of them. This seems a completely reasonable
    technique to me.

    Of course there are other ways to do this. I could have abstracted
    the differences into structs containing functions pointers, at the
    expense of some overhead and the loss of a great deal of automatic
    type-checking. But I can see no reason to reject the use of #include
    to parametrise code at the lexical level.

    -- Richard


    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 3, 2007
    #13
  14. Richard Tobin said:

    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>> Is it legal to do something like this:
    >>> #include "x.c" in other source file?

    >
    >>Yes.

    >
    > Agreed.
    >
    >>> And is it good idea?

    >
    >>No.

    >
    > But this depends entirely on what you're trying to achieve.


    Yes. Nevertheless, it remains the best answer to give a newbie. By the
    time it might be a good idea for him to do it, he'll know enough not to
    do it except when it's a good idea.

    <snip>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 4, 2007
    #14
  15. Richard Guest

    Barry Schwarz <> writes:

    > On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    > <> wrote:
    >
    >> said:
    >>
    >>> Is it legal to do something like this:
    >>> #include "x.c" in other source file?

    >>
    >>Yes.
    >>
    >>> And is it good idea?

    >>
    >>No.
    >>
    >>> Thank you in advance for your answers,
    >>> Igor.

    >>
    >>That'th all right, Igor. The uthual way to incorporate multiple

    >
    > What in the original post prompted you to be this snide? Any value in
    > your response will be lost on all the participants who struggle
    > through this group in a second language. Are you having a running
    > battle with this poster in a different group?


    It seems a common problem in this ng.
    Richard, Jun 4, 2007
    #15
  16. Richard Guest

    Mark McIntyre <> writes:

    > On Sun, 03 Jun 2007 21:50:10 +0000, in comp.lang.c , Richard
    > Heathfield <> wrote:
    >
    >>Barry Schwarz said:
    >>
    >>> On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    >>> <> wrote:
    >>>
    >>>> said:
    >>>>

    >><snip>
    >>>>
    >>>>> Thank you in advance for your answers,
    >>>>> Igor.
    >>>>
    >>>>That'th all right, Igor. The uthual way to incorporate multiple
    >>>
    >>> What in the original post prompted you to be this snide?

    >>
    >>What maketh you think I'm being thnide? Have you never read any
    >>Dithcworld bookth?

    >
    > I guess the problem is that your post could be construed as an
    > insulting joke.
    >
    > Imagine if we insisted on comparing you to Richard the Third, hump and
    > all, or invariably responded in Bronte-esque language and complained
    > about how you'd treated Cathy?


    or even Richard the Turd in the Irish vernacular.

    >
    > Igor is after all a real name as well as a running joke in Discworld.
    >
    > (and yes, I know you won't read this, you killfiled me because you
    > thought I kept insulting you. Hmm, curiouser and curiouser..)


    --
    Richard, Jun 4, 2007
    #16
  17. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > <> wrote:
    >>Is it legal to do something like this:
    >>#include "x.c" in other source file?

    >
    > Whether it will work or not is implementation defined. If my memory
    > serves me correctly this evening, the preprocessor is only -required-
    > to provide a token-to-filename mapping mechanism for the .h extension
    > and for one directory.
    >
    > The C preprocessor is -allowed- to require the user to write the
    > filename token in a manner quite different than the host operating system
    > uses to name files.

    [...]

    C99 6.10.2p4, p5:

    ... The method by which a sequence of preprocessing tokens between
    a < and a > preprocessing token pair or a pair of " characters is
    combined into a single header name preprocessing token is
    implementation-defined.

    The implementation shall provide unique mappings for sequences
    consisting of one or more letters or digits (as defined in 5.2.1)
    followed by a period (.) and a single letter. The first character
    shall be a letter. The implementation may ignore the distinctions
    of alphabetical case and restrict the mapping to eight significant
    characters before the period.

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 4, 2007
    #17
  18. On Sun, 03 Jun 2007 21:50:10 +0000, Richard Heathfield
    <> wrote:

    >Barry Schwarz said:
    >
    >> On Sun, 03 Jun 2007 19:02:18 +0000, Richard Heathfield
    >> <> wrote:
    >>
    >>> said:
    >>>

    ><snip>
    >>>
    >>>> Thank you in advance for your answers,
    >>>> Igor.
    >>>
    >>>That'th all right, Igor. The uthual way to incorporate multiple

    >>
    >> What in the original post prompted you to be this snide?

    >
    >What maketh you think I'm being thnide? Have you never read any
    >Dithcworld bookth?


    Nope, never heard of them. And based on your post, I can't tell if
    the title should be Discworld or Dizcworld. But then again, it
    doesn't really matter.


    Remove del for email
    Barry Schwarz, Jun 4, 2007
    #18
  19. On Sun, 3 Jun 2007 22:27:54 +0000 (UTC), in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >In article <>,
    > <> wrote:
    >>Is it legal to do something like this:
    >>#include "x.c" in other source file?

    >
    >Whether it will work or not is implementation defined. If my memory
    >serves me correctly this evening, the preprocessor is only -required-
    >to provide a token-to-filename mapping mechanism for the .h extension
    >and for one directory.


    AFAIR the interpretation of the token between the "" or <> is left to
    the implementation.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jun 4, 2007
    #19
  20. Richard Heathfield wrote:
    >
    > said:
    >
    > > Is it legal to do something like this:
    > > #include "x.c" in other source file?

    >
    > Yes.
    >
    > > And is it good idea?

    >
    > No.

    [...]

    It has its uses. Consider a file which is included in a "project"
    (quotes used because C doesn't define the word, but most people
    can infer its meaning from context), which is also included, in a
    slightly different customized version, in another "project".

    The customized version uses a file that consists solely of:

    #define USE_CUSTOM_VERSION
    #include "generic_version.c"

    The "generic_version.c" source file then has some #ifdef's on the
    USE_CUSTOM_VERSION name to make different versions.

    Yes, this could be done by using command-line flags to #define
    the symbol for you, but this is compiler-specific, especially
    when you consider that you might want both "object files" to be
    available without recompiling, whereas the two-file method is
    portable.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jun 4, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cloud Burst
    Replies:
    11
    Views:
    1,011
  2. Victor Bazarov

    #include "file" -vs- #include <file>

    Victor Bazarov, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    523
    Exits Funnel
    Mar 6, 2005
  3. PTM
    Replies:
    1
    Views:
    317
    Andy Dingley
    Nov 12, 2007
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    879
    Andreas Bogenberger
    Feb 22, 2008
  5. Replies:
    1
    Views:
    273
    Sebastian \lunar\ Wiesner
    Jul 15, 2008
Loading...

Share This Page