mixing C++ with C

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

    mixing C++ with C

    Hi,

    I'd appreciate any thoughts on mixing C++ and C code. I have a project that
    uses a given C interface, the rest of the project can be either in C or C++.
    What would be the recomended design pattern in this case? C++ allows for a
    better design, but integrating the code with the C code isn't straight
    forward, it becomes rather messy. Code readability and maintenance are
    important, since the project will grow in the future. Maybe someone could
    speak from personal experience about this.

    Thanks,
    Cristian




  • Rolf Magnus

    #2
    Re: mixing C++ with C

    Cristian Tota wrote:
    [color=blue]
    > Hi,
    >
    > I'd appreciate any thoughts on mixing C++ and C code. I have a project
    > that uses a given C interface, the rest of the project can be either
    > in C or C++. What would be the recomended design pattern in this case?
    > C++ allows for a better design, but integrating the code with the C
    > code isn't straight forward, it becomes rather messy. Code readability
    > and maintenance are important, since the project will grow in the
    > future. Maybe someone could speak from personal experience about this.[/color]

    It might be a good idea to wrap a C++ class interface around the C API,
    so that this API is used directly only in small parts of your program.

    Comment

    • Mike Wahler

      #3
      Re: [FAQ, link] mixing C++ with C


      "Cristian Tota" <cristian.tota@ vion-software.ro> wrote in message
      news:bqlpl2$v1p $1@ally.taide.n et...[color=blue]
      > Hi,
      >
      > I'd appreciate any thoughts on mixing C++ and C code. I have a project[/color]
      that[color=blue]
      > uses a given C interface, the rest of the project can be either in C or[/color]
      C++.[color=blue]
      > What would be the recomended design pattern in this case? C++ allows for a
      > better design, but integrating the code with the C code isn't straight
      > forward, it becomes rather messy. Code readability and maintenance are
      > important, since the project will grow in the future. Maybe someone could
      > speak from personal experience about this.[/color]





      HTH,
      -Mike


      Comment

      • Dan W.

        #4
        Re: mixing C++ with C

        On Thu, 4 Dec 2003 01:17:05 +0200, "Cristian Tota"
        <cristian.tota@ vion-software.ro> wrote:
        [color=blue]
        >Hi,
        >
        >I'd appreciate any thoughts on mixing C++ and C code. I have a project that
        >uses a given C interface, the rest of the project can be either in C or C++.
        >What would be the recomended design pattern in this case? C++ allows for a
        >better design, but integrating the code with the C code isn't straight
        >forward, it becomes rather messy. Code readability and maintenance are
        >important, since the project will grow in the future. Maybe someone could
        >speak from personal experience about this.
        >
        >Thanks,
        >Cristian
        >[/color]

        stand-alone functions, you can wrap in

        extern "C"
        {
        int foo(){}
        }

        If you need to expose classes to C, you can concatenate the class name
        to the function names, make them extern "C" and pass to them an extra
        static pointer to substitute the this pointer. Member variables become
        file-scope static variables.
        A few issues back of CUJ there was an article on state machines which
        presented object oriented code techniques in C to go with it.
        But, usually, for interfacing you'll probably just need interface
        classes --i.e. just functions, probably static, which you can collect
        into ,C files, which in turn call the C++ code.

        Hope it helps.
        Cheers!

        Comment

        • E. Robert Tisdale

          #5
          Re: mixing C++ with C

          Cristian Tota wrote:
          [color=blue]
          > I'd appreciate any thoughts on mixing C++ and C code.
          > I have a project that uses a given C interface.[/color]

          What does that mean?
          Do you mean that you have libraries implemented in C?
          [color=blue]
          > The rest of the project can be either in C or C++.
          > What would be the recommended design pattern in this case?
          > C++ allows for a better design
          > but integrating the code with the C code isn't straight forward.[/color]

          If you say so.
          [color=blue]
          > It becomes rather messy.[/color]

          It shouldn't.
          [color=blue]
          > Code readability and maintenance are important
          > since the project will grow in the future.[/color]

          Try to use C++ for the "rest of the project".
          [color=blue]
          > Maybe someone could speak from personal experience about this.[/color]

          If you have source code for your C code,
          try to compile it with your C++ compiler.
          It is *much* easier to link object code together
          if they are all compiled with the same compiler.

          If you don't have all of the C source code (just the library archives),
          extern "C" can be used to *help* with linkage. Try using

          extern "C" {
          #include <file.h>
          }

          to include your C header files.

          Use inline functions to define C++ [member] functions, operators
          and constructors which call the "C interface" so that
          no *direct* references to the C interface
          appear in your application source code.

          Comment

          Working...