Thread: Need help with preprocessor directives

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Question Need help with preprocessor directives

    Hi, been a while
    This is my problem:
    Code:
    // main.c
    #include "header.h"
    //...
    Code:
    // app.c
    #include "header.h"
    //...
    Code:
    // header.h
    #ifndef MYH
    const int value = 10;
    #define MYH
    #endif
    //...
    mingw32 says that there are multiple definition of value...any ideas why #ifndef is not working as suppose to be? Thanks
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try declaring value as extern, then define it once in a .c file.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > any ideas why #ifndef is not working as suppose to be?
    Because include guards only work to prevent you including same thing twice within a single source file.

    Multiple definition errors come from the linker, not the compiler.

    Note that you wouldn't have this problem of having an initialised const in a header file if this were C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed