Trying to add my own header files just as C library header files are added to the program containing MAIN using gcc

StackOverflow https://stackoverflow.com/questions/23454141

  •  15-07-2023
  •  | 
  •  

Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top