473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to create a C++ fn. which can be called from both C and C++ files

I have an existing C++ library . Now i am another application using
this C++ lib.
The problem here is i want to call C++ fn. from C file .If i use an
extern "C"declaration for that fn. then the original calls to this fn
creates error . eg

tobeused.cc

writtenincpp(int i ,char c)
{

}
---------

Now this fun writtenincpp() is being called from other C++ files in the
library.The compiler here does name mangling so other calls are
resolved (from c++ files)
But I want to call this fn. from a C file . so if i use
tobeused.cc

extern "C" void writtenincpp(int ,char)
writtenincpp(int i,char c)
{

}
----------------
The compiler now does not perform name mangling and this can now be
called successfully from C file .But the other refrences through C++
files generates an error.
So ,is there any means usingwhich i can have a fn. written in C++ file
which can be called from any C or C++ files (without changing anything
in other C++ files)

regards,
ankit

Sep 14 '06 #1
7 1784
<an*************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I have an existing C++ library . Now i am another application using
this C++ lib.
The problem here is i want to call C++ fn. from C file .If i use an
extern "C"declaration for that fn. then the original calls to this fn
creates error . eg

tobeused.cc

writtenincpp(int i ,char c)
{

}
---------

Now this fun writtenincpp() is being called from other C++ files in
the
library.The compiler here does name mangling so other calls are
resolved (from c++ files)
But I want to call this fn. from a C file . so if i use
tobeused.cc

extern "C" void writtenincpp(int ,char)
writtenincpp(int i,char c)
{

}
----------------
The compiler now does not perform name mangling and this can now be
called successfully from C file .But the other refrences through C++
files generates an error.
So ,is there any means usingwhich i can have a fn. written in C++ file
which can be called from any C or C++ files (without changing anything
in other C++ files)
comp.lang.c++ is a better place to ask; comp.lang.c likes to pretend C++
doesn't exist. clc++ acknowledges C's existence.

<OT>
The library must have been compiled with the function declared as extern
"C"; that disables name-mangling. You can't just modify the header; you
have to recompile the library (and everything that depends on it).

If you compile the function without that, it will be mangled as normal,
and then your attempt to call it as extern "C" without name mangling
will result in a linker error.

You may find it easier to create a stub .cpp file which contains an
extern "C" function which then calls the native C++ function. This stub
function is what you'd need to call from C. Slower, but easier to do,
especially if you can't easily modify the library (and everything that
calls it from C++).
</OT>

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '06 #2
On 2006-09-14, an*************@gmail.com <an*************@gmail.comwrote:
[snip]
extern "C" void writtenincpp(int ,char)
writtenincpp(int i,char c)
{

}
----------------
The compiler now does not perform name mangling and this can now be
called successfully from C file .But the other refrences through C++
files generates an error.
>
So ,is there any means usingwhich i can have a fn. written in C++ file
which can be called from any C or C++ files (without changing anything
in other C++ files)
You put

#ifdef __cplusplus
extern "C"
#endif
void writtenincpp(int i,char c);
#ifdef __cplusplus
}
#endif

in a header file. The point of the #ifdef __cplusplus is you can share
this header between C and C++-- the C compiler will not define
__cplusplus so will ignore the extern "C" (because to a C compiler,
everything is extern "C"); the C++ compiler will, so will cotton on to
the fact that this function is not name-mangled etc., and so should
resolve it correctly.
Sep 14 '06 #3
an*************@gmail.com posted:
I have an existing C++ library . Now i am another application using
this C++ lib.
The problem here is i want to call C++ fn. from C file .

Introduce abbreviations before using them.

Better yet, don't use them.

fn == function

If i use an extern "C"declaration for that fn. then the original calls
to this fn creates error . eg

tobeused.cc

writtenincpp(int i ,char c)
{

}

Not sure what you're saying here. If you're trying to compile C code as
C++, or vice versa, then you shouldn't be.

I'm not sure what your question is, but nonetheless here's a sample program
which compiles, links and produces a working executable for me with
gcc/g++:

/* a.c : Compiled as C */

void Func(char const*);

int main(void)
{
Func("Hello World!");
return 0;
}

/* b.cpp : Compiled as C++ */

#include <iostream>

extern "C" void Func(char const *const p)
{
std::cout << p << '\n';
}

--

Frederick Gotham
Sep 14 '06 #4

<an*************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I have an existing C++ library . Now i am another application using
this C++ lib.
The problem here is i want to call C++ fn. from C file .If i use an
extern "C"declaration for that fn. then the original calls to this fn
creates error . eg

tobeused.cc

writtenincpp(int i ,char c)
{

}
---------

Now this fun writtenincpp() is being called from other C++ files in the
library.The compiler here does name mangling so other calls are
resolved (from c++ files)
But I want to call this fn. from a C file . so if i use
tobeused.cc

extern "C" void writtenincpp(int ,char)
writtenincpp(int i,char c)
{

}
----------------
The compiler now does not perform name mangling and this can now be
called successfully from C file .But the other refrences through C++
files generates an error.
So ,is there any means usingwhich i can have a fn. written in C++ file
which can be called from any C or C++ files (without changing anything
in other C++ files)

regards,
ankit
Since the C program (or method) does not know about objects, it has
no instance variable. This it can only call static methods, not instance
methods.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 14 '06 #5
Hi Frederick,
I have just been provided with the header files and the compiled .o's
..So i cant change the function definition ,all i have is access to
function prototypes.So to enable calling this function from C file i'll
have to create another c++ file and call the desired fn. from this
file. i.e

a.c
-----

intermediate();

/----

intermediate.cc
-----
extern "C" intermediate();
intermediate()
{
writtenincpp();
}
/-----

actualfn.cc
----
{
writtenincpp()

}
/---

right?

But is there any other way without having to create a new cc file??

Frederick Gotham wrote:
an*************@gmail.com posted:
I have an existing C++ library . Now i am another application using
this C++ lib.
The problem here is i want to call C++ fn. from C file .


Introduce abbreviations before using them.

Better yet, don't use them.

fn == function

If i use an extern "C"declaration for that fn. then the original calls
to this fn creates error . eg

tobeused.cc

writtenincpp(int i ,char c)
{

}


Not sure what you're saying here. If you're trying to compile C code as
C++, or vice versa, then you shouldn't be.

I'm not sure what your question is, but nonetheless here's a sample program
which compiles, links and produces a working executable for me with
gcc/g++:

/* a.c : Compiled as C */

void Func(char const*);

int main(void)
{
Func("Hello World!");
return 0;
}

/* b.cpp : Compiled as C++ */

#include <iostream>

extern "C" void Func(char const *const p)
{
std::cout << p << '\n';
}

--

Frederick Gotham
Sep 14 '06 #6
an*************@gmail.com posted:
Hi Frederick,
I have just been provided with the header files and the compiled .o's
.So i cant change the function definition ,all i have is access to
function prototypes.So to enable calling this function from C file i'll
have to create another c++ file and call the desired fn. from this
file

That would work, yes. I don't know of any other way of achieving what you
want, although I'm sure you'd gain a wealth of knowledge over on
comp.lang.c++.

--

Frederick Gotham
Sep 14 '06 #7

<an*************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
I have just been provided with the header files and the compiled .o's
.So i cant change the function definition ,all i have is access to
function prototypes.So to enable calling this function from C file i'll
have to create another c++ file and call the desired fn. from this
file. i.e

a.c
-----

intermediate();

/----

intermediate.cc
-----
extern "C" intermediate();
intermediate()
{
writtenincpp();
}
/-----

actualfn.cc
----
{
writtenincpp()

}
/---

right?

But is there any other way without having to create a new cc file??
To the best of my knowledge, belief, and experience, NO.

The function names in the C++ *.o files are all "mangled" (or
"decorated", to be more decorous), and only God and the
C++ compiler writer knows how.

Remember, you may also be trying to call C++ functions
with return values and arguments that are not recognized by C.
You may have more problems than you are currently aware
of, so going through the two-step above will also allow you
to fix those problems.

---
William Ernest Reid

Sep 15 '06 #8

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

Similar topics

7
by: Dennis | last post by:
I am very new to PHP, although I have been using ASP for a few years. I use a product in ASP called ASPTear, it's a small dll that my hosting company happens to have loaded. Basically you can...
22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
15
by: Viviana Vc | last post by:
How can I programatically do the equivalent of the following: cacls "C:\Program Files\test" /T /G Everyone:f ? Thanks, Viv
11
by: Andre | last post by:
Hi, I have ASP.NET application running on standalone (not part of the domain) Windows 2003. I use forms authentication for my application. The problem I have is that I need to create and read...
6
by: JonSteng | last post by:
..Net Visual Studio Professional 2003 Version 7.1.3088 ..Net Framework 1.1 SP1 Version 1.1.4322 IIS 5.1 Windows XP Professional SP2 Micron T3000 Laptop (1.5 GHz; 1GB RAM; 40GB HD with 17GB Free)...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
1
by: larryawade | last post by:
I need the ability to automate the creation of 500 similar webpages for an educational site. Let’s say approximately 100 lessons with each lesson having 3-5 pages. I already have a program that...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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...
0
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...
0
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...

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.