I'm new to C and programming. I'm on Windows 10, I just installed Dev-C++ and I'm learning how to call functions from other files: i wrote this function to sum two numbers, and I call it from the main script.
The problem is that when i compile the func.c file I get the error in title, so if I run the main file it doesn't recognize the "sum" function. This is the main.c file:
#include <stdio.h>
#include "func.h"
main(){
int x,y,s;
scanf("%d %d",&x,&y);
s = sum(x,y);
printf("\n%d",s);
}
This is the header file:
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED
int func(int a, int b);
#endif // FUNC_H_INCLUDED
And this is the code of the sum function in a func.c file:
#include <stdio.h>
#include "func.h"
int func(int a, int b){
return(a+b);
}
I did read lots of other questions, but they didnt help in my case, or I didnt get the tricky answer. Thank you.
In case you are using Embarcadero's Dev-C++. I use their free compiler since it is "portable".
func.c
and func.h
both contain a function called func
while your main file has a function called sum
, you must have a consistency in the names, you can do s=func(x,y)
and that should work, but a good practice is doing the opposite and replace func
in other files by sum
. And you must compile the 3 files, compiling 1 or 2 isn't enough.
To compile them using Embarcadero's free compiler:
C:\path\to\compiler\bcc32x func.c func.h main.c
It will give you main.exe
that you can run from the command prompt.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.