473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

multiple file programs in c

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.

Nov 15 '05 #1
7 7621

rinn wrote:
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

Nov 15 '05 #2

rinn wrote:
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.

Nov 15 '05 #3
"Cong Wang" <xi************@gmail.com> writes:
rinn wrote:
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


What makes you think the OP is using gcc?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #4
Cong Wang wrote:
rinn wrote:
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


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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #5

"rinn" <ia*******@yahoo.co.in> wrote
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.


Nov 15 '05 #6
Malcolm wrote:
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #7
"Richard Heathfield" <in*****@address.co.uk.invalid> wrote in message
news:de**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
Malcolm wrote:
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.


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

--
Mabden
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
9
by: Daniel Moree | last post by:
I'm using MS VC++ 6.0 I'm working on a big project. I've currently have several files for this project. Here's the problem. I have one header file phead.h I have two code files main.cpp and...
1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
3
by: UJ | last post by:
Has anybody done anything with using log4net where you have multiple programs on a single machine writing to the same log file? TIA - J.
8
by: subramanian100in | last post by:
Suppose I have #include <stdio.h> #include <string.h> size_t strlen(const char *str) { printf("from strlen - %s\n", str); return 0; // return some dummy value
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
2
by: michael40card | last post by:
First of all... Hi I am attempting to create a html 'gallery creater' for a program... everythink was going ok, seems to work. the only trouble is i cannot find any way of allowing the user to...
2
by: rsteph | last post by:
I took a number of classes back in college with Java, VB, and VB.NET, and a few in C++. I was looking through some of my books the other day and decided to brush up on my C++ programming, as I'd like...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
2
by: Steve | last post by:
Hi All I have several POS programs (Windows forms VS 2005) Some customers have multiple copies of the programs, all networked to the 1 SQL Server 2005 Database which resides on 1 of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.