Using a namespace in extern "C"

S

salkesh

Hi,

I have got a C++ code which has a namespace definition and some
functions in it -

namespace A {

int func1();
int func2();

}

now i want to access this functions from another function which has "C"
linkage...
how can i do this...?

currently i am doin it in this way but it is giving error -

in a .h file i have defined a function as -

extern "C" int cfunc();

and the in the definition in the corresponding c++ file -

int cfunc()
{
int x = A::func1();
}

and i am compiling this with a g++ compiler to create a shared library
which is loaded in another C code ;-)

plz reply asap... need this urgently...
thnx a lot...

....
alkesh
 
A

Alf P. Steinbach

* salkesh:
I have got a C++ code which has a namespace definition and some
functions in it -

namespace A {

int func1();
int func2();

}
Goodie.


now i want to access this functions from another function which has "C"
linkage...
how can i do this...?

currently i am doin it in this way but it is giving error -

What kind of error? Be specific. There are no mind-readers on planet
Earth, except the telepathic trolls from RadamrikambraMultibruFluxa 5,
but I think the last one is being hunted down as I write...

in a .h file i have defined a function as -

extern "C" int cfunc();

and the in the definition in the corresponding c++ file -

int cfunc()
{
int x = A::func1();
}

That's one way to do it, yes.

and i am compiling this with a g++ compiler to create a shared library
which is loaded in another C code ;-)

Be aware that C and C++ are two different languages, and have different
requirements for runtime library initialization. Possibly the C++
runtime library isn't being properly initialized (I haven't tried this,
so don't know). Try changing your _main_ program to C++, I think that
should maximize the chances of getting things to work; also see your
system and compiler specific documentation about shared library issues.

Btw., shared libraries are largely off-topic in this group.

That's because C++ doesn't actively support them.
 
S

salkesh

hey Alf,

thnx for the reply... nd sorry for talkin abt shared libraries..
actually i do not think the error is because of shared libraries.. it
was just tat i mentioned it..
ok... the error i get is -

[D 16:54:28.731016] BMI_tcp_initialize: Initializing TCP/IP module.
../test-bmi-server: relocation error: /usr/lib/libtcp2udtsync.so:
undefined symbol: _ZN4CUDT6socketEiii

and some more info about the code -

the name space is UDT and i m tryin to call from within an extern "C"
function -
UDT::socket(<params>);

this namespace and the functions is defined in a separate header file
<udt.h> which i m using..

thnx once again...
 
R

Rolf Magnus

salkesh said:
hey Alf,

thnx for the reply... nd sorry for talkin abt shared libraries..
actually i do not think the error is because of shared libraries.. it
was just tat i mentioned it..
ok... the error i get is -

[D 16:54:28.731016] BMI_tcp_initialize: Initializing TCP/IP module.
./test-bmi-server: relocation error: /usr/lib/libtcp2udtsync.so:
undefined symbol: _ZN4CUDT6socketEiii

That seems to mean you tried to call the function

CUDT::socket(int, int, int)

but the linker couldn't find an implementation for it.
and some more info about the code -

the name space is UDT and i m tryin to call from within an extern "C"
function -
UDT::socket(<params>);

this namespace and the functions is defined in a separate header file
<udt.h> which i m using..

Are you sure that the function is _defined_ there, not just declared?
 
S

salkesh

Hey Rolf,

Thanx for pointing that out.
The problem is solved. It is working now.

....
alkesh
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top