By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,682 Members | 1,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,682 IT Pros & Developers. It's quick & easy.

typedef and multiple file projects

P: n/a
Hello.

I am programming a program which includes many libraries I have programmed.

All libraries require this:

typedef short word;

But I get an error when I include this statement two times.

If I used #define instead, I could use #ifdef, but I cannot with typedefs

How can I solve this?

Thanks.
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Thu, 4 Mar 2004 22:12:41 +0100, "Martin Johansen"
<ma******@is.online.no> wrote:
Hello.

I am programming a program which includes many libraries I have programmed.

All libraries require this:

typedef short word;

But I get an error when I include this statement two times.

If I used #define instead, I could use #ifdef, but I cannot with typedefs

How can I solve this?
Put the typedef into a header file that is equipped with an "include
guard", to make sure that definitions such as that only get processed once
per translation unit.

All your header files should have include guards anyway (where the
"SOME_HEADER" part of the symbol varies with the filename):

/* SomeHeader.h */
#ifndef SOME_HEADER_DEFINED
#define SOME_HEADER_DEFINED
....
typedef short word;
....
#endif
/* end of header file */

Thanks.


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2

P: n/a
On Thu, 4 Mar 2004 22:12:41 +0100, "Martin Johansen"
<ma******@is.online.no> wrote:
Hello.

I am programming a program which includes many libraries I have programmed.

All libraries require this:

typedef short word;

But I get an error when I include this statement two times.

If I used #define instead, I could use #ifdef, but I cannot with typedefs

How can I solve this?

Thanks.
Darn, just realized that I /think/ you were saying that each individual
library (i.e. header file) has its own typedef for the same thing. In that
case you can put that particular typedef, along with anything else that is
"duplicated', into its/their own header file, with its own include guard,
and then just #include that header file in all the other header files.
-leor


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3

P: n/a
Martin Johansen wrote:

Hello.

I am programming a program which includes
many libraries I have programmed.

All libraries require this:

typedef short word;

But I get an error when I include this statement two times.

If I used #define instead, I could use #ifdef,
but I cannot with typedefs

How can I solve this?


With header guards.

/* BEGIN header.c */

#include <stdio.h>
#include "header.h"
#include "header.h"
#include "header.h"
#include "header.h"
#include "header.h"

int main(void)
{
printf("sizeof(word) is %lu.\n", (long unsigned)sizeof(word));
return 0;
}

/* END header.c */

/* BEGIN header.h */

#ifndef H_HEADER
#define H_HEADER

typedef short word;

#endif

/* END header.h */

--
pete
Nov 14 '05 #4

P: n/a
Martin Johansen wrote:

Hello.

I am programming a program which includes many libraries I have programmed.

All libraries require this:

typedef short word;

But I get an error when I include this statement two times.

If I used #define instead, I could use #ifdef, but I cannot with typedefs

How can I solve this?


This is Question 10.15 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... which is somewhat surprising, since I can't recall the
last time I saw someone ask it. Also, the FAQ's answer,
while correct, is not very helpful. So here's an approach:

Presumably you've got a lot of .h files associated with
these libraries, and they all contain `typedef short word;'.
Try replacing it (everywhere) with something like

#ifndef WORD_HAS_BEEN_TYPEDEFED
typedef short word;
#define WORD_HAS_BEEN_TYPEDEFED
#endif

While you're about it, you might want to reconsider the
use of `word' as part of your libraries' public interfaces:
there's no visual signal that it means anything special or
that it "belongs" to your libraries, and it's a word that
some hapless victim might be using for his own purposes:

char line[] = "Hark! Hark! The lark at heavn's gate sings!";
char *word = line;
while ((word = strtok(word, " \t\n.,!?\"'")) != NULL) {
printf ("The next word is %s\n", word);
word = NULL;
}

You might use `MJword' instead, to suggest that the
identifier belongs to MJ's library. At the very least,
use `Word' to give a visual clue that the identifier is
in some way unusual.

--
Er*********@sun.com
Nov 14 '05 #5

P: n/a
On Thu, 04 Mar 2004 16:54:48 -0500, Eric Sosman <Er*********@sun.com>
wrote:
char line[] = "Hark! Hark! The lark at heavn's gate sings!";
char *word = line;
while ((word = strtok(word, " \t\n.,!?\"'")) != NULL) { is it \'? ^ printf ("The next word is %s\n", word);
word = NULL;
}

Nov 14 '05 #6

P: n/a
RoSsIaCrIiLoIA <n@esiste.ee> wrote:
On Thu, 04 Mar 2004 16:54:48 -0500, Eric Sosman <Er*********@sun.com>
wrote:
while ((word = strtok(word, " \t\n.,!?\"'")) != NULL) {

is it \'? ^


Within a string literal? No, why?

Richard
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.