Saltar al contenido
Underc0de /foro

How to create your own (basic) libraries in C and how to use them!

🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
Jhonjhon_123 ·
Well, this is easier to do than to write...<br /><br />first of all, let's assume we have created the following function:<br /><br />
Código: c
int suma(int x, int y);   // prototipo de la función <br /><br />suma(x,y)                 // cuando no indicamos el tipo de dato se le adjudica<br />                               // el tipo por defecto "int"<br />{<br /> return(x+y);             //retorno de la función<br />}   
  <br /><br />ok, we go to our compiler and save the function with the .h extension<br />which is the extension libraries use, and then we save it in the include folder of our compiler...<br />In my case I use Dev-C++, so it's:<br /><br />
CitarC:\Dev-Cpp\include
<br /><br />Now let's suppose this is the program through which we'll call our function:<br /><br />
Código: c
#include <stdio.h><br />#include <libreria.h><br /><br />main()<br />{<br />      int a,b,res;<br />      printf("ingresa un numero: "); <br />      scanf("%d",&a);<br />      printf("\nIngresa otro numero: ");<br />      scanf("%d",&b);<br />      res=suma(a,b);<br />      printf("la suma de los dos numeros es: %d \n", res);<br />      system("pause");<br />}
<br /><br />Conclusion:<br />When we use functions called from a library, it isn't necessary to include the function or the prototype inside our program; that is, we can keep our commonly used functions in a library and that way avoid having huge amounts of code in our programs.<br /><br />Well, I hope this was clear, and any doubts or questions go in this same thread.<br /><br />Author: S[e]C<br /><br />Regards!
t0d_mephis ·
You outdid yourself!!! with one read I didn't understand everything perfectly! but I'll study it well and apply it! thanks for your contribution!!!!
Alejandro_N12 ·
Great contribution, it's also not necessary to include the prototype if you do everything together
JAG ·
Very good tutorial, it's true that it's easier to do it than to explain it hehehe<br />I've also noticed you have a small mistake in the explanation... when we include a library such as stdio.h it is done with <stdio.h>, but when including your own the way to do it is #include "libreria.h", that is, using quotes... I guess it was a silly oversight... try to fix it so as not to cause confusion ok? Otherwise it's very good, Regards!!
mil3 ·
Cita de: JAG en Octubre 24, 2012, 03:28:37 AM<br />Very good tutorial, it's true that it's easier to do it than to explain it hehehe<br />I've also noticed you have a small mistake in the explanation... when we include a library such as stdio.h it is done with <stdio.h>, but when including your own the way to do it is #include "libreria.h", that is, using quotes... I guess it was a silly oversight... try to fix it so as not to cause confusion ok? Otherwise it's very good, Regards!!<br />
<br />If you write #include "libreria.h" you're telling the preprocessor that the header is in the directory where the file being compiled is located. On the other hand, by changing "libreria.h" to <libreria.h> we're telling the preprocessor that the header is in the default location, let's call it, where the standard libraries are located, for example.
85 ·
Actually, portable libraries use a .h file only for the prototypes; to distribute a library it is done together with the .lib file. The function's code would be in the .lib (static library) and the .h serves to reference the function in the code, although you must specify that linking be done against the static .lib library in question.<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />
Grupo de WhatsApp