Criando bibliotecas no C

A criação de bibliotecas no C é muito simples e fácil, analisem as bibliotecas do seu compilador, aqui podemos ver o exemplo de uma, a conio.h:

CODE

/* conio.h

Direct MSDOS console input/output.

Copyright © Borland International 1987,1988,1990
All Rights Reserved.
*/
#if !defined(__VIDEO)
#define __VIDEO

#if __STDC__
#define _Cdecl
#else
#define _Cdecl cdecl
#endif

#ifndef __PAS__
#define _CType _Cdecl
#else
#define _CType pascal
#endif

#define _NOCURSOR 0
#define _SOLIDCURSOR 1
#define _NORMALCURSOR 2

struct text_info {
unsigned char winleft;
unsigned char wintop;
unsigned char winright;
unsigned char winbottom;
unsigned char attribute;
unsigned char normattr;
unsigned char currmode;
unsigned char screenheight;
unsigned char screenwidth;
unsigned char curx;
unsigned char cury;
};

enum text_modes { LASTMODE=-1, BW40=0, C40, BW80, C80, MONO=7, C4350=64 };

#if !defined(__COLORS)
#define __COLORS

enum COLORS {
BLACK, /* dark colors */
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY, /* light colors */
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE
};
#endif

#define BLINK 128 /* blink bit */

extern int _Cdecl directvideo;

#ifdef __cplusplus
extern "C" {
#endif
void _Cdecl clreol (void);
void _Cdecl clrscr (void);
void _Cdecl delline (void);
int _Cdecl gettext (int __left, int __top, int __right, int __bottom,
void *__destin);
void _Cdecl gettextinfo (struct text_info *__r);
void _Cdecl gotoxy (int __x, int __y);
void _Cdecl highvideo (void);
void _Cdecl insline (void);
void _Cdecl lowvideo (void);
int _Cdecl movetext (int __left, int __top, int __right, int __bottom,
int __destleft, int __desttop);
void _Cdecl normvideo (void);
int _Cdecl puttext (int __left, int __top, int __right, int __bottom,
void *__source);
void _Cdecl textattr (int __newattr);
void _Cdecl textbackground (int __newcolor);
void _Cdecl textcolor (int __newcolor);
void _Cdecl textmode (int __newmode);
int _Cdecl wherex (void);
int _Cdecl wherey (void);
void _Cdecl window (int __left, int __top, int __right, int __bottom);

void _Cdecl _setcursortype (int __cur_t);
char *_Cdecl cgets (char *__str);
int _Cdecl cprintf (const char *__format, ...);
int _Cdecl cputs (const char *__str);
int _Cdecl cscanf (const char *__format, ...);
int _Cdecl getch (void);
int _Cdecl getche (void);
char *_Cdecl getpass (const char *__prompt);
int _Cdecl kbhit (void);
int _Cdecl putch (int __c);
int _Cdecl ungetch (int __ch);
#ifdef __cplusplus
}
#endif
#endif


Podemos observar que, ela é formada por funções, muitas funções, macros e estruturas.
Quando criamos uma biblioteca, nós podemos criar uma função para ela, por exemplo:

CODE
int soma(num1, num2){

int resultado;
resultado = num1 + num2;

return(resultado);
}


Se salvarmos essa biblioteca como soma.h nós podemos utiliza-la nos programas da devida forma:

CODE
#include "soma.h"


Assim("soma.h") o programa vai procurar a biblioteca no diretorio atual, o diretório do programa.

Mas se quisermos fazer com que ele procure no diretório do compilador, nós colocamos assim:

CODE
#include <soma.h>


Ele irá procurar na pasta em que estão as bibliotecas do compilador, se você não tiver salvo a biblioteca lá, téra que usar o primeiro código.

Usando as funções da biblioteca:

CODE
#include "soma.h"
void main(void){
printf("%i", soma(2, 4)); //obteremos o resultado 4
}

Você gostou? Comente no fórum!

Mais recentes em Outras Linguagens

Não consigo.
Por 2pac - Não consigo ver as aulas da 4 a 9 aula vai direto da...
Tudo sobre manipulação de strings e caracteres
Por fernando777 - Muito completo...
Criando bibliotecas no c
Por fernando777 - Criando bibliotecas no c - - a criação de bibliotecas...
Operador sizeof no c
Por fernando777 - Operador sizeof - - o operador sizeof mostra quanto...
Estrutura if else - se senÃo e operadores lógicos
Por fernando777 - If else e operadores lógicos no c...

Ver mais Artigos de Outras Linguagens.

Ver e retirar outras dúvidas no fórum Webly.

Alguns Direitos Reservados | RSS | O Fórum

Webly Portal e Fóruns - Internet + Humana | Design by ArthurHenrique.com