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;
}
----------------
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;
}
----------------