Compiling with One Header File.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sakebu
    New Member
    • Oct 2014
    • 2

    Compiling with One Header File.

    Hello,
    I am trying to compile two programs using the same header
    file and definitions. I need help on the makefile part to make this work.

    =============== =============== ==
    My headerfile looks like this:

    #ifdef ONE
    void hello(int s);

    #else
    int pin;
    int verification(ch ar* name);

    #endif
    =============== ==============

    My makefile

    SRCONE = hello.c input.c
    OBJONE = hello.o input.o

    SRCTWO = name.c account.c
    OBJTWO = name.o account.o

    INCLUDEPATH = ../include/
    CFLAGS += -I$(INCLUDEPATH)

    all: one two

    one: $(OBJONE)

    two: $(OBJTWO)

    =============== =============== =

    How can I include the flag -DONE in my makefile so that
    it includes void hello(int s); for "one" and
    int pin;
    int verification(ch ar* name);
    for "two"
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The first thing about header files is that they cannot contain any code. Like defining an int. Or anything else that occupies memory or generates instructions.

    When this header is used in multiple source files you get an int in every object file. When the object files are linked, the linker can fail on multiple definitions.

    So if you share an int in multiple source files, you put the int in it's own source file and add this file to your build. Access from the other source files is accomplished by using extern.

    The #ifdef (or #ifndef) only protects against multiple declarations if the include file is included more than in the same source file.

    You should have:

    Code:
    Header one:
    
     void hello(int s);
    Code:
    Header two:
    
    int verification(char* name);
    Code:
    Source file for global variables:
    
    int pin;
    Code:
    Access the global variable from another source file:
    
    extern int pin;

    Comment

    • sakebu
      New Member
      • Oct 2014
      • 2

      #3
      It actually works the way I have it.
      My solution was that for each .o file in my ONE program I add to CFLAGS like this:

      hello.o: CFLAGS+= -DONE
      input.o: CFLAGS+= -DONE

      This will automatically attach -DONE during compilation and select the correct definitions in the header file for program ONE.

      And for program TWO I do not need to do anything since it will go to the else portion of the header file.

      Comment

      Working...