making a simple module

Discussion in 'C Programming' started by Johs32, Mar 14, 2006.

  1. Johs32

    Johs32 Guest

    I have the made the 3 following files:

    1) my headerfile "draft.h":

    struct tkb {
    int *prio;

    };
    int init();
    int test();

    2) file with the implementation of the functions called "draft.c":
    #include<stdio.h>
    #include<stdlib.h>
    #include "draft.h"

    static struct tkb first_thread;
    struct tkb *current = &first_thread;

    int init()
    {
    int a = 222;
    current->prio = &a;
    printf("init :%d\n", *current->prio);
    return 0;
    }

    int test()
    {
    printf("test : %d\n",*current->prio);

    return 0;
    }


    3) And a file containing a main function that can use the functions called
    "start.c":
    #include "draft.h"
    #include<stdio.h>
    #include<stdlib.h>

    int main()
    {

    init();

    return 0;

    }

    but how do I compile the three files into a working program?

    Johs
     
    Johs32, Mar 14, 2006
    #1
    1. Advertising

  2. Johs32

    Ben C Guest

    On 2006-03-14, Johs32 <> wrote:
    > I have the made the 3 following files:
    >
    > 1) my headerfile "draft.h":
    >
    > struct tkb {
    > int *prio;
    >
    > };
    > int init();
    > int test();
    >
    > 2) file with the implementation of the functions called "draft.c":
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include "draft.h"
    >
    > static struct tkb first_thread;
    > struct tkb *current = &first_thread;
    >
    > int init()
    > {
    > int a = 222;
    > current->prio = &a;
    > printf("init :%d\n", *current->prio);
    > return 0;
    > }
    >
    > int test()
    > {
    > printf("test : %d\n",*current->prio);
    >
    > return 0;
    > }
    >
    >
    > 3) And a file containing a main function that can use the functions called
    > "start.c":
    > #include "draft.h"
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > int main()
    > {
    >
    > init();
    >
    > return 0;
    >
    > }
    >
    > but how do I compile the three files into a working program?


    Depends what your "development environment" is.

    If it's Linux, or something similar, you can just go

    $ gcc start.c draft.c

    and your program will appear as a file called a.out. But better to have
    a look at make and learn how to do makefiles.

    On some other systems with a graphical IDE you might have to create what
    they usually call a "Project" using the menus.
     
    Ben C, Mar 14, 2006
    #2
    1. Advertising

  3. Johs32

    Johs32 Guest

    Ben C wrote:

    > On 2006-03-14, Johs32 <> wrote:
    >> I have the made the 3 following files:
    >>
    >> 1) my headerfile "draft.h":
    >>
    >> struct tkb {
    >> int *prio;
    >>
    >> };
    >> int init();
    >> int test();
    >>
    >> 2) file with the implementation of the functions called "draft.c":
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >> #include "draft.h"
    >>
    >> static struct tkb first_thread;
    >> struct tkb *current = &first_thread;
    >>
    >> int init()
    >> {
    >> int a = 222;
    >> current->prio = &a;
    >> printf("init :%d\n", *current->prio);
    >> return 0;
    >> }
    >>
    >> int test()
    >> {
    >> printf("test : %d\n",*current->prio);
    >>
    >> return 0;
    >> }
    >>
    >>
    >> 3) And a file containing a main function that can use the functions
    >> called "start.c":
    >> #include "draft.h"
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >>
    >> int main()
    >> {
    >>
    >> init();
    >>
    >> return 0;
    >>
    >> }
    >>
    >> but how do I compile the three files into a working program?

    >
    > Depends what your "development environment" is.
    >
    > If it's Linux, or something similar, you can just go
    >
    > $ gcc start.c draft.c
    >
    > and your program will appear as a file called a.out. But better to have
    > a look at make and learn how to do makefiles.
    >
    > On some other systems with a graphical IDE you might have to create what
    > they usually call a "Project" using the menus.



    Ok I have now made the following makefile:

    CC=gcc
    CFLAGS=-g

    all:draft
    draft: draft.o start.o
    $(CC) $(CFLAGS) draft.o start.o -o run

    draft.o: draft.c draft.h
    $(CC) $(CFLAGS) -c draft.c

    start.o: start.c draft.h
    $(CC) $(CFLAGS) -c start.c

    clean:

    it seems to work fine. Is there something I am missing?
     
    Johs32, Mar 14, 2006
    #3
  4. Johs32 <> writes:
    > I have the made the 3 following files:
    >
    > 1) my headerfile "draft.h":

    [snip]
    > 2) file with the implementation of the functions called "draft.c":

    [snip]
    > 3) And a file containing a main function that can use the functions called
    > "start.c":

    [snip]
    > but how do I compile the three files into a working program?


    I'd use a compiler.

    You're asking about how to use your tool set, not about the C
    programming language. The answer depends on which compiler and
    operating system you're using. The answer should also be reasonably
    easy to find in your system's documentation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 14, 2006
    #4
  5. Johs32

    Ben C Guest

    On 2006-03-14, Johs32 <> wrote:
    > Ok I have now made the following makefile:
    >
    > CC=gcc
    > CFLAGS=-g


    > all:draft


    You ought to put

    ..PHONY: all

    It's just that if you have an actual file called "all" things can get
    very confusing.

    > draft: draft.o start.o
    > $(CC) $(CFLAGS) draft.o start.o -o run


    You don't want CFLAGS there, but possibly LDFLAGS (if you need any
    linker flags).

    > draft.o: draft.c draft.h
    > $(CC) $(CFLAGS) -c draft.c
    >
    > start.o: start.c draft.h
    > $(CC) $(CFLAGS) -c start.c


    You can make gcc generate the dependencies on header files for you, see
    under gcc -MM. But this is all right.

    > clean:


    After clean you'd want something like "rm *.o run". And you'd want to
    make clean PHONY as well.

    > it seems to work fine. Is there something I am missing?


    I don't think my first makefile was half as good as this one!
     
    Ben C, Mar 14, 2006
    #5
  6. Johs32 <> writes:
    > Ben C wrote:
    >> On 2006-03-14, Johs32 <> wrote:
    >>> I have the made the 3 following files:

    [snip]
    >>> but how do I compile the three files into a working program?

    >>
    >> Depends what your "development environment" is.

    [snip]
    > Ok I have now made the following makefile:
    >
    > CC=gcc
    > CFLAGS=-g

    [snip]
    > it seems to work fine. Is there something I am missing?


    Yes, you're missing the fact that you're not asking a C language
    question. For system-specific issues like tihs, try
    comp.unix.programmer (or some other newsgroup appropriate to your
    system).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 14, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Rothenberg
    Replies:
    0
    Views:
    459
    Robert Rothenberg
    Jun 4, 2004
  2. Replies:
    5
    Views:
    1,895
    Andrew Thompson
    Jan 12, 2005
  3. Paul Gardella

    Python module for making org charts?

    Paul Gardella, Oct 10, 2003, in forum: Python
    Replies:
    4
    Views:
    426
    Dave Kuhlman
    Oct 13, 2003
  4. Maric Michaud
    Replies:
    0
    Views:
    7,204
    Maric Michaud
    Jun 24, 2006
  5. Ulrich Eckhardt
    Replies:
    3
    Views:
    222
    Ulrich Eckhardt
    Nov 23, 2010
Loading...

Share This Page