Interaction between C and C++

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

    Interaction between C and C++

    Hello,

    I'm writing a code library which must have a C interface. Since I can see
    many advantages by using STL over making your own linked lists I thought I
    would make a test to see if I could get it working.
    The test consist of 2 parts: a C++ file which has C interface and a plain C
    file which calls the functions offered.

    The code is listed below.
    Even though it seems to work I am not convinced that it's safe. As a example
    C doesn't have constructors or destructors. In the example the vector<int>
    constructor needs to be called and similar for the destructor. So when the
    constructor/destructor gets called depends on when the objects gets in and
    out of scope which C wouldn't know anything about. My theory is that the
    linker has something additional to do such as call the constuctor when the
    object is about to be called. I verified that my theory was right by
    disassembling the resulting object code from the example code and saw that
    there is a public symbol created with the name _CRT$XCU which code calls the
    destructor. So it seems that CRT comes into play.

    Can any of you tell me how this works? Would it be safe for distribution?
    My tests were done with MS VC++ but is it safe to assume that other
    compilers under other OS's such as gcc would work similarly?

    Thanks,

    -- John

    File listing:
    lib.cpp:
    ----------------
    #include "lib.h"

    #include <vector>

    using namespace std;

    vector<int> g_IntVector;

    void AddInt(int nVal)
    {
    g_IntVector.pus h_back(nVal);
    }

    int RemoveInt()
    {
    int nVal;

    nVal = g_IntVector.bac k();
    g_IntVector.pop _back();

    return nVal;
    }
    ----------------
    lib.h:
    ----------------
    #ifndef LIB_H
    #define LIB_H

    #ifdef __cplusplus
    extern "C"
    {
    #endif

    void AddInt(int nVal);
    int RemoveInt();

    #ifdef __cplusplus
    }
    #endif

    #endif /* LIB_H */
    ----------------

    testprog.c:
    ----------------
    #include "../lib.h"
    #include <stdio.h>

    int main()
    {
    AddInt(1);
    AddInt(2);
    AddInt(3);
    AddInt(4);

    printf("%d\n", RemoveInt());
    printf("%d\n", RemoveInt());
    printf("%d\n", RemoveInt());
    printf("%d\n", RemoveInt());
    return 0;
    }
    ----------------


  • Victor Bazarov

    #2
    Re: Interaction between C and C++

    "John Eskie" <cyberheg@l115. langkaer.dk> wrote...[color=blue]
    > [..
    > Can any of you tell me how this works? Would it be safe for distribution?
    > My tests were done with MS VC++ but is it safe to assume that other
    > compilers under other OS's such as gcc would work similarly?[/color]

    The main problem is that global construction/destruction is supposed
    to happen before 'main' in C++. However, if your 'main' is not in
    C++ but in C, there is no guarantee that it's going to happen. So,
    it is better not to rely upon global objects. If you have a need to
    have a singleton in your library, make it dynamically constructed.
    Better yet, have some kind of "init" and "close" for your library so
    that it knows when the objects have to be constructed and destroyed.

    Victor


    Comment

    • John Eskie

      #3
      Re: Interaction between C and C++

      > The main problem is that global construction/destruction is supposed[color=blue]
      > to happen before 'main' in C++. However, if your 'main' is not in
      > C++ but in C, there is no guarantee that it's going to happen. So,
      > it is better not to rely upon global objects. If you have a need to
      > have a singleton in your library, make it dynamically constructed.
      > Better yet, have some kind of "init" and "close" for your library so
      > that it knows when the objects have to be constructed and destroyed.[/color]

      You're thinking of something like:

      myclass *pObj;
      void init()
      {
      pObj = new myclass();
      }

      void close()
      {
      delete pObj;
      }

      So I would call constructor/destructor when I want it to happen, right?

      -- John


      Comment

      Working...