473,890 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile C Code With A CPP Compiler?

Hey all,

I'm working with some legacy C code and I would like to compile it as
a CPP file.

I get the following error message:

driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
assignment

Reading through the web I've come across vague references to the
'void' issue between C and C++, I don't know C++ well and would
appreciate any pointers or references which might help me out.

Thanks!

entropy
Nov 13 '05
52 6846
Dan Pop wrote:
"Peter Nilsson" <ai***@acay.com .au> writes:

.... snip ...

No C99 implementation will define __cplusplus, but AFAIK C90
implementations are not constrained in that regard.


Indeed. This is a last moment addition to the C99 standard
(missing from N869).


Perchance you have accumulated an informal list of those
additions. If so, I think many would be pleased to see it.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #21
In <3F************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
"Peter Nilsson" <ai***@acay.com .au> writes:

... snip ...
>
> No C99 implementation will define __cplusplus, but AFAIK C90
> implementations are not constrained in that regard.


Indeed. This is a last moment addition to the C99 standard
(missing from N869).


Perchance you have accumulated an informal list of those
additions. If so, I think many would be pleased to see it.


Well, it's in my head, only. Here are the changes I'm aware of:

From the last part of 6.2.6.2p2 on:

- the corresponding value with sign bit 0 is negated (sign
and magnitude);

- the sign bit has the value -(2**N) (two's complement);

- the sign bit has the value -(2**N - 1) (one's complement).

Which of these applies is implementation-defined, as is
whether the value with sign bit 1 and all value bits zero
(for the first two), or with sign bit and all value bits 1 (for
one's complement), is a trap representation or a normal value.
In the case of sign and magnitude and one's complement, if this
representation is a normal value it is called a negative zero.

3 If the implementation supports negative zeros, they shall be
generated only by:

- the &, |, ^, ~, <<, and >> operators with arguments that
produce such a value;

- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;

- compound assignment operators based on the above cases.

It is unspecified whether these cases actually generate a
negative zero or a normal zero, and whether a negative zero
becomes a normal zero when stored in an object.

4 If the implementation does not support negative zeros, the
behavior of the &, |, ^, ~, <<, and >> operators with arguments
that would produce such a value is undefined.

=============== =============== =============== =============== ============

6.3.1.3 Signed and unsigned integers

3 Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined
^^^^^^
or an implementation-defined signal is raised.
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^

=============== =============== =============== =============== ============

6.5.2.3p5 (The first statement in the N869 version is missing in the final
version, which turns implementation-defined behaviour into undefined
behaviour.)

5 One special guarantee is made in order to simplify the use of
unions: if a union contains several structures that share a
common initial sequence (see below), and if the union object
currently contains one of these structures, it is permitted to
inspect the common initial part of any of them anywhere that
a declaration of the complete type of the union is visible.
Two structures share a common initial sequence if corresponding
members have compatible types (and, for bit-fields, the same
widths) for a sequence of one or more initial members.

=============== =============== =============== =============== ============

6.10.8 Predefined macro names

5 The implementation shall not predefine the macro __cplusplus,
nor shall it define it in any standard header.

=============== =============== =============== =============== ============

7.18.1.1 Exact-width integer types

1 The typedef name intN_t designates a signed integer type with
width N, no padding bits, and a two's complement
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
representation. Thus, int8_t denotes a signed integer type with
^^^^^^^^^^^^^^^
a width of exactly 8 bits.

=============== =============== =============== =============== ============

7.20.4.4 The _Exit function

Synopsis

1 #include <stdlib.h>
void _Exit(int status);

Description

2 The _Exit function causes normal program termination to occur
and control to be returned to the host environment. No functions
registered by the atexit function or signal handlers registered
by the signal function are called. The status returned to the host
environment is determined in the same way as for the exit function
(7.20.4.3). Whether open streams with unwritten buffered data
are flushed, open streams are closed, or temporary files are
removed is implementation-defined.

Returns

3 The _Exit function cannot return to its caller.

=============== =============== =============== =============== ============

Any additions to the list are welcome.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #22
entropy123 wrote:
The project relies upon about 10 years worth of legacy 'C' code.
I've been working in C, and, after a few conversations,
I decided that C++ is a better longterm decision for the code.


Correct. The future of C is C++.
You should try to write C code which is compatible with C++.
For example, you should write:

#include <stdlib.h>

Nov 13 '05 #23
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
entropy123 wrote:
The project relies upon about 10 years worth of legacy 'C' code.
I've been working in C, and, after a few conversations,
I decided that C++ is a better longterm decision for the code.


Correct. The future of C is C++.
You should try to write C code which is compatible with C++.
For example, you should write:

#include <stdlib.h>

.
.
.

int* p = (int*)malloc(n* sizeof(int));

to convert the void* pointer returned by malloc
into a pointer to an int.


Ridiculous. If you want C++ code, then it's much better to write
C++ code then to try to make C-ish C++ code or C++-ish C
code. When you try to write code that compiles in both, you wind
up using idioms that are considered crappy by knowledgeable
coders of both languages. And there is absolutely no reason to
write C code like this, even if you do plan on moving to C++,
since C++ fully supports linking to C object files, so you can
keep your C code and still use it as-is from within C++ code.

In C, the way to avoid important errors when allocating memory is
to not cast malloc()'s return. In C++, the way to avoid important
errors when allocating memory is to use new (although, I have
used malloc() in situations where being able to use realloc() was
appropriate).

Not to mention that the #include <stdlib.h> above is deprecated
in C++, and may be removed in future versions of the language, so
it's not even forward-compatible, which is allegedly what you
were trying to achieve. And even in the current version of C++,
that method of inclusion will pollute the global namespace, which
is less-than-undesirable.

Conclusion: if you want C code, write good C code. If you want
C++ code, write good C++ code. You're only robbing yourself if
you try to write code that is mediocre for both.

-Micah
Nov 13 '05 #24
"E. Robert Tisdale" wrote:
Correct. The future of C is C++.

I see Trollsdale is at it again.

Brian Rodenborn
Nov 13 '05 #25
Micah Cowan wrote:
There is absolutely no reason to write C code like this
even if you do plan on moving to C++
since C++ fully supports linking to C object files, so you can
keep your C code and still use it as-is from within C++ code.


No!

There is *no* guarantee that you will be able to link
code compiled by a C compiler (or even another C++ compiler)
into a C++ program. The only thing specified by ANSI/ISO C++
is that the extern "C" mechanism will emit C "style" linkage
which means that the symbols left behind in the object file
will be the undecorated function names introduced by the programmer.

Nov 13 '05 #26
"E. Robert Tisdale" wrote:
entropy123 wrote:
The project relies upon about 10 years worth of legacy 'C' code.
I've been working in C, and, after a few conversations,
I decided that C++ is a better longterm decision for the code.


Correct. The future of C is C++.
You should try to write C code which is compatible with C++.
For example, you should write:

#include <stdlib.h>
.
.
int* p = (int*)malloc(n* sizeof(int));

to convert the void* pointer returned by malloc
into a pointer to an int.


Hogwash. You have just illustrated an excellent reason to write C
code for a C compiler, and C++ code for a C++ compiler. May the
camels nose penetrate your tent.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #27
E. Robert Tisdale wrote:
Micah Cowan wrote:
There is absolutely no reason to write C code like this
even if you do plan on moving to C++
since C++ fully supports linking to C object files, so you can
keep your C code and still use it as-is from within C++ code.
No!


Er, yes, actually.
There is *no* guarantee that you will be able to link
code compiled by a C compiler (or even another C++ compiler)
into a C++ program.
There is no guarantee that qsort won't use bubble-sort, either. Can you say
QoI?
The only thing specified by ANSI/ISO C++
is that the extern "C" mechanism will emit C "style" linkage
which means that the symbols left behind in the object file
will be the undecorated function names introduced by the programmer.


And the whole idea of that is to make it possible to link C code into C++
programs. They even /called/ the extension "C". It couldn't be much clearer
- except, of course, to a troll.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #28
E. Robert Tisdale wrote:
entropy123 wrote:
The project relies upon about 10 years worth of legacy 'C' code.
I've been working in C, and, after a few conversations,
I decided that C++ is a better longterm decision for the code.
Correct. The future of C is C++.


Incorrect. C++ is a different language, which exists separately. C's future
is C. C++ and C have already split, and have been heading in different
directions for many years.
You should try to write C code which is compatible with C++.
No, you really, really shouldn't.
For example, you should write:

#include <stdlib.h>

.
.
.

int* p = (int*)malloc(n* sizeof(int));
Typical Tisdale nonsense.

Quite apart from the hopelessly daft type dependency inside the malloc, the
spurious cast constitutes unwarranted chumminess with C's sister language.

All code should either do something good or stop something bad from
happening. Your cast does neither.
to convert the void* pointer returned by malloc
into a pointer to an int.


int *p = malloc(n * sizeof *p);

is cleaner, easier to read, and utterly compatible with C++, as is all C
code that is translated by a C compiler and then linked into C++ programs -
a feature which C++ specifically supports with the 'extern "C"' construct.
This is to facilitate code re-use. There is absolutely no need for anyone
to turn C code into pidgin-C++ against a day when they might want to make
it compilable in a C++ compiler.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #29
"P.J. Plauger" <pj*@dinkumware .com> writes:
"Micah Cowan" <mi***@cowan.na me> wrote in message news:m3******** ****@localhost. localdomain...
In C, the way to avoid important errors when allocating memory is
to not cast malloc()'s return.


What important error to you mask when you cast a pointer known to
be a void * to one that can only be assigned to a pointer of the
expected type?


Naturally, the error of having failed to #include <stdlib.h>,
thus getting an implied but incorrect function declaration.
Not to mention that the #include <stdlib.h> above is deprecated
in C++, and may be removed in future versions of the language, so
it's not even forward-compatible, which is allegedly what you
were trying to achieve.


It's compatible today, and will be for at least several years to
come. I'll venture to predict that the next major revision of
Standard C++ will still include the *.h headers of Standard C.
Probably those of C99, in fact.
And even in the current version of C++,
that method of inclusion will pollute the global namespace, which
is less-than-undesirable.


The C++ Standard actually does a pretty lame job of avoiding
namespace pollution, even with the handful of implementations
that actually follow all its Procrustean rules. You win way
less than you think by avoiding the *.h headers, while losing
way more de facto portability than you think in the bargain.


Well, you still get all the potential macro definitions,
yeah. As to the de facto portability: that sort of portability is
why I avoided C++ for so long, considering how poorly
standardized it was for a long time, and then how poorly the
standard had been followed until somewhat recently. When I cater
to such implementations that can't properly handle #include
<cstdlib>, those same implementations also frequently lack other
important features, to the point where I don't feel like I'm
really coding C++. Generally, if I choose to employ C++ in my
project, I deliberately decide to limit portability to
standard-conformant implementations .

-Micah
Nov 13 '05 #30

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

Similar topics

0
2632
by: Jordan Willms | last post by:
My xsl stylesheet is as simple as follows: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns="http://ltsc.ieee.org/xsd/LOMv1p0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/> <!-- rename ims datetime tags to IEEE dateTime tags --> <xsl:template match="ims:datetime">
6
3342
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
17
3141
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
5
3346
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new rightType;
5
3829
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace ) Person.xsd includes Common-Naming.xsd ( anonimous schemas ) Common-Naming.xsd includes common.xsd ( both are anonimous schemas ) Compilation of Resident.xsd raise the following exception: "System.Xml.Schema.XmlSchemaException: The attribute 'oid'...
10
4483
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
2
4655
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to one other property. In standard coding I can throw an exception during run-time, but I would rather...
4
8992
by: John Smith | last post by:
Hi I'm porting some C++ code to new platforms and have some 1-byte aligned structures which need a specific size. Since datatypes can vary on different platforms (which I found out the hard way since longs are not the same size on win64 and linux x64) I would like to do a check at compile time to make sure things are correct. This will ease future work. Is it possible to do something like the following at preprocessing stage:
15
4875
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
12
2772
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
0
9979
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, 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...
0
9826
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
10829
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
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,...
0
10468
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 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...
0
9640
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...
0
5854
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4682
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
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.