Supposing you have the following declaration in a header file:
-- head.h --
struct s {
int c;
char v;
#if defined(__cplus plus)
s();
s(double);
method1(int);
method2(float);
#endif
};
and then two sources,
-- cppsrc.cc --
#include "head.h"
extern "C" {
s *create(void)
{
s *res = new s(1.0);
res->method1(4);
return res;
}
-- csrc.c --
#include "head.h"
extern struct s *create(void);
int f()
{
struct s* val = create();
val->v = 'd';
}
Is this legal/portable?
Thanks in advance,
Vincenzo 13 1980
Vincezo Ciaschini wrote: Supposing you have the following declaration in a header file: -- head.h --
struct s { int c; char v; #if defined(__cplus plus) s(); s(double); method1(int); method2(float); #endif };
and then two sources,
-- cppsrc.cc -- #include "head.h"
extern "C" { s *create(void) { s *res = new s(1.0); res->method1(4); return res; }
-- csrc.c -- #include "head.h"
extern struct s *create(void);
int f() { struct s* val = create(); val->v = 'd'; }
Is this legal/portable?
I have no definite answer to this question. I believe, though, that
it would be implementation-specific. However, regardless of what I
believe, I'd like to know *why* would you want to do something like
that. Why do you feel the need to have both C and C++ translation units
in the same program?
V
Vincezo Ciaschini wrote: Supposing you have the following declaration in a header file: -- head.h --
struct s { int c; char v; #if defined(__cplus plus) s(); s(double); method1(int); method2(float); #endif };
and then two sources,
-- cppsrc.cc -- #include "head.h"
extern "C" { s *create(void) { s *res = new s(1.0); res->method1(4); return res; }
-- csrc.c -- #include "head.h"
extern struct s *create(void);
int f() { struct s* val = create(); val->v = 'd'; }
Is this legal/portable?
I don't think so. I believe it violates the One Definition Rule. But
I'm not sure if this applies when mixing C and C++.
-shez-
Victor Bazarov wrote: Vincezo Ciaschini wrote:
Supposing you have the following declaration in a header file: -- head.h --
struct s { int c; char v; #if defined(__cplus plus) s(); s(double); method1(int); method2(float); #endif };
and then two sources,
-- cppsrc.cc -- #include "head.h"
extern "C" { s *create(void) { s *res = new s(1.0); res->method1(4); return res; }
-- csrc.c -- #include "head.h"
extern struct s *create(void);
int f() { struct s* val = create(); val->v = 'd'; }
Is this legal/portable?
I have no definite answer to this question. I believe, though, that it would be implementation-specific. However, regardless of what I believe, I'd like to know *why* would you want to do something like that. Why do you feel the need to have both C and C++ translation units in the same program?
Because they would not be in the same program. The cppsrc.cc would be
in a dynamic library that will be linked to the program containing
csrc.c. It is an effort to provide a C interface to a C++ library.
Bye,
Vincenzo V
Shezan Baig wrote: Vincezo Ciaschini wrote:
Supposing you have the following declaration in a header file: -- head.h --
struct s { int c; char v; #if defined(__cplus plus) s(); s(double); method1(int); method2(float); #endif };
and then two sources,
-- cppsrc.cc -- #include "head.h"
extern "C" { s *create(void) { s *res = new s(1.0); res->method1(4); return res; }
-- csrc.c -- #include "head.h"
extern struct s *create(void);
int f() { struct s* val = create(); val->v = 'd'; }
Is this legal/portable?
I don't think so. I believe it violates the One Definition Rule. But I'm not sure if this applies when mixing C and C++.
My doubt exactly, especially considering that the two definition are
identical with respect to the included data.
It seems reasonable to me that, if the data definition part is
identical, and there is no virtual/RTTI on the C++ part, that the memory
layout should be identical. However, I am not certain about this.
Also, the cppsrc.cc code will be in a library linked by the csrc.c (and
a main() function) code.
Bye,
Vincenzo -shez-
Vincezo Ciaschini wrote: Victor Bazarov wrote: [...] I'd like to know *why* would you want to do something like that. Why do you feel the need to have both C and C++ translation units in the same program? Because they would not be in the same program. The cppsrc.cc would be in a dynamic library that will be linked to the program containing csrc.c.
Read your own statement again. If it is _linked_, it's in the same
program. By definition.
It is an effort to provide a C interface to a C++ library.
I don't see any need to have a C struct created in C++ code and returned
by a C function. It would be best to de-couple them altogether and use
some kind of "handle" to identify your C++ object inside your C++ space.
Your library is the only thing that should know about the inner workings
of that object. The outside world should use the C interface to do all
the processing. Passing the "handle" back and forth is the only answer
AFAIK.
<offtopic> But is it really needed to have C interface? How many
customers do you have that use C versus C++ bindings? Any at all?
Never mind. Marketing decisions are not necessarily based on common
sense. And don't tell me that having a C interface is not a marketing
decision.
</offtopic>
V
Vincezo Ciaschini wrote: It seems reasonable to me that, if the data definition part is identical, and there is no virtual/RTTI on the C++ part, that the memory layout should be identical. However, I am not certain about this.
I think is unreasonable to worry about that. Just write a class that derives
from or contains the C style struct and use it from C++.
--
Salu2
Victor Bazarov wrote: <offtopic> But is it really needed to have C interface? How many customers do you have that use C versus C++ bindings? Any at all? Never mind. Marketing decisions are not necessarily based on common sense. And don't tell me that having a C interface is not a marketing decision. </offtopic>
Sometimes it is just a case that certain developers at the company who
are "afraid" to use C++. Sounds silly, yes. But we all cannot control
everybody :(
-shez-
Victor Bazarov wrote: And don't tell me that having a C interface is not a marketing decision.
It's often a sensible engineering decision. C interfaces are far less
dependent on the compiler you're using than C++ interfaces, so a shared
library with a C interface is more broadly useful than a shared library
with a C++ interface. Having a well-defined C++ ABI for the platform
you're targeting makes this less of a problem, to the extent that the
ABI really is well-defined.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Vincezo Ciaschini wrote: Supposing you have the following declaration in a header file: -- head.h --
.... Is this legal/portable?
Maybe. But the classic way to provide a C interface for a C++ program
goes like this:
// somefile.h ----------------------
struct s; // declaration
extern "C" {
struct s *create(void);
void destroy (struct s* x);
void method1 (struct s* x, int);
// ...
}
// somefile.cpp --------------------
struct s {
s();
s(double);
method1(int);
method2(float);
private:
int c;
char v;
};
s *create(void)
{
struct s *res = new s(1.0);
res->method1(4);
return res;
}
void destroy (struct s* x) { ... }
void method1 (struct s* x, int) { ... }
// the C user ------------------------
int main() {
struct s* val = create();
method1 (s, 33);
destroy (s);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Cgacc20 |
last post by:
I have a c struct from old code that cannot be modified and I am trying to
write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a library and I
wanted to keep my new class transparent and compatible with those functions.
That is, when using the code, I should be able to do either:
oldVector xyz;
function( &xyz );
myVector xyz;
function( &xyz );
|
by: Arthur J. O'Dwyer |
last post by:
As far as I know, C89/C90 did not contain the
now-standard offsetof() macro.
Did C89 mandate that structs had to have a consistent
layout? For example, consider the typical layout of
the following structure:
struct weird
{
int x; /* sizeof(int)==4 here */
|
by: Kobu |
last post by:
In embedded systems (programmed in C), often times structure
declarations are used to group together several status/control/data
registers of external hardware (or even internal registers). The
example below (from GBD's THE C BOOK) uses this. Is this a portable
method? .
/*
|
by: Al Bahr |
last post by:
H
I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated.
bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat..
' <summary
' Palette entry structur
' </summary
bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat..
|
by: Bonj |
last post by:
I almost understand TSTs, to the point where I just need to know the answer
to this:
When making a TST (in C++) that will have as its leaf nodes words that make
up SQL language and an categorising identifier for each one, and each layer
of the tree will represent comparison of a further letter within the search
string, what will happen when a particular node is a leaf node itself, but
also has leaf nodes of its own? i.e. specifically, as...
| |
by: Jake Thompson |
last post by:
Hello
I have the following defined structure
struct cm8linkstruc
{
char *type; /* type of item*/
char *desc; /* description of item */
char *item_increment; /* increment value for item in
folder */
|
by: lawtrevor |
last post by:
I have a question regarding the use of free() based on some code I need
to decipher:
{
struct A {<A fields>};
struct B {<A fields+ <Bfields>};
typedef struct A Aobj;
typedef struct B Bobj;
|
by: kyle york |
last post by:
Greetings,
Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what's the point?
Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific...
|
by: hugo.arregui |
last post by:
Hi!
I have two struts like that:
struct {
int num;
int num2;
struct b arrayOfB;
} a;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |