C++ code in C file

Discussion in 'C++' started by Bhan, Oct 5, 2005.

  1. Bhan

    Bhan Guest

    I have written code inside a c file in C++ Style(NOTE: No objects are
    created by myself.I am just using singleton objects in my c file.Also
    used extern "c").I am able to compile,link and test.

    But my question is that how I am able to get singleton instances in a C
    file?

    Is that writing C++ Code in C and viceversa doesn't matter as long as
    you use extern for c type of calls?

    Also.how does the compiler treat the code?
    - Does it treat the code inside a c file as just some piece of code of
    C or c++ language?(as translation unit and not bother about the file
    name?)


    If yes,Can I define classes in c files and compile successfully using a
    C++ Compiler?


    By the way I am using GNU Compiler for C++.
    Bhan, Oct 5, 2005
    #1
    1. Advertising

  2. Bhan

    Dan Cernat Guest

    Bhan wrote:
    > I have written code inside a c file in C++ Style(NOTE: No objects are
    > created by myself.I am just using singleton objects in my c file.Also
    > used extern "c").I am able to compile,link and test.
    >
    > But my question is that how I am able to get singleton instances in a C
    > file?
    >
    > Is that writing C++ Code in C and viceversa doesn't matter as long as
    > you use extern for c type of calls?
    >
    > Also.how does the compiler treat the code?
    > - Does it treat the code inside a c file as just some piece of code of
    > C or c++ language?(as translation unit and not bother about the file
    > name?)
    >
    >
    > If yes,Can I define classes in c files and compile successfully using a
    > C++ Compiler?
    >
    >
    > By the way I am using GNU Compiler for C++.



    I cannot figure out what exactly you did and what you want to do. Show
    us some code. Otherwise the answer is yes ... or no.

    /dan
    Dan Cernat, Oct 5, 2005
    #2
    1. Advertising

  3. Bhan

    Bhan Guest

    Sorry for confusing...Dont bother abt my program
    This is my requirement...

    1.Is that writing C++ Code in C and viceversa doesn't matter as long as

    you use extern for c type of calls?


    2.Also.how does the compiler treat the code?
    - Does it treat the code inside a c file as just some piece of code of
    C or c++ language?(as translation unit and not bother about the file
    name?).If yes,Can I define classes in c files and compile successfully
    using a C++ Compiler?
    Bhan, Oct 5, 2005
    #3
  4. Bhan

    Dan Cernat Guest

    Bhan wrote:
    > Sorry for confusing...Dont bother abt my program
    > This is my requirement...
    >
    > 1.Is that writing C++ Code in C and viceversa doesn't matter as long as
    >
    > you use extern for c type of calls?
    >
    >
    > 2.Also.how does the compiler treat the code?
    > - Does it treat the code inside a c file as just some piece of code of
    > C or c++ language?(as translation unit and not bother about the file
    > name?).If yes,Can I define classes in c files and compile successfully
    > using a C++ Compiler?


    Writing C++ code in C? WTF is that? There was a joke going around: *a
    good programmer can write FORTRAN in any language*. Is this what you
    are trying to do?

    Usually the compilers will look at the file extension. if it is .CPP it
    will compile it as C++. If it is .C it will compile it as C. If you use
    C++ features in a .C file, the compiler might choke.

    /dan
    Dan Cernat, Oct 5, 2005
    #4
  5. Bhan

    Markus Moll Guest

    Hi

    Bhan wrote:

    > Sorry for confusing...Dont bother abt my program
    > This is my requirement...
    >
    > 1.Is that writing C++ Code in C and viceversa doesn't matter as long as
    >
    > you use extern for c type of calls?


    Short answer: You cannot write C++ programs in C and vice-versa (this would
    mean C == C++ syntactically, wouldn't it?)

    Long answer: As C++ is based upon C, a lot of C++ code actually also is
    valid C code (and vice-versa). E.g. as long as you stick to global
    functions (not using bool, inline (introduced in C99, wasn't it?), class,
    templates or other C++ stuff) your code is likely to compile fine with a C
    compiler.

    The extern "C" declaration is only needed if you want to _link_ C code to
    your _C++_ application.

    > 2.Also.how does the compiler treat the code?
    > - Does it treat the code inside a c file as just some piece of code of
    > C or c++ language?(as translation unit and not bother about the file
    > name?).


    A C++ compiler will treat your code as C++, a C compiler as C, a Java
    compiler as Java, a Haskell compiler as Haskell... what else do you expect?

    Your compiler frontend might choose to switch between compilers depending on
    the file name (or extension). The manual should tell you details.

    > If yes,Can I define classes in c files and compile successfully
    > using a C++ Compiler?


    No, you cannot add C++ code to a C file and expect it to compile; just one
    of a dozen of simple reasons:

    int throw(void)
    {
    }

    is legal C code...


    Markus
    Markus Moll, Oct 5, 2005
    #5
    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. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,152
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Ryan
    Replies:
    1
    Views:
    502
    Scott Allen
    Nov 5, 2004
  3. jason
    Replies:
    4
    Views:
    2,632
    jason
    Jun 2, 2005
  4. keithb
    Replies:
    1
    Views:
    882
    Bruce Barker
    Mar 29, 2006
  5. higer
    Replies:
    10
    Views:
    866
    higer
    Jun 8, 2009
Loading...

Share This Page