473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting typedef at preprocessing time

Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.
Apr 6 '06 #1
12 8210

Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.


/* File f.h */

#ifndef _UI32
#define _UI32
typedef unsigned int ui32 ;
#endif

Apr 6 '06 #2
In article <pa************ *************** @nanobots.com>,
Thomas Carter <T.******@nanob ots.com> wrote:
Imagine that there is some include file f.h that contains the following
line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessin g F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.

No, not without assistance such as defining a macro to indicate the
existance of the feature.

The only currencies of the preprocessor are macros and arithmetic
constant expressions.
--
Prototypes are supertypes of their clones. -- maplesoft
Apr 6 '06 #3
Thomas Carter schrieb:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.


The preprocessor itself cannot interpret typedefs.

Usually, you want to have only one such typedef.
As you can protect the contents of a header against multiple
inclusion, the usual construct is

,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
.....
typedef unsigned int ui32;
.....
#endif
`----

If you need ui32 or another of the typed from "mytypes.h" , then
#include "mytypes.h"
If any header needs to provide ui32, too, then make sure it
includes mytypes.h as well.

If you are in the rare and unfortunate situation that you can
either have an implementation typedef or have to provide one of
your own, then do the same: There usually is a, possibly
implementation specific, means to detect whether the type already
is there. Use this in your header mytypes.h. Whenever this header
is included, you can be sure that you have either one typedef or
the other -- but that it is there.
Usually better: Provide your own typedef name.

Example: You need a signed integer type with at least 24 bits and
decide you want to try first whether <stdint.h> defines one before
typedef'ing your own:

One solution:
,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
#include <limits.h>
#include <stdint.h>
.....
# ifndef INT_LEAST24_MAX
typedef signed long int_least24_t;
/* In order to "normalise" the picture: provide limits */
# define INT_LEAST24_MAX LONG_MAX
# define INT_LEAST24_MIN LONG_MIN
# endif
.....
#endif
`----

Another solution (one I would favour):
,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
#include <limits.h>
#include <stdint.h>
.....
# ifdef INT_LEAST24_MAX
typedef int_least24_t sint24;
/* Optional: Provide limits */
# else
typedef signed long sint24;
/* Optional: Provide limits */
# endif
.....
#endif
`----
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #4
Ivanna Pee schrieb:
Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessi ng F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.
/* File f.h */

#ifndef _UI32
#define _UI32


In principle correct but the (macro) identifiers starting with
_ should be left to the implementation -- this is not the exact
truth but you are on the safe side if you keep to it.
typedef unsigned int ui32 ;
#endif

Cheers
Michael

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #5

Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.


In f.h:

#define UI32_TYPEDEF
typedef ... ui32;

in F.c:

#ifndef UI32_TYPEDEF
typedef ... ui32;
#endif

Apr 6 '06 #6
On Thu, 06 Apr 2006 18:53:58 +0000, Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.


Thanks everybody for your suggestions. I forgot to point out that, in
general, one is not allowed to modify f.h. I guess that with such
restriction, what I want to do in general just can't be done.
Apr 6 '06 #7
In article <pa************ *************** *@nanobots.com> ,
Thomas Carter <T.******@nanob ots.com> wrote:
On Thu, 06 Apr 2006 18:53:58 +0000, Thomas Carter wrote:
My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.
Thanks everybody for your suggestions. I forgot to point out that, in
general, one is not allowed to modify f.h. I guess that with such
restriction, what I want to do in general just can't be done.


You might want to look at "autoconf" as a mechanism to figure out
whether the typedef is present and to generate an include file with
the appropriate macro definition. "autoconf" (and other such tools)
are outside of the rhelm of standard C, but it is plausibly ported
to all of the environments you will care about.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Apr 6 '06 #8
Thomas Carter <T.******@nanob ots.com> writes:
On Thu, 06 Apr 2006 18:53:58 +0000, Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.


Thanks everybody for your suggestions. I forgot to point out that, in
general, one is not allowed to modify f.h. I guess that with such
restriction, what I want to do in general just can't be done.


There are ways to do it if you're wiling to go beyond the scope of
what the compiler alone can do for you.

For example, you can create a small test program that attempts to use
ui32:

#include "f.h"
#include <stdio.h>
int main(void)
{
ui32 dummy;
printf("ok\n");
return 0;
}

Attempt to compile and execute the program. If it prints "ok", the
typedef exists; otherwise, it doesn't. Use this result to generate,
say, another header file to be included by F.c (is that really an
uppercase "F"?). The generated header file might just include a
series of #define directives. Depending on your environment, a
reasonably simple shell script, batch file, or equivalent should do
the job.

Your F.c might then contain something like this:

#include "f.h"
#include "config.h"
....
#ifndef UI32_DEFINED
typedef unsigned int ui32;
#endif
....

If you're doing this kind of thing a lot, you might consider using a
tool like GNU autoconf (which is off-topic here, but easy to google).

You also need to be aware of what assumptions you're making. Type
unsigned int isn't necessarily 32 bits; it can be 16, 64, or even 47,
and even if it's 32 bits, some of them could be padding bits. There
might not even be a 32-bit integer type (though there will always be
some integer type that's *at least* 32 bits).

Ideally, you should use the <stdint.h> header, which defines typedefs
such as uint32_t, along with macros that let you determine whether
they're defined. The <stdint.h> header is new in C99, but it's not
difficult to implement most of it in C90; see Doug Gwyn's q8,
<http://www.lysator.liu .se/c/q8/index.html>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 6 '06 #9
Michael Mair wrote:
Ivanna Pee schrieb:
Thomas Carter wrote:
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h,
is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if
and
only if such typedef is not already in some include file used by F.c.


/* File f.h */

#ifndef _UI32
#define _UI32


In principle correct but the (macro) identifiers starting with
_ should be left to the implementation -- this is not the exact
truth but you are on the safe side if you keep to it.
typedef unsigned int ui32 ;
#endif

Cheers
Michael


I agree with Michael. I normally follow the convention:

#ifndef H_FILENAME_H
....
#endif /* !defined(H_FILE NAME_H) */

In reality it doesn't really matter what naming convention you use so
long as you are consistent throughout the code.

Hope that helps,

Joe
Apr 8 '06 #10

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

Similar topics

4
8915
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...
12
2610
by: Ark | last post by:
Hello NG, I arrange data in structs like { members... uint16_t crc; more members, maybe... } Then I need to save them, up to and including crc, in non-volatile memory or a file, as the case may be. The data size I need for type T is offsetof(struct T, crc) +
15
29579
by: informativeguy | last post by:
Hi, I did typedef int Man; After some code I get a necessity to use the same name Man for char. I mean to say,I need to typedef char Man; But how can I do this???
20
3108
by: Binary | last post by:
Hi, With keyword struct, we can simply do forward declare by: struct struct_a; but if we typedef it: typdef struct struct_a struct_a_t;
4
2120
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating point operations. As it is a big project it is not possible to check every line manually, so is there any other method to detect floating point...
1
2473
lotus18
by: lotus18 | last post by:
Hi This is a follow-up post from my post (please click here) as CyberSoftHari suggested me to post it here. These are the tables that I've made: Schedules -ScheduleID -StartTime
18
1839
by: vivek | last post by:
What will happen if i replace a typedef with a #define?
40
1742
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly done. I want to ask ahead of time because what I do might work but it might not be the "correct" or standard way. #include <stdio.h> #include...
4
1275
by: tkpmep | last post by:
I have a simulation that runs many times with different parameters, and I want to aggregate the output into a single file with one rub: I want a header to be written only the first time. My program looks a bit like this: def main(): for param in range(10): simulate(param) def simulate(parameter):
0
7734
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. ...
0
7979
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...
1
7497
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...
0
7826
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...
1
5385
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...
0
5107
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...
1
1960
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
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
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...

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.