#define from a separate file

This is a discussion on #define from a separate file within the C Programming forums, part of the General Programming Boards category; Hi everyone! I'm trying to figure out how I can #define something, but I want to define it from a ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    #define from a separate file

    Hi everyone!



    I'm trying to figure out how I can #define something, but I want to define it from a separate file.


    I want the code to be static, but, I want some of the variables to do dynamic, this way it makes it so I don't have to recompile it if I want to spread the same program among many computers.


    This is a .c file using a linux OS.



    Thank you very much!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    #define the macro in a separate file, and #include that file.

    In practice, such a file is often referred to as a header file and given an extension that indicates it is a header (e.g. ".h").

    The only thing to remember is to either ensure that no .c file #include's the file more than once (as redefining macros is not allowed). If that is not possible (eg headers that #include headers, which can occur in larger projects) look up the topic of "include guards".

    I'm not sure what you mean by "I want some of the variables to do dynamic" though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    So you have in a header file (say foo.h)
    Code:
    #define MAGIC 1234

    Then in your C file(s), you simply have
    Code:
    #include "foo.h"

    And then later on, say
    Code:
    while ( var != MAGIC )

    Or do you have something else in mind when you say "something" (It's rather vague)
    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.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Thank you for the reply.

    However, in order for any changes to take effect, the program would have to be recompiled.


    This seperate file is a configuration file, and I don't want to have to recompile the program at all.



    Thank you again!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Well if you're hoping to get from say

    Code:
    #define func(a,b)  a+b
    to
    Code:
    #define func(a,b) a*b
    without any recompiling, then you're going to have to do a LOT more work than using the pre-processor.

    Even simple constants would need to be stored and parsed from a text file at run time (not #include at compile time).

    Eg.
    [config]
    MAGIC = 1234

    You open the file, you read the lines and you parse out the words and values.
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    But you still have to compile the program for every OS anyway.
    But if you're referring to being able to change some settings without compiling the executable, then you would need a configuration file somewhere. Defines won't do this. You need to store information in a file and read that file.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Okay!

    Thank you very much everyone for you help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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