0

I want to call a function that reside in my func.c file into my main.c This is my func.c

#include "func.h"
int func(int i) {
return ++i ;
}

This is my header file func.h

int func(int i);

And this is my main code: main.c

#include <stdio.h>
#include <stdlib.h>
#include "func.h"    
main()
{
   int res = func(3);
   printf("%d\n",res);  
system("pause");
}

Compiling the main code I get the error: undefined reference to 'func' My goal is to call function from an external file (that is not part of the same compilation unit). How to do it? Thank you!

1 Answers1

-2
#include <stdio.h>
#include <stdlib.h>
#include "func.h"    // change ( since func.h is not a slandered header file its a user define header file)  
main()
{
   int res = func(3);
   printf("%d\n",res);  
system("pause");
}
rabi shaw
  • 441
  • 1
  • 3
  • 14
  • 1
    The inclusion of the header file has nothing to do with the linker error. It won't cause it, it won't resolve it. – The Paramagnetic Croissant Dec 23 '14 at 10:29
  • as you mentioned in your earlier comments that we can link both the file using gcc -o progname main.c func.c or or use gcc -c main.c but if you try to compile using any of these you will hit the error "func.h no such file or dir" – rabi shaw Dec 23 '14 at 10:34
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – DavidPostill Dec 23 '14 at 10:47
  • @DavidPostill Where do you see the request for clarification? – Tom Dec 23 '14 at 11:41
  • @Tom It is the default text that is added when flagging an answer that doesn't answer the question but may be a valid comment ... – DavidPostill Dec 23 '14 at 11:57
  • @DavidPostill I know that this is a standard text and I still don't know why you've choosed that and why this is not an answer. It might be a bad one, but still an "answer". – Tom Dec 23 '14 at 12:01
  • It doesn't help either. – user3789035 Dec 23 '14 at 12:02
  • @Tom I didn't flag it as "not an answer". That is a different flag. I flagged it because it doesn't actually answer the question, but it **could** be a valid **comment**. – DavidPostill Dec 23 '14 at 12:05
  • @DavidPostill Well, when you can explain why it doesn't answer the question? – Tom Dec 23 '14 at 12:11
  • @Tom Please see the rest of the comments and the edit to the question. `#include "func.h"` hasn't fixed the problem ... – DavidPostill Dec 23 '14 at 12:15
  • @DavidPostill And? As I said, it might be a bad/wrong answer and it might deserve downvotes for that, but this doesn't justify the flags. I guess we both have different opinions in that case and won't get an agreement. – Tom Dec 23 '14 at 12:25