typedef problem

This is a discussion on typedef problem within the C Programming forums, part of the General Programming Boards category; Hello, I have 3 header files and 1 .c file. The .c file #includes both header files. In the 2 ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    typedef problem

    Hello, I have 3 header files and 1 .c file.

    The .c file #includes both header files.

    In the 2 header files, there are extern definitions of of a struct:

    header1.h:

    Code:
    #include "types.h"
    
    extern struct a
    {
         mytype a;
         ....
    } myStructA;
    header2.h:

    Code:
    #include "types.h"
    
    extern struct b
    {
          mytype b;
          .....
    } myStructB
    types.h
    Code:
    typedef int mytype;
    mycfile.c

    Code:
    #include "header1.h"
    #include "header2.h"
    
    struct myStructA;
    struct myStructB;
    
    int main()
    {
       ....
    }
    My issue is that gcc keeps complaining about a redefinition of typedef mytype. I understand what this means since I #include types.h in both headers. I need to do this since both headers use mytype in their stucts. Is there any way around this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Use inclusion guards.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    if it is microsoft, you can use
    #pragma once

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    That pragma does not work very well for inclusion guards.
    And the struct shouldn't be "extern" either. It's a declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 11:27 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21