typedef and multiple file projects

  • Thread starter Martin Johansen
  • Start date
M

Martin Johansen

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.
 
L

Leor Zolman

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 */

Leor Zolman
BD Software
(e-mail address removed)
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
 
L

Leor Zolman

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
(e-mail address removed)
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
 
P

pete

Martin said:
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 */
 
E

Eric Sosman

Martin said:
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.
 
R

RoSsIaCrIiLoIA

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top