none
calling a function in a file from another file RRS feed

  • Question

  • Hi

    I have created a add.h file and its contents are as follows:

    #ifndef ADD_H
    #define ADD_H

    extern int add(int a, int b);

    #endif

    and created another file add.c

    #include<stdio.h>
    extern int add(int a,int b);
    void main()
    {
    printf("sum of two numbers is %d",add(3,4));
    }
    int add(int a,int b)
    {
    int c;
    c=a+b;
    return c;
    }

    now i want to access add function in dif.c and its contents are:

    #include <stdio.h>
    #include<add.h>
    int sub(int x,int y);

    void main()

    {

    printf("difference is %d",sub(5,4));
    }
    int sub(int x,int y)
    {
    int d;
    x=add(3,4);
    d=x-y;
    return d;
    }

    i compiled diff.c file and im geting error cannot include add.h

    i also tried to link add.c and diff.c using cmd "cl add.c dif.c"

    im gettng error:

    main already in l1.obj.

    how to link two files and cal a function in add.c from dif.c?

     

     im using visual studio command prompt to compile c programs

    Wednesday, December 29, 2010 7:19 AM

Answers

  • The compiler is not able to find the location of add.h file

    Make sure you have added the correct path of add.h added in "include Directories" in case of diff.c compilation.

     

    You should have one main function in one of the .c file and defination of called function another .c file.

     

    main() is the entry for start of execution for an exe.

     

    You can create a library static or dynamic and call its functions to create an exe. Static and dynamic libraries has its own advantages and disadvantages.

     

    Functions can be exported if it is libraries/modules so that it can be linked at runtime.

     

    Two .c file with two main() cannot crate an exe.

     

    Hope this will help

     

    ---Misbah

     

    • Marked as answer by Archana44 Wednesday, December 29, 2010 12:25 PM
    Wednesday, December 29, 2010 7:44 AM

All replies

  • The compiler is not able to find the location of add.h file

    Make sure you have added the correct path of add.h added in "include Directories" in case of diff.c compilation.

     

    You should have one main function in one of the .c file and defination of called function another .c file.

     

    main() is the entry for start of execution for an exe.

     

    You can create a library static or dynamic and call its functions to create an exe. Static and dynamic libraries has its own advantages and disadvantages.

     

    Functions can be exported if it is libraries/modules so that it can be linked at runtime.

     

    Two .c file with two main() cannot crate an exe.

     

    Hope this will help

     

    ---Misbah

     

    • Marked as answer by Archana44 Wednesday, December 29, 2010 12:25 PM
    Wednesday, December 29, 2010 7:44 AM
  • thanks for the reply.i have removed main in one .c file and compiled.i got the answer.but if v have two files wid two main functions and want to access a function in one file from anothr how do v do that?
    Wednesday, December 29, 2010 12:25 PM
  • The answer is that you don't.  You create a third file with the common function.

    Better is to create a library with the common functions and link it into both exes.


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com
    Wednesday, December 29, 2010 1:33 PM
    Moderator
  • You need to think about what you actually want to do.  You want to have two separate programs and you want one to "call" the other?  Why?  Although your question has been addressed, I think you're asking the wrong question.  Tell us what you want to do and give us a chance to help you, since you're obviously inexperienced, architect a solution that makes sense.

    Paul T.

    Wednesday, December 29, 2010 2:55 PM
  • Ok.I understood that we need to create a library for common functions and access those functions from library.I have written a c file called first.c with two functions and another file with main which accesses only one function from first.c.I linked and compiled both files.After executing im getting second function also included even though im not accessing it.what should i do to access only specific functions?and how to create a library from which we can access only specific functions of all functions contained in it?
    Thursday, December 30, 2010 4:59 AM
  • To build a statically linked library, set the TARGETTYPE in your sources file to LIBRARY.
    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com
    Thursday, December 30, 2010 1:45 PM
    Moderator