Need help with preprocessor directives

This is a discussion on Need help with preprocessor directives within the C Programming forums, part of the General Programming Boards category; Hi, been a while This is my problem: Code: // main.c #include "header.h" //... Code: // app.c #include "header.h" //... ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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