how to define constants and structs with header files

This is a discussion on how to define constants and structs with header files within the C Programming forums, part of the General Programming Boards category; I need to define some structs and constants to be used by 2 different programs that will use them for ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    how to define constants and structs with header files

    I need to define some structs and constants to be used by 2 different programs that will use them for communication.

    I'm trying to figure out how to use headers files. I've read as much as I could about it.

    This is what I figured so far:
    - you need a .h file for prototypes and a .c for the code that goes with those prototypes

    This is what I want to find out:
    - How to link those 2 files in a makefile
    - For structs what part goes in the .h file and what in the .c file
    - How to define constants
    - Do the .h and .c files need to in the same place as the makefile?

    Thanks in advance

  2. #2
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Not sure it'll answer all your questions, but this article might be of interest:

    http://www.eskimo.com/~scs/cclass/notes/sx9a.html

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    That article is great!

    But I still don't know:
    - How to link those 2 files in a makefile
    - For structs what part goes in the .h file and what in the .c file
    - Do the .h and .c files need to in the same place as the makefile?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by kotoko
    How to link those 2 files in a makefile
    What do you mean? It is a matter of writing the makefile so that the source can be compiled correctly.

    Quote Originally Posted by kotoko
    For structs what part goes in the .h file and what in the .c file
    That article mentioned structure definitions. An example would be:
    Code:
    struct s
    {
        int x;
        double y;
    };
    Quote Originally Posted by kotoko
    Do the .h and .c files need to in the same place as the makefile?
    Generally, no.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kotoko View Post
    - For structs what part goes in the .h file and what in the .c file
    Generally, we want AS LITTLE AS POSSIBLE in the header (.h) file, and as much as possible in a source (.c) file. The more we expose of functions and data structures, the less freedom we have to change those functions and data structures in the future, without also changing and/or recompiling the code that uses those things.

    AS LITTLE AS POSSIBLE means that if you only need to tell the rest of the code that "there is a struct called X", then a declaration "struct X;" is sufficient to have in the header-file. However, this means that no user of the header file will actually be able to use the content of struct X directly - all references to such a structure must be via a pointer, and the content of the struct is not available). (An example of this is the FILE structure defined by stdio.h - you never use a FILE struct, only FILE *, and you never do "FILE->something", because we never need to use the CONTENT of the FILE structure - we just need to have a pointer to a FILE struct, and let the C runtime library internals worry about what the content is and how to use the content).

    Of course, if you want to do complex number calculation, and you want the application to be able to access the real and imaginary parts of the complex number directly, then the whole struct should be defined in the external interface - the header file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Thanks for all the explanations!

    But I still don't understand the following:

    How should I write the makefile so that I can include libraries that are not standard? How can I specify where they are? I guess I need to do that otherwise the compiler won't find them.

    A practical example.

    I have bookie.c and client.c . Each are in it's own folder because I don't know how to have a makefile for two different files.
    I was going to put the header files I need and their .c files in the parent folder. How do I do that?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what does your current makefile look like? If you haven't got a makefile at all, you should read up on general makefiles.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    It ooks like:
    Code:
    FLAGS  = -Wall -g -std=c99
    CC     = gcc
    PROG   = Client
    OBJS   = semlib.o helper.o client.o
    
    all:	${PROG}
    
    clean:
    	rm ${OBJS} ${PROG} *~
      
    ${PROG}:	${OBJS}
    	${CC} ${FLAGS} ${OBJS} -o $@
    
    .c.o:
    	${CC} ${FLAGS} $< -c
    
    ##########################
    
    semlib.o: semlib.c
    
    helper.o: helper.c
    
    client.o: client.c
    But I know that it can't be right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display copied data into structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 05-02-2006, 02:12 PM
  2. seg fault
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 04-27-2006, 02:29 AM

Tags for this Thread


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