How to invoke function from external .c file in C? - c

My files are
// main.c
#include <ClasseAusiliaria.c>
int main(void) {
int result = add(5,6);
printf("%d\n", result);
}
and
// add.c
int add(int a, int b) {
return a + b;
}

Use double quotes #include "ClasseAusiliaria.c" [Don't use angle brackets (< >) ]
And I prefer to save the file with .h extension In the same directory/folder.
TLDR:
Replace #include <ClasseAusiliaria.c> with
#include "ClasseAusiliaria.c"

Change your Main.c like so
#include <stdlib.h>
#include <stdio.h>
#include "ClasseAusiliaria.h"
int main(void)
{
int risultato;
risultato = addizione(5,6);
printf("%d\n",risultato);
}
Create ClasseAusiliaria.h like so
extern int addizione(int a, int b);
I then compiled and ran your code, I got an output of
11

You must declare
int add(int a, int b); (note to the semicolon)
in a header file and include the file into both files.
Including it into Main.c will tell compiler how the function should be called.
Including into the second file will allow you to check that declaration is valid (compiler would complain if declaration and implementation were not matched).
Then you must compile both *.c files into one project. Details are compiler-dependent.

make a file classAusiliaria.h and in there provide your method signatures.
Now instead of including the .c file include this .h file.

There are many great contributions here, but let me add mine non the less.
First thing i noticed is, you did not make any promises in the main file that you were going to create a function known as add(). This count have been done like this in the main file:
int add(int a, int b);
before your main function, that way your main function would recognize the add function and try to look for its executable code.
So essentially your files should be
Main.c
int add(int a, int b);
int main(void) {
int result = add(5,6);
printf("%d\n", result);
}
and
// add.c
int add(int a, int b) {
return a + b;
}

You can include the .c files, no problem with it logically, but according to the standard to hide the implementation of the function but to provide the binaries, headers and source files techniques are used, where the headers are used to define the function signatures where as the source files have the implementation. When you sell your project to outside you just ship the headers and binaries(libs and dlls) so that you hide the main logic behind your function implementation.
Here the problem is you have to use "" instead of <> as you are including a file which is located inside the same directory to the file where the inclusion happens. It is common to both .c and .h files

you shouldn't include c-files in other c-files. Instead create a header file where the function is declared that you want to call.
Like so:
file ClasseAusiliaria.h:
int addizione(int a, int b); // this tells the compiler that there is a function defined and the linker will sort the right adress to call out.
In your Main.c file you can then include the newly created header file:
#include <stdlib.h>
#include <stdio.h>
#include <ClasseAusiliaria.h>
int main(void)
{
int risultato;
risultato = addizione(5,6);
printf("%d\n",risultato);
}

write main.c like this -
caution : while linking both main.0 and ClasseAusiliaria.o should be
available to linker.
#include <stdlib.h>
#include <stdio.h>
extern int addizione(int a, int b)
int main(void)
{
int risultato;
risultato = addizione(5,6);
printf("%d\n",risultato);
}

Related

How To Include Header Files In C

I don't know how to include my own header files in my source file.
I declare addition in my header file(myhead.h):
int addition(int a, int b);
In the source file I define it(myhead.c):
int addition(int a, int b){
return a+b;
}
In the third source file I include the header file and use addition(processSimulator.c):
#include "myhead.h"
#include <stdio.h>
int main(){
printf("Compiled %d\n", addition(3,5));
}
It gives me this error
You need to add myhead.c file to gcc arguments gcc processSimulator.c my head.c

Functions and variables scope in C

Why we don't use extern when using function from one .c file in another .c file , but we must do extern for variables case? Is it related to linker?
Functions are extern qualified by default (unless you change it to internal with static). For example,
int func(void) {
}
extern int func2(void) {
}
Both func and func2 are external. The extern keyword is optional for external functions.
Actually, function names act just like variable names, but function prototypes are extern by default.
From cpprerefence:
If a function declaration appears outside of any function, the identifier it introduces has file scope and external linkage, unless static is used or an earlier static declaration is visible.
you can create a .hfile,declare functions you want to use in the other .c files and #include the .hfile in the other .c files.
Demo program,
one.c
#include "one.h"
void func1() //defination
{
//code
}
one.h
void func1(); //declaration
main.c
#include <stdio.h>
#include "one.h"
int main()
{
func1();
}
Then compile program in Gcc Linux : gcc main.c one.c
Yes, Let consider you have one .c file as process.c and you declared it in process.h . Now if you want to use the function from process.c to suppose tools.c then simply #include "process.h" in tools.c and use ther function. The process.h and process.c file should be in your project.
process.c file
#include<stdio.h>
#include<conio.h>
#include "process.h"
unsigned int function_addition(unsigned int a, unsigned int b)
{
unsigned int c = 0;
c = a + b;
return c;
}
process.h:
<bla bla bla >
unsigned int function_addition(unsigned int a, unsigned int b);
<bla bla bla >
tools.c file:
#include<stdio.h>
#include<conio.h>
#include "process.h"
my_tools()
{
unsigned int X = 1, Y = 9, C = 0;
C = function_addition(X,Y);
}
All these files are in one project.

How to make GCC warn unmatched number of arguments in function calls and function definition?

I have two files.
The first file contains the function prototype and the main function calls myfunc with only one argument:
int myfunc (int x);
int main ()
{
int x =5;
myfunc(x);
}
and the second file contains the function definition but with 2 arguments:
int myfunc (int x, int y)
{
return x+y;
}
When I tried to compile this two files using GCC I got no errors or warnings.
How to force GCC to warn about something like this??
Put your prototypes in a header file, and #include the header file in all source files which use the functions.
GCC compiles each file independently, so it cannot know that the definition of the function does not correspond to the declaration unless the declaration is also included in the file with the definition.
It should look like this:
myfunc.h
#ifndef MYFUNC_H
#define MYFUNC_H
int myfunc (int x);
#endif
myfunc.c
#include "myfunc.h"
int myfunc (int x, int y)
{
return x+y;
}
main.c
#include "myfunc.h"
int main ()
{
int x =5;
myfunc(x);
}
Because the two source files are two different translation units that are compiled completely separately, it's impossible for the compiler to know about this. And as C symbols doesn't really have information about arguments, the linker can't warn about this either.
The only solution is to put function prototypes in header files that are included in all relevant source files.

Use of extern: getting 'file not found'

//fpoin1.c
#include<stdio.h>
#include<conio.h>
#include<fpoin.c>
void swap(int,int);
void main()
{
int i=0;
i++;
if(i<=5)
{
printf("%d",i);
swap(59,23);
getch();
}
//fpoin.c
#include<stdio.h>
#include<conio.h>
extern int i;
void swap(int ,int );
int main()
{
int i=3;
int p,q;
swap(p,q);
printf("\np=%dq=%d",p,q);
getch();
return 0;
}
void swap(int p,int q)
{
int t=p;
p=q;
q=t;
}
When I compile fpoin1, it says "fpoin.c not found", but both are in same directory.
What is missing?
This should fix it:
#include "fpoin.c"
Using "" or <> affects how the compiler searches for a file/header. You should use <> for "system" and "library" includes, and use "" when including your own files.
But, you shouldn't include a .c file. You should compile each of these, then link them together, using e.g. this gcc one-liner: gcc -Wall -o appname fpoin.c fpoin1.c
You will also need to decide which of your two main() functions you want to use. You must have exactly one main()
Additionally, your swap function will not work as you expect, since you pass its arguments by value. Try this:
void swap(int * p, int * q) {
int t=*p;
*p=*q;
*q=t;
}
...
swap(&p, &q)
Of course, you cannot then call swap(59,23) - There's no variables to modify. What was swap(59,23) supposed to do?
One normally only includes .h files, and links other .c files, but if you are going to include a local file, it should be done with "..." and not <...> delimiters, so:
#include "fpoin.c"
The <...> indicates a system header.
In your case, just switching to linking won't quite be good enough, as your main() will be multiply-defined either way. You might want to rethink your modularization a bit.
Using < and > in the #include says to the compiler that you are including a "standard header". Standard headers need not be real files on your disk: the compiler uses magic to include standard headers.
To include real files, use quotes in the #include line
#include "fpoin.c"
One more thing: don't get used to including code. Write files with declarations only and include them (tipically files with .h extension).

Calling function from main() function in C using Eclipse IDE

I am not familiar to Eclipse IDE. I want to know how can O call various functions from a single source file that contains a main() function.
For Example
Project name - TestProject
Source File name - EntryPoint.c
Now I want to make various method like add(), Sub(), mul() etc.
Please tell me these method should be in another source file or a file.
If you want to call a function that is defined in another source file, you still need to declare the function in the source file you are calling it from. This is commonly done through the use of header files which are included at the top of each source file that refers to the function.
Here is an example.
methods.c
#include "header.h"
int add(int a, int b){
return a+b;
}
int sub(int a, int b){
return a-b;
}
int mult(int a, int b){
return a*b;
}
EntryPoint.c
#include "header.h"
int main(){
return sub(add(2, 3), mult(2,5));
}
header.h
#ifndef _HEADER_H
#define _HEADER_H
int add(int, int);
int sub(int, int);
int mult(int, int);
#endif
See setup guide http://tylorsherman.com/hello-world-eclipse.
You can call add and other function as you call in c++ , so nothing special is required.

Resources