473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ 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 11452
* 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 RadamrikambraMu ltibruFluxa 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_initial ize: Initializing TCP/IP module.
../test-bmi-server: relocation error: /usr/lib/libtcp2udtsync. so:
undefined symbol: _ZN4CUDT6socket Eiii

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(<pa rams>);

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_initial ize: Initializing TCP/IP module.
./test-bmi-server: relocation error: /usr/lib/libtcp2udtsync. so:
undefined symbol: _ZN4CUDT6socket Eiii
That seems to mean you tried to call the function

CUDT::socket(in t, 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(<pa rams>);

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

5
1957
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
3524
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 using std::cin; using std::cout;
1
2653
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
3082
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 things specific to C++. One thing that causes me many troubles is how to properly nest "#include 'filename'" statements in combination with the "#pragma once" directive and
3
3005
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
6202
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
30
4126
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
25
2598
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 example: with quitCommandButton .enabled = true .default = true end with
14
6031
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
6
2606
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 C++/CLI. I have read 4 of the published books as best I could. The one book that has a great deal of info for my interest was Pro Visual C++/CLI and the .NET 2.0 platform by Fraser. Problem: Using VS2005 C++ and writing C++/CLI code, and using...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7644
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.