multiple file programs in c

R

rinn

Hello,
I have written a main() program in sap.c and created one external
function in another file fun.c my doubt is how to link these two files
sap.c, fun.c and run a program inorder to get a result.i'll be happy if
anyone gives the procedure.
 
C

Cong Wang

rinn said:
Hello,
I have written a main() program in sap.c and created one external
function in another file fun.c my doubt is how to link these two files
sap.c, fun.c and run a program inorder to get a result.i'll be happy if
anyone gives the procedure.
gcc -o result sap.c fun.c
 
J

John Bode

rinn said:
Hello,
I have written a main() program in sap.c and created one external
function in another file fun.c my doubt is how to link these two files
sap.c, fun.c and run a program inorder to get a result.i'll be happy if
anyone gives the procedure.

This depends on what development environment you are using, and the
question is a little beyond the scope of this particular newsgroup, as
it's not strictly a C *language* issue.

If you are using gcc, the procedure would be either

gcc -o sap sap.c fun.c

or

gcc -c sap.c
gcc -c fun.c
gcc -o sap sap.o fun.o

Both of these will compile sap.c and fun.c and link them into an
executable named sap. If you leave off the "-o sap", the executable
will be named a.out.

Most Unix command-line driven compilers work roughly the same way,
although specific options will differ between them.

It's been a while since I've had to develop with anything like Visual
Studio or similar. I remember you had to add files to the project in
order for them to be compiled and linked, but I don't remember any
other specifics; check your manuals or online help.
 
C

CBFalconer

Cong said:
gcc -o result sap.c fun.c

Assuming the prototype of the function is in fun.h and that sap.c
has a statement #include "fun.h". The function in fun.c should not
be declared as static.

The purpose of the .h files is to specify the access to the
appropriate .c files allowed.
 
M

Malcolm

rinn said:
I have written a main() program in sap.c and created one external
function in another file fun.c my doubt is how to link these two files
sap.c, fun.c and run a program inorder to get a result.i'll be happy if
anyone gives the procedure.

/*
sap.c
*/
#include <stdio.h>
#include "fun.h"

int main(void)
{
int x;
x = afunction("how many xes are in this string?");
printf("function returned %d\n", x);
return 0;
}

/*
fun.h
*/
#ifndef fun_h
#define fun_h
int afunction(const char *str);
#endif

/*
fun.c
*/

static int countchars(const char *str);

int afunction(const char *str)
{
return countchars(str, 'x');
}

static int countchars(const char *str, char ch)
{
int answer = 0;

while(*str)
{
if(*str == ch)
answer++;
str++;
}

return answer;
}


That shows tha basic program layout.

Put sap.c, fun.c and fun.h into the same directory (sometimes you can make
the complier use fancier structures, do i the easy way for now).

If you are using a command line complier, generally you need to type the
names of all the .c source files. It will then compile them all and link.
if you are using an IDE like Visual C++, you need to create a "project" file
in some internal format, which contains the names of all your source files,
the build from the visual tools.

When your project gets really complicated you need to move to makefiles, but
that's a whole differnet story.
 
R

Richard Heathfield

Malcolm said:
When your project gets really complicated you need to move to makefiles,
but that's a whole differnet story.
^^^^^^^^^

It's Usenet, not Differnet.

Um, actually, never mind... you do have a point.
 
M

Mabden

Richard Heathfield said:
^^^^^^^^^

It's Usenet, not Differnet.

Um, actually, never mind... you do have a point.

I went on the line to the Differnet, but it was like going to Europe -
they do everything the same there.
 

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
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top