473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

typedef and multiple file projects

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
6 4749
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
3
by: Harry Whitehouse | last post by:
I'm developing a small class which will be used in a number of distinct C# projects. This small class is undergoing constant change/refinement as I try to employ it in the different projects. ...
7
by: Dennis Myrén | last post by:
Hi. Is there any way to define an alias for a type like you could in C++: typedef int NUMBER; I tried using #define but that did not work. Thank you. -- Regards,
36
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a...
12
by: Thomas Carter | last post by:
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...
7
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a...
13
by: Paul Cheetham | last post by:
Hi, I have two projects where I want to use the same source file in each project. If I add the file to each project, then a local copy is made. I want to be able to have a single source file...
5
by: Alan Woodland | last post by:
Hi, I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.