0

i have created a Makefile to run C program in shell script.but i get as error fatal error: addFunc.h: No such file or directory in mainProg.c page and addFunc.c page.i tried my best to solve this problem.but i didn't get a solution.i have mentioned my code below.

mainProg.c

#include <stdio.h>
#include "/home/name/Desktop/add/addFunc.h"
int main(){
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("sum:%d\n",add(a,b));
return 0;
}

addFunc.h

int add(int a, int b)

addFunc.c

#include "/home/name/Desktop/add/addFunc.h"
int add(int a,int b){
return (a+b);
}

Makefile

Add: mainProg.c addFunc.c
    gcc -o Add mainProg.c addFunc.c -I.
SRa
  • 23
  • 2
  • 7

2 Answers2

1

You really should be cutting and pasting, because clearly the text you've pasted here is nothing like what you're actually using (you have tons of syntax errors here).

However, the problem is that the compiler can't find your header file. This can be solved in one (or both) of two ways:

First, you should use the #include <...> form only for system headers like stdio.h etc. For your personal headers, you should use the form #include "..." instead. The difference is implementation-specific but for most compilers it is that include files using <...> are never looked for in the current directory but only in system directories and directories given with -I, while include file using "..." are looked for also in the current directory.

You could also simply ask the compiler to always look in the current directory by adding the -I. option to your compile line in your makefile.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I didn't say add `-I` I said add `-I.` (note the period here: a period means "current directory", just like `..` (two periods) means the parent directory, etc. – MadScientist Dec 26 '17 at 20:46
0

It seems the problem you are getting is occurring because you are using #include <addFunc.h>. You would use <> when you are using a system header file. From your makefile it seems you are not.

The #include statement is a little different when you use header files of your own program because it searches in the dictionary your .c file is located.

Using #include "addFunc.h" should do the trick.

For more info on the #include directive see this page

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • They are using `&` with `scanf`. That's necessary. – MadScientist Dec 26 '17 at 20:32
  • now i get as error `mainProg.c:3:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token int main(){ ^ mainProg.c:9:1: error: expected ‘{’ at end of input } ` – SRa Dec 26 '17 at 20:32
  • @SRa That's because you're missing a semicolon in your header file, I think. – MadScientist Dec 26 '17 at 20:34
  • @SRa Yes you never closed the main function. Add a `}` at the next line of `return 0;` – Xantium Dec 26 '17 at 20:34
  • I guess I was wrong when I thought you just had mistyped your examples due to all the syntax errors... you actually do have all those syntax errors in your code!! – MadScientist Dec 26 '17 at 20:35
  • @MadScientist This is confusing are you talking to me or SRa? – Xantium Dec 26 '17 at 20:36
  • I'm talking to @SRa – MadScientist Dec 26 '17 at 20:45
  • @SRa as I said in a comment above, the problem is not the missing close brace (although that is also a problem and does need to be fixed), the error messages you are reporting in your comment above are caused by not having a semicolon after the function declaration in your header file `addFunc.h`. – MadScientist Dec 26 '17 at 20:53
  • 1
    @SRa I just created a short example locally by cutting and pasting your code above and (a) adding the missing close brace and (b) adding the missing semicolon, and the `mainProg.c` file compiled fine. So, you must have done something wrong when you fixed it. You should update your question, or probably ask a new question, with the current version of the code cut/pasted in. – MadScientist Dec 26 '17 at 21:01
  • 1
    @SRa there is still no semicolon at the end of the function declaration in the `addFunc.h` file, which is what I said the problem was. Until you add that semicolon you will always get compiler errors. Also, it's always a bad idea to include hardcoded paths in your `#include` directives: if you ever move the code or change the directory name it won't compile anymore. – MadScientist Dec 26 '17 at 21:09