linking source files

This is a discussion on linking source files within the C Programming forums, part of the General Programming Boards category; HI how do i link one or more source files to the file containing the main() function. e.g. if i ...

  1. #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

  2. #2
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    just type:

    #include "myFile.c"

    where your other includes are.
    http://www.KBeutler.com

  3. #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?

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    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]
    Last edited by Deckard; 04-02-2002 at 10:50 AM.
    Jason Deckard

  5. #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.

  6. #6
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    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

  7. #7
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    No and no ;)
    Jason Deckard

  8. #8
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    So when do we need to include prototypes?

  9. #9
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    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

  10. #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 ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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