Is it possible to call a C++ function in C program ? if so ?
Regards
Srinath 10 1524
> Is it possible to call a C++ function in C program ? if so ?
You should be able to call extern "C" C++ functions from a C program.
--
Razvan Cojocaru
KeyID: 1024D/04CA34DE
I am asking for otherwise ?
Is extern "C" be used to include C++ code in C and vice versa..Am i
right???
smreddy wrote: I am asking for otherwise ?
smreddy wrote: I am asking for otherwise ?
You can't, at least not portably.
On 29 Jun 2005 01:57:14 -0700, "smreddy" <sr***********@gmail.com>
wrote in comp.lang.c++: Is it possible to call a C++ function in C program ? if so ?
Completely implementation-defined, not specified by the language, but
in many cases the answer is no.
A good many C++ implementations require that C++ code they generate
can only be called from a C++ program. If you include C++ functions
in a C program, they are not really ready to run because necessary
initialization by the C++ start up code has not been performed.
Finally, putting extern "C" on a C++ function does not make it C, it
only gives it C linkage, a rather nebulous concept. If the function
is compiled with a C++ compiler, it is still compiled as C++. If the
function takes reference parameters, for example, then no you can't
call it from C because C cannot provide the reference parameters.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jack Klein wrote: On 29 Jun 2005 01:57:14 -0700, "smreddy" <sr***********@gmail.com> wrote in comp.lang.c++:
Is it possible to call a C++ function in C program ? if so ?
Completely implementation-defined, not specified by the language, but in many cases the answer is no.
A good many C++ implementations require that C++ code they generate can only be called from a C++ program. If you include C++ functions in a C program, they are not really ready to run because necessary initialization by the C++ start up code has not been performed.
Finally, putting extern "C" on a C++ function does not make it C, it only gives it C linkage, a rather nebulous concept. If the function is compiled with a C++ compiler, it is still compiled as C++. If the function takes reference parameters, for example, then no you can't call it from C because C cannot provide the reference parameters.
What if one just switches the compiler half way?
Compile from c++ to assembly with c++ compiler than proceed with c-compiler?
just an idea...
matthias
I guess you can definitely call a C++ function from c.
You just need to know the calling convention used and ofcourse the
linker should link(with name-mangling) the call to link to the C++
function.
But the point is if you randomly call any C++ function, this function
may be using many C++ features for e.g new operator, which would
probably need some initialization by the C++ run-time just like the C
runtime.
And as a result it will eventually throw an exception.
This would be the case if you are linking to a static C++ library. But
i guess the above scenario may just work on dynamic libraries.
I am not air-tight sure about the above.
Could anyone care to clarify.
Vinu
Herr Matthias Pfeifer wrote: Jack Klein wrote: On 29 Jun 2005 01:57:14 -0700, "smreddy" <sr***********@gmail.com> wrote in comp.lang.c++:
Is it possible to call a C++ function in C program ? if so ?
Completely implementation-defined, not specified by the language, but in many cases the answer is no.
A good many C++ implementations require that C++ code they generate can only be called from a C++ program. If you include C++ functions in a C program, they are not really ready to run because necessary initialization by the C++ start up code has not been performed.
Finally, putting extern "C" on a C++ function does not make it C, it only gives it C linkage, a rather nebulous concept. If the function is compiled with a C++ compiler, it is still compiled as C++. If the function takes reference parameters, for example, then no you can't call it from C because C cannot provide the reference parameters.
What if one just switches the compiler half way? Compile from c++ to assembly with c++ compiler than proceed with c-compiler?
just an idea...
matthias
Jack Klein wrote: Finally, putting extern "C" on a C++ function does not make it C, it only gives it C linkage, a rather nebulous concept. If the function is compiled with a C++ compiler, it is still compiled as C++. If the function takes reference parameters, for example, then no you can't call it from C because C cannot provide the reference parameters.
'extern "C"' states that the function can be called using C conventions
and might be implemented in C. It could also be implemented in C++. Of
course, if the declaration uses constructs unavailable in C like
reference parameters or class types, it can neither be implemented in
C nor called from C. However, if the function only uses C constructs it
is likely that it can be called from C even though it is implemented in
C++ and in my experience this is indeed the case. Actually, in all
cases I tested it is even possibly to throw exceptions through C code.
Of course, the C++ standard does not make an guarantees about the
behavior of C code nor does the C standard make an guarantees.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Vinu schreef: I guess you can definitely call a C++ function from c. You just need to know the calling convention used and ofcourse the linker should link(with name-mangling) the call to link to the C++ function.
But the point is if you randomly call any C++ function, this function may be using many C++ features for e.g new operator, which would probably need some initialization by the C++ run-time just like the C runtime. And as a result it will eventually throw an exception.
Or fail in another way (e.g. null pointer dereference) You got the
main reason right, though. The library initialization is the main
part. Exception handling may be another issue. That's not really
library-related, but it can affect the ability to call a C++ function
- even if the exception is caught again inside the C++ part! C may
simply fail to provide a suitable stack for exceptions.
Basically the C compiler has to help, too.
Regards,
Michiel Salters
msalters wrote:
Vinu schreef: I guess you can definitely call a C++ function from c. You just need to know the calling convention used and ofcourse the linker should link(with name-mangling) the call to link to the C++ function.
But the point is if you randomly call any C++ function, this function may be using many C++ features for e.g new operator, which would probably need some initialization by the C++ run-time just like the C runtime. And as a result it will eventually throw an exception.
Or fail in another way (e.g. null pointer dereference) You got the main reason right, though. The library initialization is the main part. Exception handling may be another issue. That's not really library-related, but it can affect the ability to call a C++ function - even if the exception is caught again inside the C++ part! C may simply fail to provide a suitable stack for exceptions.
Basically the C compiler has to help, too.
Regards, Michiel Salters
I just hit this while looking over Stroustrup's FAQ, and remembered this
thread. http://www.research.att.com/~bs/bs_faq2.html#callCpp
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Manuel |
last post by:
Can I invoke a function before main
I could do it by invoking it in a Global object's constructor .
Is there any other method other than this.
Manuel
|
by: ~neil~ |
last post by:
hello
I'm fairly new to the language, and my problem is I'm trying to write
a program to run a command from the DOS prompt "C:>". I don't know all
the functions in "C", however there are a few...
|
by: Jack |
last post by:
I want to design a server which performs some business related user
authentiation, and would like to let the clients invoke it through
webservice, so my first thinking is using IIS as the web...
|
by: ypjofficial |
last post by:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
eg.
#include <iostream.h>
class test
{
public:
void...
|
by: Haxan |
last post by:
Hi,
I have an unmanaged application that converts a function pointer to a
delegate and then pass this as a parameter(delegate) to a managed function
which then invokes it. Currently Im able to...
|
by: ketan |
last post by:
Hi All,
I have a situation where in I need to call static methods of the
derived class. I checked previous posts, but could not get any
satisfactory reply.
My situation can be simulated with...
|
by: dgunjal |
last post by:
Is there any method by which i can invoke C++ class functions within C functions?
|
by: Matthew Lock |
last post by:
Hello,
I am automating Internet Explorer in order to do some simple automated
testing of a web application. How do I invoke Javascript functions in
the web page I load?
I can successfully start...
|
by: Thijs |
last post by:
Hello everybody,
I´ve got a problem that´s slightly complicated (at least to me it
seems) so I will try to state it as clearly as possible.
At a certain point in time, I start a second thread...
|
by: Ramesh |
last post by:
Hi,
I am trying to create an array of pointers to member functions inside
my class. When I created a global array of type pfn & initialized with
member functions and copy it back to the member...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |