473,472 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using a namespace in extern "C"

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

May 1 '06 #1
4 11387
* 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 1 '06 #2
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...

May 1 '06 #3
salkesh wrote:
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?

May 1 '06 #4
Hey Rolf,

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

....
alkesh

May 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
4
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
5
3
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
10
6
by: MACKTEK | last post by:
Intro: I have recently started programming in C++/CLI. I have done a lot of research on the net trying to locate an answer to this problem, but there seems to be a lack of Tutorials and books on...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
15
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.