473,804 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linking C++ functions in a C program

Hi everyone, I'm trying to compile a C++ function and then call it from
a C program.
Since Google is my friend I've ended up to this link which seems very clear:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

Unfortunately it does not work.
Here is what I'm doing:

-----------------------------------------
//main.c

#include "mylib.h"

int main (int argc, char *argv[]){
foo();
return 0;
}
-----------------------------------------
//mylib.h

#indef __cplusplus
extern "C"{
#endif

void foo();

#ifdef __cplusplus
}
#endif
-----------------------------------------
//mylib.cc

#include <iostream>
#include "mylib.h"

using namespace std; // don't know what for!

void foo(){
cout<<"Hello World!\n";
}
-----------------------------------------
Thanks a lot for any suggestion.

Al
Nov 6 '08
38 2669
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

CBFalconer wrote:
In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.
7.5/3 states that: "Every implementation shall provide for linkage to
functions written in the C programming language" by allowing things like
extern "C"
7.5/6 tells us that is is possible to define function with C-style
linkage in C++ code.
7.5/9 informs us that implementation can allow to link objects from
other languages to C++ and from C++ to other languages.

Concluding, C++ standard gives possibility to link C++ functions from C
code (with some naming restrictions).

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkk TLUIACgkQPFW+cU iIHNrXWwCfaS8A4 1lRmL6kpsneGgyH LEBt
qdEAoJCroT5PWNI luUsCyyfE1IQUor Po
=R7xw
-----END PGP SIGNATURE-----
Nov 6 '08 #11
CBFalconer <cb********@yah oo.comwrites:
abasili wrote:
>Ian Collins wrote:
>>It does if you follow all the points. The first two are often
overlooked.

I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++,
there are too many incompatibiliti es and a lot of work has been
already done in C. So I have to find another way to call a C++
function through my C code.

In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.
Chuck, we've had this discussion before. Yes, you can call C++
functions from C, and yes, you can call C functions from C++. C++
defines the mechanism for doing this in both directions.

Last time we discussed this, I pointed you to the C++ FAQ that
explains this; it's already been cited in this thread. I even
demonstrated how to do it and posted the output. For some reason you
still didn't believe me, and now you're claiming yet again that it
can't be done.

Please consider the possibility that you might be wrong.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 6 '08 #12
CBFalconer wrote, On 06/11/08 21:54:

<snip>
describes the access to the C code required. In general C++ can
access C code, but C cannot access C++ code.

Remember, the languages are different.
Remember, it has been pointed out to you many times and with references
that you are WRONG. C++ defines how C code can call C++ code and you
have been shown how it works. Since the theory says it should work and
practice shows that it works why keep insisting that it does not?
Neither theory nor practice are on your side.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 6 '08 #13

Keith Thompson <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
CBFalconer <cb********@yah oo.comwrites:
abasili wrote:
Ian Collins wrote:

It does if you follow all the points. The first two are often
overlooked.

I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++,
there are too many incompatibiliti es and a lot of work has been
already done in C. So I have to find another way to call a C++
function through my C code.
In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.

Chuck, we've had this discussion before. Yes, you can call C++
functions from C, and yes, you can call C functions from C++. C++
defines the mechanism for doing this in both directions.

Last time we discussed this, I pointed you to the C++ FAQ that
explains this; it's already been cited in this thread. I even
demonstrated how to do it and posted the output. For some reason you
still didn't believe me, and now you're claiming yet again that it
can't be done.

Please consider the possibility that you might be wrong.
Please consider the possibility that he might be
<sotto voce>insane</sotto voce>.

I mean, there is a total nadir of Usenet kookery, the
murky abyss where sense is nonsense and right is wrong
and reality can never escape the intense gravitational pull of
the completely collapsed nuerons of the infernally eternally
defiantly dense.

Also, please consider the possibility that YOU might
be insane if you ever try to correct him on this or the many
other topics he is wrong, according to this widely-accepted
measure:

"Insanity is doing the same thing over and over again and
expecting a different result."

---
William Ernest Reid

Nov 7 '08 #14
Flash Gordon wrote:
CBFalconer wrote, On 06/11/08 21:54:
If you're responding to what he wrote at that time, why don't you post
it as a response to the message in which he actually wrote it, on the
thread in which he wrote it? It's a little confusing to post it as a
response to a message I wrote, in a thread where he's said some
incorrect things about C/C++ linking, but he hasn't actually said these
particular incorrect things (at least, not yet):
>describes the access to the C code required. In general C++ can
access C code, but C cannot access C++ code.
Nov 7 '08 #15
CBFalconer wrote:
abasili wrote:
>Ian Collins wrote:
>>It does if you follow all the points. The first two are often
overlooked.
I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++,
there are too many incompatibiliti es and a lot of work has been
already done in C. So I have to find another way to call a C++
function through my C code.

In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.
If you mean "in general" to mean "in the normal case where extern "C" is
not used", you are correct. However, I am confident that you know C++
provides that mechanism to disable function name mangling, so your
answer is deliberately misleading. You'd have been better off simply
declaring it OT, as this entire thread has been from the beginning.

S
Nov 7 '08 #16
Flash Gordon wrote:
CBFalconer wrote, On 06/11/08 21:54:

<snip>
>describes the access to the C code required. In general C++ can
access C code, but C cannot access C++ code.

Remember, the languages are different.

Remember, it has been pointed out to you many times and with
references that you are WRONG. C++ defines how C code can call
C++ code and you have been shown how it works. Since the theory
says it should work and practice shows that it works why keep
insisting that it does not? Neither theory nor practice are on
your side.
I sent some answers, but something has fouled, and they are not
here.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Nov 7 '08 #17
Ian Collins <ia******@hotma il.comwrites:
abasili wrote:
>Hi everyone, I'm trying to compile a C++ function and then call it from
a C program.
Since Google is my friend I've ended up to this link which seems very
clear:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

Unfortunatel y it does not work.

It does if you follow all the points. The first two are often overlooked.
Some would say it's impossible. If you are building the final
executable (main and linking) with a C++ compiler, then they
might say that you are building a C++ program that calls C
functions. However, what was required was instead a C program
which calls C++ functions. If you're careful to avoid anything
too C++-like, then C++ can be incorporated into C, but then
you're hamstrung a bit with what you can do in C++, and what's
the point. So I'd say stick C into C++, not the other way round.

Phil
--
We must respect the other fellow's religion, but only in the sense and to the
extent that we respect his theory that his wife is beautiful and his children
smart. -- Henry Louis Mencken (1880-1956), American editor and critic
Nov 7 '08 #18
CBFalconer wrote, On 06/11/08 22:07:
Pawel Dziepak wrote:
>CBFalconer wrote:
>>In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.
7.5/3 states that: "Every implementation shall provide for linkage
to functions written in the C programming language" by allowing
things like extern "C"
7.5/6 tells us that is is possible to define function with C-style
linkage in C++ code.
7.5/9 informs us that implementation can allow to link objects from
other languages to C++ and from C++ to other languages.

Concluding, C++ standard gives possibility to link C++ functions
from C code (with some naming restrictions).

You are quoting from, I believe, the C++ standard.
Yes he is, because that is what defines how to do it.
My point is
that C cannot call to C++ code, but C++ can call C code.
Which is EXACTLY what is wrong.
The idea
is to prepare C object code and allow the C++ system to call it
correctly, by using the extern "C" identification _in the header
file for the C code_.
1) You don't have to modify the C headers

extern "C" {
#include "c-header"
}

2) You have already been shown how to use the mechanisms defined in C++
to call C++ from C. These mechanisms to not violate the C standard any
more than any other library being callable from C does.
The C++ standard is meaningless in c.l.c. Here, we use the C std.
You can tell people that if they want to discuss it to go to
comp.lang.c++, however telling people something that is just plain WRONG
rather than redirecting people where you do not know any better is what
the trolls do. If you don't know about something, either redirect people
to an appropriate group or don't say anything.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 7 '08 #19
Keith Thompson wrote, On 07/11/08 02:49:
James Kuyper <ja*********@ve rizon.netwrites :
>Flash Gordon wrote:
>>CBFalconer wrote, On 06/11/08 21:54:
If you're responding to what he wrote at that time, why don't you post
it as a response to the message in which he actually wrote it, on the
thread in which he wrote it? It's a little confusing to post it as a
response to a message I wrote, in a thread where he's said some
incorrect things about C/C++ linking, but he hasn't actually said
these particular incorrect things (at least, not yet):
>>>describes the access to the C code required. In general C++ can
access C code, but C cannot access C++ code.

James, either I'm confused or you are.
You are not confused, James is.

<snip>
Flash's article was a direct followup to CBFalconer's article, and
CBFalconer did write exactly the quoted incorrect words:

describes the access to the C code required. In general C++ can
access C code, but C cannot access C++ code.

Remember, the languages are different.

Did the "06/11/08" date format throw you off? It's DD/MM/YY, not
MM/DD/YY -- i.e., it's today, not 5 months ago. (MM/DD/YY is more
common in the US, DD/MM/YY in the UK; that's why I use YYYY-MM-DD
whenever possible.)
Yes, I should probably look at how to change it.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 7 '08 #20

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

Similar topics

1
2591
by: Jeff Hagelberg | last post by:
I'm trying to create a python module which can be used by a python interpreter embedded inside a fortran program I have. To do this, I first created python wrappers for all the functions in my fortran program using f2py. I then start an embedded python interpreter in c code which I link against the fortran program. I invoke the fortran program with a filename containing python code. This file is passed to the c code which passes it on...
7
5127
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting normal functions in header files results in multiple definition errors even when include guards are used. -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com Mozilla:...
20
3245
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
1
6099
by: Venky | last post by:
I'm compiling a C program that is using Interbase 6.0 APIS. Getting the following errors at the time of linking. Linking test.exe: Linker Warning: No module definition file specified: using defaults Linker Error: Undefined symbol isc_detach_database in module TEST.C ..... ..... If I set the option not to generate the underscore (function prefixed with "_") I get the following errors. Interbase 6.0 APIs resolved
1
313
by: Peetah_junkmail | last post by:
Hi, I'm not sure this is completely a C related question since it's more about linking problems, so don't hesitate to redirect me to a more appropriate NG. I have a set of useful functions (let's call this SUF) that facilitates my life when coding, such as memory allocation wrappers or generic string functions. I have a project composed of a shared library (SL) and a main program (MP) using SL. For some reasons, I don't want to make a...
3
3529
by: Kevin Burton | last post by:
I am trying to use managed C++ but I am getting the following link errors: Metadata file 'D:\Projects\Visa\AddressVerification\AddressVerificat ionTest\bin\Debug\AddressVerificationInterface.dll' could not be found AddressVerificationInterface error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int)" (??2@$$FYAPAXI@Z)
3
4376
by: walkeraj | last post by:
I'm trying to compile an open source game called IVAN , and I'm able to compile it from a makefile, but not from an IDE. I have attempted to recreate the way the makefile compiles the project as closely as possible, yet I'm getting odd linking errors. I think I've traced it to some kind of problem with the SDL libraries it uses, yet I can't explain why it will compile from the makefile and not from the IDE. I have made sure that the ide...
6
2494
by: Joe.pHsiao | last post by:
Hi, I tried to link a C program to a library which is written by me in C+ +. I read some posts about linking a C program to C++ libraries. It seems doable by adding extern "C" to the C++ head file and to the function body modifier. However, in my test, it still doesn't work. The linking error messages are like undefined reference for new operator, and undefined reference from dequeue.tcc. ( sorry I don't know how to copy lines from...
10
2201
kiseitai2
by: kiseitai2 | last post by:
Hi everyone. My problem is tha Dev-C++ compiles perfectly fine but it fails in the linking process. I receive the linker error "multiple definitions of"; howerver, I modified the names of the functions in three of the files, checked my code to make sure I wasn't redefining my functions, changed code back and forth from .cpp to .h, and even excluded some files from the project and included them manually. Also, I even recopied the lines of code...
0
9594
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
10595
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
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
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
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.