making a simple module

J

Johs32

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
 
B

Ben C

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.
 
J

Johs32

Ben said:
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?
 
K

Keith Thompson

Johs32 said:
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.
 
B

Ben C

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.

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!
 
K

Keith Thompson

Johs32 said:
Ben said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top