Thread: define functions in another file

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    define functions in another file

    I'd like to define some functions for a program in another file as they are too long..
    So, I used
    PHP Code:
    # include <filename.c> 
    but the problem comes here is that ...
    The variables that I commonly used in main and this files have to be declared before I write main()..

    something like:
    Code:
    int variable; declared above the whole code..
    
    # include <stdio.h>
    int main()
    {
        int a,b,c; 
       ....      
    }
    Is this the right way to do.
    I don't know whether it totally works or not.. as I am in the middle of my code. As far as I recognized is that .. its not showing the respective error.

    1. If I want to define functions in some other files and include in main..
    how do I do it ?
    2. Actually, my function files are not located in default location..
    so, I need to mention the address too..
    PHP Code:
    # include </home/username/folder/filename.c> 
    is there any other way to do that..
    what I should do .. so that I need not mention this address.
    something like adding search directory to the gcc compiler for checking filess..

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    We just had a discussion about this not too long ago. Take a look at this thread. It should answer all your questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by suryak
    The variables that I commonly used in main and this files have to be declared before I write main()..
    Avoid global variables. Instead, write function that accept arguments and work on them. These functions can then do one thing and do it well, and it becomes easier to reason about your program because you only need to consider those parameters and local variables.

    If you persist with unnecessary use of global variables, dividing your code into functions will only be of minimal use since you would have to keep checking if the global state is changed whenever you call a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I am not totally cleared about that..
    What I have done in my project is that.. I divided the whole into some modules.. (say, mod1.c mod2.c ... )
    These modules have some function definitions which I need to call in main().

    how do I include these files in main().

    As far as from the above discussion.. I decided to use extern for common variables that are involved in all modules. but does it solve my matter...?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by suryak
    how do I include these files in main().
    It depends. If you want to add the directory they are in to the include file search path, take a look at the gcc documentation: Options for Directory Search.

    Quote Originally Posted by suryak
    As far as from the above discussion.. I decided to use extern for common variables that are involved in all modules. but does it solve my matter...?
    NO! In terms of getting your program to compile and link, it may "solve" your problem, but you should strictly avoid global variables until you have acquired more experience that will allow you to make a sound decision on whether global variables are appropriate. Sure, those variables may be common to all modules, but that in itself does not mean that they should be global.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I think. you didn't get me..

    if I have mod1.c, mod2.c which have some function definitions .. mod1() & mod2()
    how do I call them in main().. As those functions are defined in separate file..how to I include them in main()

    is this correct way to write in main file.. main.c which contain main()

    # include <mod1.c>
    # include <mod2.c>


    and where should I place them.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by suryak
    if I have mod1.c, mod2.c which have some function definitions .. mod1() & mod2()
    how do I call them in main().. As those functions are defined in separate file..how to I include them in main()
    You need to place their prototypes in a header file that is included.

    Quote Originally Posted by suryak
    is this correct way to write in main file.. main.c which contain main()

    # include <mod1.c>
    # include <mod2.c>
    Probably not because you would be including source files rather than header files, if the .c extension is a valid clue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Typically, you would have your own header files which have your function prototypes in them. Then you would include these header files in your main project file. Additionally, these header files would be included in your module files which would define your functions. We have an example here - multiple source files FAQ. It uses C++ but should give you the idea.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    1337
    Join Date
    Jul 2008
    Posts
    135
    Quote Originally Posted by suryak View Post
    I think. you didn't get me..

    if I have mod1.c, mod2.c which have some function definitions .. mod1() & mod2()
    how do I call them in main().. As those functions are defined in separate file..how to I include them in main()

    is this correct way to write in main file.. main.c which contain main()

    # include <mod1.c>
    # include <mod2.c>


    and where should I place them.
    if you want to include .c file in main(), use this
    Code:
    # include "mod1.c"
    # include "mod2.c"
    Make sure all three files are in the same directory. Else, you will have to specify the full path for mod1.c and mod2.c

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @Andrew.. this is what I am looking for.. Though its in c++ .. I could catch the point.

    Here is what I did.

    instead of building mod1.c and mod2.c ..
    I made mod1.h and mod2.h

    added

    #ifndef mod1_H
    #define mod1_H

    and at the end, i added
    #endif

    and in the main.c ..
    I add

    # include <mod1.h>
    # include <mod2.h>


    when I complile .. its showing " no such file or directory found.."

    what's the problem..how do i fix it.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If the files are in the same directory, it would be:

    #include "mod1.h"
    #include "mod2.h"

    Also note Laser's link about setting up your directory search options with gcc.

    EDIT: The angle brackets < > tell the compiler to look in the system header files but it doesn't look in the current directory by default. Hence Laser's link.
    Last edited by AndrewHunter; 08-08-2011 at 11:28 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by suryak
    instead of building mod1.c and mod2.c ..
    I made mod1.h and mod2.h

    added

    #ifndef mod1_H
    #define mod1_H
    and at the end, i added
    #endif

    and in the main.c ..
    I add

    # include <mod1.h>
    # include <mod2.h>
    This might be correct, but generally, it isn't. Normally, we would do something like this:
    Code:
    #ifndef FOO_H
    #define FOO_H
    
    /* foo.h */
    
    void foo(int n);
    
    #endif
    Code:
    /* foo.c */
    
    #include <stdio.h>
    
    void foo(int n)
    {
        printf("This is foo: %d\n", n);
    }
    Code:
    /* main.c */
    
    #include <stdio.h>
    #include "foo.h"
    
    int main(void)
    {
        printf("%s\n", "Hello world!");
        foo(123);
        return 0;
    }
    Quote Originally Posted by suryak
    when I complile .. its showing " no such file or directory found.."

    what's the problem..how do i fix it.
    Refer to the link in my post #5.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Hmm . I did what exactly you have said..
    I got this error: What might have possible went wrong

    /tmp/ccisYCMp.o: In function `main':
    main.c.text+0x14a): undefined reference to `PossValue'
    main.c.text+0x199): undefined reference to `PossValue'
    collect2: ld returned 1 exit status


    Here PossValue is the header file PossValue.h and I defined functions in PossValue.c

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I used at terminal to mention header file loc

    gcc main.c -I /home/username/code/folder

  15. #15
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by suryak View Post
    I used at terminal to mention header file loc

    gcc main.c -I /home/username/code/folder
    u need to compile the other .c files with it...
    i.e
    gcc main.c mod1.c mod2.c -I /home/username/code/folder

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define from a separate file
    By Syekiya in forum C Programming
    Replies: 6
    Last Post: 09-19-2010, 03:33 AM
  2. define two functions
    By noob programmer in forum C Programming
    Replies: 10
    Last Post: 11-04-2008, 01:41 PM
  3. Replies: 7
    Last Post: 04-17-2008, 12:54 PM
  4. which file define the PI value
    By henryluo in forum C Programming
    Replies: 4
    Last Post: 05-18-2005, 05:37 AM
  5. Help! Sample prog. on user-define string functions
    By orbandjay in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2002, 02:48 AM

Tags for this Thread