Re: linking C++ functions in a C program
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 07 Nov 2008 18:07:00 -0500
James Kuyper wrote:
CBFalconer wrote: Various incorrect things..... snip ...
Rather than repeat my previous failed attempts to get a straight
answer out of you about that question, I've written a program that
contains one function named foo() which is compiled in C, and is
therefore unambiguously a C function. It contains another function
named baz() compiled in C++ that has "C++" language linkage, so it
is unambiguously a C++ function. foo() calls bar() which calls
baz(). Therefore, no matter how you classify bar(), this program
contains a C function calling a C++ function.
foobar.h:
========================
#ifndef FOOBAR_H
#define FOOBAR_H
#ifdef __cplusplus
extern "C" {
#endif
void foo(int);
void bar(int);
#ifdef __cplusplus
}
#endif
#endif
foo.c:
===================================
#include <stdio.h>
#include "foobar.h"
void foo(int i) {
puts("About to call bar()");
bar(i);
puts("Finished calling bar()");
}
main.C
==================================================
#include <iostream>
#include "foobar.h"
int main() {
foo(42);
}
static void baz(int i) {
std::cout << "the number is:" << i << std::endl;
}
void bar(int i) {
baz(i);
}
I concede my posts have not been accurate. I still don't think C
calling C++ is convenient, accurate, and really useful, although
possible. In the above:
foobar.h sets up definitions of foo and bar that requires them to
be called without name adornment The actual code is implemented in
the C++ file (.cpp would be more descriptive than .C, IMO). I
guess the compiler can generate two references to foo, one adorned,
and one not adorned, although that is an unnecessary complication.
Some confusion: Note that the code for bar is compiled with a C++
compiler. Thus it must follow C++ rules, for such things as
reserved names, size of char constants, etc. It can thus make a
great difference if bar is moved to a .c file, #includes
"foobar.h", and totally removed from main.C. To me, this is a
cleaner organization, and ensures that C code is compiled as C, and
C++ code is compiled as C++. Conceded, it will prevent using
std::cout etc.
I think I have avoided confusing my biases with facts here :-)
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
.
- Follow-Ups:
- Re: linking C++ functions in a C program
- From: Stephen Sprunk
- Re: linking C++ functions in a C program
- From: Ian Collins
- Re: linking C++ functions in a C program
- From: jameskuyper
- Re: linking C++ functions in a C program
- References:
- linking C++ functions in a C program
- From: abasili
- Re: linking C++ functions in a C program
- From: Ian Collins
- Re: linking C++ functions in a C program
- From: abasili
- Re: linking C++ functions in a C program
- From: CBFalconer
- Re: linking C++ functions in a C program
- From: jameskuyper
- Re: linking C++ functions in a C program
- From: CBFalconer
- Re: linking C++ functions in a C program
- From: James Kuyper
- linking C++ functions in a C program
- Prev by Date: Re: Shared pointers???
- Next by Date: Re: Shared pointers???
- Previous by thread: Re: linking C++ functions in a C program
- Next by thread: Re: linking C++ functions in a C program
- Index(es):
Relevant Pages
|
Loading