Interaction between C and C++

J

John Eskie

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.push_back(nVal);
}

int RemoveInt()
{
int nVal;

nVal = g_IntVector.back();
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;
}
----------------
 
V

Victor Bazarov

John Eskie said:
[..
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?

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
 
J

John Eskie

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top