C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2002, 08:47 AM   #1
Registered User
 
Join Date: Mar 2002
Posts: 12
Question linking source files

HI

how do i link one or more source files to the file containing the main() function.
e.g. if i write functions and save em in different files and wanna use them from my mian source files by passign parameter to those functions how do i let the mian fle know where to look?

im using Borland Turbo C V 2.01

thank you
estranged is offline   Reply With Quote
Old 04-02-2002, 09:10 AM   #2
Nit
Registered User
 
Join Date: Mar 2002
Posts: 42
just type:

#include "myFile.c"

where your other includes are.
__________________
http://www.KBeutler.com
Nit is offline   Reply With Quote
Old 04-02-2002, 09:24 AM   #3
Registered User
 
Join Date: Mar 2002
Posts: 12
ah ok

and all the source files must b in the same directory or can i point to other folders?
estranged is offline   Reply With Quote
Old 04-02-2002, 09:31 AM   #4
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
Estranged,

I may have misunderstood your intent, but it also possible to compile your individual source files into object files, and link the object files together into one executable. Is this what you had in mind? If so, tell us what compiler you are using.

Cheers,

[edit]s/objects/object files/g[/edit]
__________________
Jason Deckard

Last edited by Deckard; 04-02-2002 at 11:50 AM.
Deckard is offline   Reply With Quote
Old 04-02-2002, 11:44 AM   #5
....
 
Join Date: Aug 2001
Location: Groningen (NL)
Posts: 2,386
>#include "myFile.c"

You normally don't inlcude C source-files. When you have more C files you could create a make-file or pass the files to the linker. In your case it's probably possible to create a project and add files to the project. When compiling the project, everything will be done for you. I thought that Turbo C had a menu Project, not sure about that.
Shiro is offline   Reply With Quote
Old 04-02-2002, 04:49 PM   #6
Registered User
 
PutoAmo's Avatar
 
Join Date: Mar 2002
Posts: 72
Quote:
Originally posted by Deckard
Estranged,

I may have misunderstood your intent, but it also possible to compile your individual source files into object files, and link the object files together into one executable. Is this what you had in mind? If so, tell us what compiler you are using.

Cheers,

[edit]s/objects/object files/g[/edit]
Two questions:

1) Is there any difference between:

gcc -o execfile fun1.o fun2.o

and

gcc -o execfile fun2.o fun1.o

??

2) Do I have to include prototypes for fun1() and fun2() in header file?

Thanks
PutoAmo is offline   Reply With Quote
Old 04-02-2002, 05:58 PM   #7
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
No and no ;)
__________________
Jason Deckard
Deckard is offline   Reply With Quote
Old 04-03-2002, 05:49 AM   #8
Registered User
 
PutoAmo's Avatar
 
Join Date: Mar 2002
Posts: 72
So when do we need to include prototypes?
PutoAmo is offline   Reply With Quote
Old 04-03-2002, 06:38 AM   #9
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
Function names are a lot like pointers (variables that hold memory addresses) in that they reference the memory location of a function. Like any variable, a function cannot be called prior to it being declared or defined.
Code:
#include <stdio.h>

int main( void )
{
  show_message();  /* Wrong */

  return 0;
}

void show_message( void )
{
  printf( "Hello World\n" );
}
Prior to using the show_message() function, it must either be declared (with a prototype) or defined (by placing the function in the source file before any calls to it). To fix the code example above, either prototype the show_message() function or swap the order of the functions, so that show_message() is above main().

Prototypes are also unnecessary to declare functions that exist in other modules (source files), as the dependency will be resolved when linking the object files together into an executable. Here's an example of a program that uses three source files (note function prototypes are not required):

test1.c
Code:
int main( void )
{
  show_message_1( "Hello " );
  show_message_2( "World\n" );

  return 0;
}
test2.c
Code:
#include <stdio.h>

void show_message_1( const char *msg )
{
  printf( "%s", msg );
}
test3.c
Code:
#include <stdio.h>

void show_message_2( const char *msg )
{
  printf( "%s", msg );
}
This can be compiled with cc or gcc using: gcc -o testprog test1.c test2.c test3.c

Having said all that, let me point out that prototyping is (in my opinion) good documentation. As a programmer, you should understand the circumstances where prototyping is required. You should also include a prototype for every function, near the top of your source file, so a quick glance reveals what that module contains.
__________________
Jason Deckard
Deckard is offline   Reply With Quote
Old 04-03-2002, 06:38 PM   #10
Registered User
 
Join Date: Mar 2002
Posts: 25
I think we got off subject

I think I know where the original post was getting at.

Estranged, all you would need is to make a header file containing all the prototypes for the functions your source files will contain. For example:

main.c - source file containing your main "function"
sprites.c - source file for sprite functions
sprites.h - header file with all prototype for sprite functions
video.c - source file for video functions
video.h - header file for video prototypes

You will be including sprites.h and video.h in your main.c. Include all "sub" headers in your sources. For example, video.h will be included in video.c.

Using turbo c v.2.01, all you would need to link this at the command line: (NOTE: This compile will compile sources to object files, pass them to the linker, and generate 80286 compatible instructions using a huge memory model ;-) )

TCC -l-c -2 -mm -w main.c sprites.c video.c mathm.lib cm.lib graphics.lib emu.lib fp87.lib

Download a few sources off the net to see what I'm talking about. Programmer's Heaven has a lot of game sources that can be compiled with the same example above, including a nice Bomberman clone ;-)
__________________
Storm! Studios
WHurricane16 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion on header and source files dnguyen1022 C++ Programming 4 01-17-2009 03:42 AM
Class Inheritance over multiple source files Swarvy C++ Programming 7 11-11-2008 10:03 AM
Multiple Source Files, make files, scope, include thetinman C++ Programming 13 11-05-2008 11:37 PM
pseudocode for multiple source files Calef13 C++ Programming 4 11-13-2007 09:07 AM
Linking 3 files in Borland C 3.11 Brain Damage C++ Programming 5 05-29-2005 02:53 AM


All times are GMT -6. The time now is 02:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22