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

A

ankitjain.bvcoe

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
 
S

Stephen Sprunk

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
 
F

Fred Kleinschmidt

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.
 
B

Ben C

On 2006-09-14 said:
extern "C" void writtenincpp(int ,char)
writtenincpp(int i,char c)
{

}
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.
 
F

Frederick Gotham

(e-mail address removed) 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';
}
 
A

ankitjain.bvcoe

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??
 
F

Frederick Gotham

(e-mail address removed) 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++.
 
B

Bill Reid

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.
 

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,009
Latest member
GidgetGamb

Latest Threads

Top