473,513 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.cpp vs .c

I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.
It doesn't do any good to include a C module if I'm gonna stomp all over it
with different rules. How can I get around this? furunculus
Jun 22 '06 #1
27 2076
Your Uncle wrote:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.
It doesn't do any good to include a C module if I'm gonna stomp all over it
with different rules. How can I get around this? furunculus


What exactly do you want to get around? The name of the file is
irrelevant-- what matters is which compiler is being invoked (C or C++,
presumably). Most compilers will infer the language from the filename
but will also offer some mechanism to override this. (E.g., with gcc
you can use the -x flag.)
Jun 22 '06 #2

Your Uncle wrote:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.

This statement is somewhat puzzling to me. What's the impetus behind
removing the cast and expecting the 'other' to compile? I could be
mistaken but if memory serves the csat is necessary.

Jun 22 '06 #3
ma740988 wrote:
Your Uncle wrote:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.

This statement is somewhat puzzling to me. What's the impetus behind
removing the cast and expecting the 'other' to compile? I could be
mistaken but if memory serves the csat is necessary.


In C++, implicit casts from void pointers are not allowed for type
safety. In C, the implicit cast is allowed.

Cheers! --M

Jun 22 '06 #4
Your Uncle schrieb:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.
It doesn't do any good to include a C module if I'm gonna stomp all over it
with different rules. How can I get around this? furunculus


In C, a void* pointer will implicitly converted to any pointer type, so
you don't have to cast malloc's return value. In C++, you have to cast,
or better use new.

Thomas
Jun 22 '06 #5

mlimber wrote:
[...]
In C++, implicit casts from void pointers are not allowed for type
safety. In C, the implicit cast is allowed.

Thanks for the correction.. I tend to forget these distinctions.

Jun 22 '06 #6

"ma740988" <ma******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

mlimber wrote:
[...]
In C++, implicit casts from void pointers are not allowed for type
safety. In C, the implicit cast is allowed.

Thanks for the correction.. I tend to forget these distinctions.

And they let you live? The difference in the cast one language to the other
is how I'm going to know whether the compiler thinks __cplusplus is defined.
I was having delusions of grandeur thinking that:
# ifdef __cplusplus
extern "C" {
# endif
int * get_an_int(void);
# ifdef __cplusplus
}
# endif
was going to be my ticket out. fu


Jun 22 '06 #7

Your Uncle wrote:
Thanks for the correction.. I tend to forget these distinctions. And they let you live?

Well not for long ...

The difference in the cast one language to the other is how I'm going to know whether the compiler thinks __cplusplus is defined.
I was having delusions of grandeur thinking that: Momentary insanity for me..
# ifdef __cplusplus
extern "C" {
# endif
int * get_an_int(void);
# ifdef __cplusplus
}
# endif
was going to be my ticket out. fu

You meant furunculus ?

Jun 22 '06 #8
On 2006-06-22, Your Uncle <in*****@crippled.net> wrote:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.
It doesn't do any good to include a C module if I'm gonna stomp all over it
with different rules. How can I get around this? furunculus


As annoying as it is not to implicitly cast from void *, why don't you just
use new and delete when compiling C++?

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 22 '06 #9

"Andrew Poelstra" <ap*******@localhost.localdomain> wrote in message
news:slrne9jvl8.h2c.ap*******@localhost.localdomai n...
On 2006-06-22, Your Uncle <in*****@crippled.net> wrote:
I have 2 different files that differ only in filetype. One is Text1.c ,
the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.
It doesn't do any good to include a C module if I'm gonna stomp all over
it
with different rules. How can I get around this? furunculus


As annoying as it is not to implicitly cast from void *, why don't you
just
use new and delete when compiling C++?

Because I don't want to shitcan everything I did on the other end of the
hallway. I want c++ to talk to its brother underneath the OS. cheers,
furunculus
Jun 22 '06 #10
Your Uncle wrote:
Because I don't want to shitcan everything I did on the other end of the
hallway. I want c++ to talk to its brother underneath the OS.


Writing C++-ready C is trivial. Just add extra typecasts, and watch your
types and name mangling. So leave (int*) on the C side.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 22 '06 #11
On 2006-06-22, Phlip <ph******@yahoo.com> wrote:
Your Uncle wrote:
Because I don't want to shitcan everything I did on the other end of the
hallway. I want c++ to talk to its brother underneath the OS.


Writing C++-ready C is trivial. Just add extra typecasts, and watch your
types and name mangling. So leave (int*) on the C side.


Then that undermines the safety of C. If <stdlib.h> is unincluded, typecasting
will hide that error, because C will assume malloc returns int. This invokes
undefined behavior.

You understand that C and C++ are different languages, right? While in this
case you can hack code that will compile as either, this is basically like
trying to put Javascript through a C# compiler.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 22 '06 #12

"Phlip" <ph******@yahoo.com> wrote in message
news:qU********************@newssvr29.news.prodigy .net...
Your Uncle wrote:
Because I don't want to shitcan everything I did on the other end of the
hallway. I want c++ to talk to its brother underneath the OS.


Writing C++-ready C is trivial. Just add extra typecasts, and watch your
types and name mangling. So leave (int*) on the C side.

With regard to the malloc, I don't think typecasts is an issue (I'm not
saying that because I actually know). With the return types on the malloc,
I thought we decided that was unwinnable. NameMangling is not a productive
topic:-)

Is your idea here though to use an executable to ultimately convert the
source? ciao, f
Jun 22 '06 #13
Your Uncle posted:
I have 2 different files that differ only in filetype. One is Text1.c
, the other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one
won't. It doesn't do any good to include a C module if I'm gonna stomp
all over it with different rules. How can I get around this?
furunculus


Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );
Jun 22 '06 #14

"Frederick Gotham" <fg*******@SPAM.com> wrote in message
news:Xn***************************@195.188.240.200 ...
Your Uncle posted:
I have 2 different files that differ only in filetype. One is Text1.c
, the other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one
won't. It doesn't do any good to include a C module if I'm gonna stomp
all over it with different rules. How can I get around this?
furunculus


Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );

If those are my only scenarios, then I think to misunderstand the nature of
a "module." Clearly, anyone who wants to keep from turning cross-eyed keeps
the two separate. Oh well. Thanks for your reply. fu
Jun 22 '06 #15
Frederick Gotham wrote:
Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")

[snip]

Yes, if you're writing C++, you generally should use new rather than
malloc (see
http://www.parashift.com/c++-faq-lit....html#faq-16.4 for
reasons why), but more to the point, you should avoid using new when
possible because it is error prone. If you do need to use it (e.g. for
creating an object that will be used polymorphically), always attach
the result of "new" to a smart pointer that will automatically release
it (you can also use some smart pointers with malloc/free), and prefer
std::vector over of dynamically allocated arrays of objects (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1).

C++ allows several programming paradigms (old-style C being one of
them), but the advice above represents the generally accepted "best
practice" for C++.

Cheers! --M

Jun 22 '06 #16
Frederick Gotham <fg*******@spam.com> wrote:
Your Uncle posted:
I have 2 different files that differ only in filetype. One is Text1.c
, the other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one
won't. It doesn't do any good to include a C module if I'm gonna stomp
all over it with different rules. How can I get around this?
furunculus


Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );


In that case, would this work?

#ifdef __cplusplus
pt = static_cast<int*>(malloc(sizeof *pt));
#else
pt = malloc(sizeof *pt);
#endif

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 22 '06 #17

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e7**********@news-int2.gatech.edu...
Frederick Gotham <fg*******@spam.com> wrote:
Your Uncle posted:
I have 2 different files that differ only in filetype. One is Text1.c
, the other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one
won't. It doesn't do any good to include a C module if I'm gonna stomp
all over it with different rules. How can I get around this?
furunculus


Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );


In that case, would this work?

#ifdef __cplusplus
pt = static_cast<int*>(malloc(sizeof *pt));
#else
pt = malloc(sizeof *pt);
#endif

You can ifdef around the differences, when you've identified them and lost a
day in development doing so. Mixing the two is simply a recipe for a flat
tire. cheers, f
Jun 22 '06 #18

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e7**********@news-int2.gatech.edu...
Frederick Gotham <fg*******@spam.com> wrote:
Your Uncle posted:
I have 2 different files that differ only in filetype. One is Text1.c
, the other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one
won't. It doesn't do any good to include a C module if I'm gonna stomp
all over it with different rules. How can I get around this?
furunculus

Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );


In that case, would this work?

#ifdef __cplusplus
pt = static_cast<int*>(malloc(sizeof *pt));
#else
pt = malloc(sizeof *pt);
#endif

You can ifdef around the differences, when you've identified them and lost
a day in development doing so. Mixing the two is simply a recipe for a
flat tire. cheers, f


You're screwed then. Better switch to Basic.

Seriously, if you want to be able to compile C code in a C++ compiler, you
are going to have to work around these things. Your other option is not to
compile C code as C++ code then you don't have to worry about it.

Yes, it is going to take you time to find the problems and fix them. It
would be better, IMO, to just convert it to C++ code and don't try to
compile it as C code anymore then you won't have to do these types of
things. You get what you pay for, in other words.
Jun 23 '06 #19

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:jC*****************@fe05.lga...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e7**********@news-int2.gatech.edu...
Frederick Gotham <fg*******@spam.com> wrote:
Your Uncle posted:
> I have 2 different files that differ only in filetype. One is Text1.c
> , the other is Text1.cpp . If therein I have this function:
>
> int * get_an_int(void)
> {
> int t;
> int * pt;
> pt = (int *)malloc (sizeof*pt);
> t = 41;
> pt[0] = t;
> return pt;
> }
> Then one will compile, whereas if I remove the cast, the other one
> won't. It doesn't do any good to include a C module if I'm gonna stomp
> all over it with different rules. How can I get around this?
> furunculus

Three scenarios.

Scenario 1:
You're writing C++ code. Name the file "code.cpp" and put the
following in it:

pt = static_cast<int*>( malloc(sizeof *pt) );

(Also consider using "new")
Scenario 2:
You're writing C code. Name the file "code.c" and put the following
in it:

pt = malloc( sizeof *pt );
Scenario 3:
You're writing code which you wish to compile successfully as either
C or C++ (not a good idea in my opinion). This is where you'd use:

pt = (int*)malloc( sizeof *pt );

In that case, would this work?

#ifdef __cplusplus
pt = static_cast<int*>(malloc(sizeof *pt));
#else
pt = malloc(sizeof *pt);
#endif You can ifdef around the differences, when you've identified them and
lost a day in development doing so. Mixing the two is simply a recipe
for a flat tire. cheers, f


You're screwed then. Better switch to Basic.

As a twelve year old, I could lprint(something) and have something as a hard
copy. Trying to get information from C to a printer has now become an
odyssey, one that I'm not even going to be able to do without MFC.
Seriously, if you want to be able to compile C code in a C++ compiler, you
are going to have to work around these things. Your other option is not
to compile C code as C++ code then you don't have to worry about it.

Yes, it is going to take you time to find the problems and fix them. It
would be better, IMO, to just convert it to C++ code and don't try to
compile it as C code anymore then you won't have to do these types of
things. You get what you pay for, in other words.

I would think that one could wrap function calls to C functions so that a
c++ compiler would compile them as C but link as c++. cheers, f
Jun 23 '06 #20
I V
On Fri, 23 Jun 2006 17:59:00 -0400, Your Uncle wrote:
I would think that one could wrap function calls to C functions so that a
c++ compiler would compile them as C but link as c++. cheers, f


Can you not just compile the C functions with your C compiler, your C++
functions with your C++ compiler, and link the results?
Jun 23 '06 #21
Your Uncle wrote:

I would think that one could wrap function calls to C functions so that a
c++ compiler would compile them as C but link as c++. cheers, f

You just use extern "C" to wrap your function prototypes in their header.

--
Ian Collins.
Jun 23 '06 #22

"Ian Collins" <ia******@hotmail.com> wrote in message
news:4g*************@individual.net...
Your Uncle wrote:

I would think that one could wrap function calls to C functions so that a
c++ compiler would compile them as C but link as c++. cheers, f

You just use extern "C" to wrap your function prototypes in their header.

# ifdef __cplusplus
extern "C" {
# endif
int * get_an_int(void);
# ifdef __cplusplus
}
# endif
That's what I thought! When I renamed the source file from .c to .cpp, my
compiler asked me to recast a malloc in the function, which is trouble. The
opinion at the other end of the hallway is that it's a c++ issue. cheers,
fu
Jun 24 '06 #23
I V
On Fri, 23 Jun 2006 21:09:44 -0400, Your Uncle wrote:
That's what I thought! When I renamed the source file from .c to .cpp, my
compiler asked me to recast a malloc in the function, which is trouble. The


Why are you renaming the source file from .c to .cpp ?

Jun 24 '06 #24
On 21 Jun 2006 18:31:21 -0700, "mlimber" <ml*****@gmail.com> wrote in
comp.lang.c++:
ma740988 wrote:
Your Uncle wrote:
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:

int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won't.

This statement is somewhat puzzling to me. What's the impetus behind
removing the cast and expecting the 'other' to compile? I could be
mistaken but if memory serves the csat is necessary.


In C++, implicit casts from void pointers are not allowed for type
safety. In C, the implicit cast is allowed.


Time for the terminology police!

There is no such thing as an "implicit cast" in C or C++. Both
languages have conversions. Some conversions are automatic, some will
not be performed automatically. The use of a cast operator performs
an "explicit conversion".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
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
Jun 24 '06 #25

"I V" <wr******@gmail.com> wrote in message
news:pa****************************@gmail.com...
On Fri, 23 Jun 2006 21:09:44 -0400, Your Uncle wrote:
That's what I thought! When I renamed the source file from .c to .cpp,
my
compiler asked me to recast a malloc in the function, which is trouble.
The


Why are you renaming the source file from .c to .cpp ?

That's a little bit of a story. The source contains a simple function that
works wonderfully in C, and as such, would only coincidentally work at all
compiled as c++. The wrapper is intended to test the integrity of the
wrapper, and so far, has fallen short. On my implementation, and it's not
on a backwater OS with a vendor you wouldn't recognize, assumes that a .cpp
file is to be compiled as c++. When the compiler sees the functions
wrapped with extern "C", I want it to treat that enclosed function as C and
link it so that I'm going to be able to have an app with some functions
compiled in C, others, yet unwritten, in c++, and the whole enchilada linked
successfully. I wouldn't have thought it profitable for the languages to
touch below the OS, but then I saw this wrapper on Cbfalconer's famous
ggets(), and thought, maybe I could do something with this. So far I've
only consumed bandwidth. cheers, f
Jun 24 '06 #26
I V
On Sat, 24 Jun 2006 11:32:43 -0400, Your Uncle wrote:
"I V" <wr******@gmail.com> wrote in message
news:pa****************************@gmail.com...
Why are you renaming the source file from .c to .cpp ?


That's a little bit of a story. The source contains a simple function
that works wonderfully in C, and as such, would only coincidentally work
at all compiled as c++. The wrapper is intended to test the integrity
of the wrapper, and so far, has fallen short. On my implementation, and
it's not on a backwater OS with a vendor you wouldn't recognize, assumes
that a .cpp file is to be compiled as c++. When the compiler sees the
functions wrapped with extern "C", I want it to treat that enclosed
function as C and link it so that I'm going to be able to have an app
with some functions compiled in C, others, yet unwritten, in c++, and
the whole enchilada linked successfully. I wouldn't have thought it
profitable for the languages to touch below the OS, but then I saw this
wrapper on Cbfalconer's famous ggets(), and thought, maybe I could do
something with this. So far I've only consumed bandwidth. cheers, f


Unfortunately, "extern C" doesn't do what you want it to do - it doesn't
compile the functions as C, which I guess you have seen. However, what it
does do is tell the C++ compiler that functions with these names _will
have been compiled by a C compiler_ elsewhere. Then, you should
be able to link the objects produced by the C compiler, with the objects
produced by the C++ compiler. So you shouldn't have to rename your .c
files to .cpp files to use them in your C++ program. There's more about
this in the FAQ:

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

Have you tried compiling your .c files with the .c, and linking them with
your .cpp files? You do something like this:

--- cfunctions.c ---

int my_c_function()
{
/* Do some C stuff here */
}

--- cfunctions.h ---
#ifdef __cplusplus
extern "C" {
#endif

int my_c_function();

#ifdef __cplusplus
}
#endif

--- cppfunction.cpp ---

#include "cfunctions.h"

int my_cpp_function()
{
int i = my_c_function();
// Do some C++ stuff here
}

---

And you would compile them with:

# Compile the C functions with a C compiler
cc -c cfunctions.c -o cfunctions.o

# Compile the C++ functions with a C++ compiler
c++ -c cppfunctions.cpp -o cppfunctions.o

# Use the C++ compiler to link the C and C++ functions together into a
# program
c++ -o program cfunctions.o cppfunctions.o


Jun 24 '06 #27

"I V" <wr******@gmail.com> wrote in message
news:pa***************************@gmail.com...
On Sat, 24 Jun 2006 11:32:43 -0400, Your Uncle wrote:
"I V" <wr******@gmail.com> wrote in message
news:pa****************************@gmail.com...
Why are you renaming the source file from .c to .cpp ?


That's a little bit of a story. The source contains a simple function
that works wonderfully in C, and as such, would only coincidentally work
at all compiled as c++. The wrapper is intended to test the integrity
of the wrapper, and so far, has fallen short. On my implementation, and
it's not on a backwater OS with a vendor you wouldn't recognize, assumes
that a .cpp file is to be compiled as c++. When the compiler sees the
functions wrapped with extern "C", I want it to treat that enclosed
function as C and link it so that I'm going to be able to have an app
with some functions compiled in C, others, yet unwritten, in c++, and
the whole enchilada linked successfully. I wouldn't have thought it
profitable for the languages to touch below the OS, but then I saw this
wrapper on Cbfalconer's famous ggets(), and thought, maybe I could do
something with this. So far I've only consumed bandwidth. cheers, f


Unfortunately, "extern C" doesn't do what you want it to do - it doesn't
compile the functions as C, which I guess you have seen. However, what it
does do is tell the C++ compiler that functions with these names _will
have been compiled by a C compiler_ elsewhere. Then, you should
be able to link the objects produced by the C compiler, with the objects
produced by the C++ compiler. So you shouldn't have to rename your .c
files to .cpp files to use them in your C++ program. There's more about
this in the FAQ:

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

Have you tried compiling your .c files with the .c, and linking them with
your .cpp files? You do something like this:

--- cfunctions.c ---

int my_c_function()
{
/* Do some C stuff here */
}

--- cfunctions.h ---
#ifdef __cplusplus
extern "C" {
#endif

int my_c_function();

#ifdef __cplusplus
}
#endif

--- cppfunction.cpp ---

#include "cfunctions.h"

int my_cpp_function()
{
int i = my_c_function();
// Do some C++ stuff here
}

---

And you would compile them with:

# Compile the C functions with a C compiler
cc -c cfunctions.c -o cfunctions.o

# Compile the C++ functions with a C++ compiler
c++ -c cppfunctions.cpp -o cppfunctions.o

# Use the C++ compiler to link the C and C++ functions together into a
# program
c++ -o program cfunctions.o cppfunctions.o

You're on the right track, cowboy. Do you know how this information would
look for the Evil Empire version, where, say .o is .obj ? I took a pain
killer and think I'm getting a little loopy because I wanted to tell a total
starnger with a kickstand that I loved him. Yeah, I'm stoned. ciao, f
Jun 24 '06 #28

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

Similar topics

3
11180
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
5775
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
22959
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
8436
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
8540
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
18217
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
6777
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
31348
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
23534
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
7265
marktang
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,...
0
7388
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,...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7111
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...
0
7539
tracyyun
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...
0
5692
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,...
0
4751
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...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
461
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...

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.