1

Here are three different files. I am trying to add myfunc.h, just as C Library header files are added to the program containing main, so that functions present in the myfunc.h can be called just by using #include<myfunc.h>.


myfunc.c

float power(float p, float q)
{
  float r,prod;
  int i;
  prod=p;
  for(i=1;i<=q-1;i++)
  prod=prod*p;
  return prod;
}


char prime(int n)
{
  int i;
  for(i=2;i<=n-1;i++)
  {
    if(n%i==0)
    return 'N';
  }
  if(n==i)
  return 'Y';
}



myfunc.h

float power(float p, float q);
char prime(int n);



Practice.c contains the main()

#include <stdio.h>
#include "myfunc.h"

main()
{
  int i,k;

  printf("\nEnter Number = ");
  scanf("%d",&i);

  k=prime(i);

  printf("\n  IS %d PRIME = %c",i,k);
}

Ques : 1 How do I use myfunc.c & myfunc.h in my main program?

Ques : 2 I created & compiled myfunc.h which in return produced myfunc.h.gch file. What is this myfunc.h.gch?


Consider Example program.

#include <stdio.h>

main()
{
  printf("This is an Example.");
}

Did I declare or define printf() inside the main() or anywhere? No, I just called it with value passed in it. But still the program gets compiled & executed successfully.

That's exactly how I want functions in myfunc.c to be called. How can I do that?.

phougatv
  • 881
  • 2
  • 12
  • 29
  • 1
    http://stackoverflow.com/questions/1241399/what-is-a-h-gch-file – SSpoke May 04 '14 at 08:37
  • It is not clear what exact problem you have. You have successfully compiled your program using standard, widely used techniques you should have used. Nothing problematic here whatsoever. Continue just like that. – n. m. could be an AI May 04 '14 at 08:41
  • "After this I created & compiled myfunc.h which in return produced myfunc.h.gch". This one isn't so widely-used. You have built a precompiled header here. I recommend against precompiled headers unless you have a large project that would otherwise take too long to compile. You probably don't want any added complexity associated with them. – n. m. could be an AI May 04 '14 at 09:04
  • @n.m. The **Example** code I wrote in the question can be compiled using `gcc -oExample Example.c` and can be executed just by writing `Example`. But to compile **myfunc.c** I first need to write `gcc -c myfunc.c -omyfunc.o` to create an object file, than compile it by link up with **Practice.c** using `gcc -oPractice Practice.c myfunc.o`.Than the program gets executed. But what I'm trying is to compile & execute the **myfunc.c** for once & be able to call all it's functions just like by writing `#include`. – phougatv May 04 '14 at 09:36
  • Nobody does it, it is not recommended and can lead to all kinds of problems. The C library is a big and complicated thing. Touching it will almost certainly break stuff on your machine. Don't do that. The way you have done it is the normal and accepted way. – n. m. could be an AI May 04 '14 at 09:56
  • You mean what I did to compile and execute the **Practice.c** using `gcc -oPractice Practice.c myfunc.o` is correct to use. @n.m. – phougatv May 04 '14 at 10:07

3 Answers3

1

You can make your own library function file acc to : http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html more info for windows at; Building a shared library using gcc on Linux and MinGW on Windows

Community
  • 1
  • 1
1

After searching and trying a few times, I finally was able to perform the answer of this question. And I would like to share it with you.

                                   Answer 1

myfunc.c contains the definition of the functions(here they are power & prime). By definition I mean, it contains the functions with their actual code, which will be executed when one or both of these functions will be called during runtime.

Whereas, myfunc.h contains the complete declaration of these functions with ;(semicolon) used at the end of every function's declarations.

Steps :

1 : Use the following code for the compilation of the program: gcc -c Practice.c myfunc.c. It is not necessary to compile both files(Practice.c & myfunc.c) together. One can compile them separately: gcc -c Practice.c & gcc -c myfunc.c, later part will consume time, so why make two runs when you can do it in one.

2 : After compilation your current directory contains 2 extra files, namely : Practice.o & myfunc.o. You will need both these files. Use this : gcc Practice.o myfunc.o -oPractice.

3 : After the 2nd step, write Practice in cmd. It will start working.


NOTE : Important part here is myfunc.o. This file can be used with any other C program of your's, containing main function.


For EXAMPLE, if we have the following program using the functions present in the myfunc.c file, than

Test.c containing the main()

#include <stdio.h>
#include "myfunc.h"  //double quotes here states that # will first look for the 
                     //header file in the current directory & later in the C library.
                     //And will replace this line during compilation with
                     //the declaration of functions present in this header file.
                     //This will inform the compiler that there exists a function of
                     //the particular type.


main()
{
  int i,k;

  printf("\nEnter Number = ");
  scanf("%d",&i);

  k=prime(i);          //During the execution of this line, runtime will need an *`object(.o)`*
                       //file with *`Test.c`*. Else your program won't work.


  printf("\n  IS %d PRIME = %c",i,k);
}

Use the following code : gcc Test.c myfunc.o -oTest. You don't need to compile myfunc.c every time, but once.

Read the answers in the following question, for the correct compilation of the source code : LINK

                                      Answer 2

Information about pre-compiled header files can be found on Hottest 'pre-compiled header files' Answers

Community
  • 1
  • 1
phougatv
  • 881
  • 2
  • 12
  • 29
0

Add #include "myfunc.h" in myfunc.c and try gcc -c practice.c myfunc.c myfunc.h gcc -o practice practice.o myfunc.o

Badda
  • 98
  • 7
  • I tried and it worked just fine. But, what exactly will happen when I write this `#include "myfunc.h"` ? Please elaborate. @user3498984 – phougatv May 04 '14 at 09:49
  • With the use of #include "myfunc.h" in myfunc.c, you are telling the compiler, that the functions of myfunc.h are defined in myfunc.c – Badda May 04 '14 at 13:40
  • That means every time I compile I have to write the code that you just wrote, only than I'll be able to execute the program ?? @Badda – phougatv May 04 '14 at 16:09