including files

This is a discussion on including files within the C Programming forums, part of the General Programming Boards category; I would like to use a method written in another c file that I have. How would I do this?...

  1. #1
    Deb
    Deb is offline
    Registered User
    Join Date
    Apr 2007
    Posts
    28

    including files

    I would like to use a method written in another c file that I have.

    How would I do this?

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Make sure you link the two files together and you can just declare the function:
    Code:
    // file1.c
    void function( void ) {
      // stuff
    }
    Code:
    // file2.c
    void function( void );
    
    
    int main( void ) {
      function();
    }
    Code:
    C:\> gcc file1.c file2.c
    It's probably better to add a header file to do that for you though. Then you can add more stuff any not have to keep typing a declaration every time you want to use it:
    Code:
    // file1.h
    #ifndef _FILE1_H_
    #define _FILE1_H_
    
    
    void function( void );
    
    
    #endif
    Code:
    // file1.c
    #include "file1.h"
    
    
    void function( void ) {
      // stuff
    }
    Code:
    // file2.c
    #include "file1.h"
    
    
    int main( void ) {
      function();
    }
    Code:
    C:\> gcc file1.c file2.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including my own header files
    By bushymark in forum C Programming
    Replies: 17
    Last Post: 11-03-2009, 08:09 PM
  2. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Makefile producing a list of files.
    By leonm54 in forum Tech Board
    Replies: 1
    Last Post: 07-23-2007, 09:54 AM
  5. including other .c files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 08:56 PM

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