By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
<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

P: n/a
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

P: n/a
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

P: n/a

<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

P: n/a
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

P: n/a
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

P: n/a

<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 discussion thread is closed

Replies have been disabled for this discussion.