Can I include a library in a function residing in said library? - c

I am somewhat new to programming and I'm trying to make a project where I make my own function library. Inside of said library I have one function that uses two other functions in it too. Can I #include "library.h" on the function inside library.h to use the other two functions? Thanks in advance =)
Example of some code:
//mylib.h
int func1(int x);
int func2(int x);
int func3();
in func3.c:
int func3()
{
int x1 = func1(int x);
int x2 = func2(int x);
return(x1 + x2);
}
//func3 needs func1 and func2 which are defined in the same library
hope this makes any sense

Of course you can, and you should. It is common sense to include the header file containing the public API of some code in the source file(s) implementing that code. This way the compiler checks for you that the prototypes (and other stuff like declarations) match their counterparts in the implementation source.
For example, the header file "mylib.h" is:
#if !defined(MY_LIB_H)
#define MY_LIB_H
int func1(int x);
int func2(int x);
int func3(int x1, int x2);
#endif
This header file contains the so-called header-guard. It helps against multiple definitions if the header file is include multipl times, for example if you include it in multiple other header files, which are included in some source file.
And its implementation "mylib.c":
#include "mylib.h"
int func1(int x) {
return x;
}
int func2(int x) {
return -x;
}
int func3(int x1, int x2) {
return func1(x1) * func2(x2);
}

Related

Multiple symbols within a function call in a static library

I need to compile a static library which inherits functions with the same name and these functions are furthermore calling each other.
Is there a way to solve my problem?
Here is the minimal example:
add_outer.c
#include "bridge.h"
int add(int a, int b){
return add_bridge(a,b);
}
bridge.c
#include "add_inner.h"
int add_bridge(int a, int b){
return add(a,b);
}
add_inner.c
int add(int a, int b){
return a+b;
}
Now my problems:
I can't change the structure
I can't modify the code at all
It needs to be compiled to a static library
so i can't cheat using a version-script (->shared library)
Instead of compiling them together, write a .c file that will
#define add something_else
then #include the add_bridge.c and add_inner.c

How can I call to function that exists in other file?

Given 2 files, for examples:
file1.c :
int main(){
f();
return 0;
}
file2.c:
void f(){
return;
}
Why I can't call f from file1.c like that?
Because first you need to tell the compiler (declare) that it exists somewhere:
void f(); //function declaration
int main()
{
f();
return 0;
}
Usually, though, it is better to put such declarations in a separate header file (e.g. file2.h) so that later you could include this file (e.g. #include "file2.h") instead of duplicating such declaration in every other file where you need this function.
The problem is that file1.c does not "know" that the function f exists. You need to use a prototype. The standard way is to put prototypes in header files and definitions in .c files.
It could look like this:
file1.c:
#include "file2.h"
int main(){
f();
return 0;
}
file2.h:
#ifndef FILE2_H
#define FILE2_H
void f();
#endif
file2.c:
#include "file2.h"
void f(){
return;
}

How to invoke function from external .c file in 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);
}

Faux getter/setter functions in C returning wrong value

Please help? I'm at a bit of a loss here.
main.c:
int main(){
double x = 12.345;
set_alpha(x);
double y = get_alpha();
printf("%f\n", y);
return 0;
}
block.c:
double alpha;
void set_alpha(double a){
alpha = a;
printf("%f\n", alpha);
}
double get_alpha(){
return alpha;
}
When running gcc block.c main.c, I get
12.345000
183898224.000000
, where the latter number changes at random. What's going on and how do I fix my faux getter/setter functions?
If you don't have prototypes for get and set_alpha then the compiler doesn't know what their parameters and return values are. Each source file is compiled independently. If the prototypes aren't listed in main.c then the compiler has to guess at the function signatures. It wrongly guesses that everything's an int, as in int set_alpha(int a) and int get_alpha(). Oops!
The fix:
void set_alpha(double a);
double get_alpha();
int main() {
...
}
The best thing to do is to create a separate header file block.h and put the prototypes there. Also to make sure to enable all your compiler's warnings (e.g. gcc -Wall) so you don't get bit by this again.
block.h
#ifndef BLOCK_H
#define BLOCK_H
void set_alpha(double a);
double get_alpha();
#endif
main.c
#include "block.h"
int main() {
...
}
block.c
#include "block.h"
...

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