LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   multiple C source files howto (https://www.linuxquestions.org/questions/programming-9/multiple-c-source-files-howto-348538/)

introuble 07-31-2005 07:52 AM

multiple C source files howto
 
Ok ..

So how would you do this under linux:

You have a program and you want cirtain "functions" to be in sepparate source files .. how would you compile this program ?

i.e:

main.c : contains "main" function which calls function 'test", includes "prog.h"

prog.h : definition for function "test"

test.c : body of function test

?

sind 07-31-2005 08:50 AM

It can be as simple as:

Code:

$ gcc -o program main.c test.c
Or, you can have seperate commands for each step:

Code:

$ gcc -c main.c
$ gcc -c test.c
$ gcc -o program main.o test.o

~sind

jonaskoelker 07-31-2005 07:49 PM

Quote:

prog.h: definition of "test"
test.c: body of "test"
The definition of a function *is* it's body.

I think you mean that the *declaration* is in prog.h

Code:

/* declaration */
int factorial(int);

/* definition */
int factorial(int n) {
  if (!n) return 1;
  return n * factoral(n - 1);
}

hth --Jonas


All times are GMT -5. The time now is 05:12 PM.