![]() |
| | #1 |
| Registered User Join Date: Mar 2002
Posts: 12
| 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 | |
| | #2 |
| 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 | |
| | #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 | |
| | #4 |
| B26354 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 | |
| | #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 | |
| | #6 | |
| Registered User Join Date: Mar 2002
Posts: 72
| Quote:
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 | |
| | #7 |
| B26354 Join Date: Jan 2002
Posts: 631
| No and no ;)
__________________ Jason Deckard |
| Deckard is offline | |
| | #8 |
| Registered User Join Date: Mar 2002
Posts: 72
| So when do we need to include prototypes? |
| PutoAmo is offline | |
| | #9 |
| B26354 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" );
}
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;
}
Code: #include <stdio.h>
void show_message_1( const char *msg )
{
printf( "%s", msg );
}
Code: #include <stdio.h>
void show_message_2( const char *msg )
{
printf( "%s", msg );
}
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |