Você está na página 1de 9

GCC - o bsico

Vamos comear com o compilador...


Eu espero que vocs saibam como compilar um cdigo "na mo", isto ,
sem "ctrl+f9" do seu IDE.
Para aqueles qu no sabem, a sintaxe do gcc :
gcc [opes][arquivos][-o sada (opcional)]

Algumas das opes mais utilizadas:


-Idir --> adiciona um diretrio "dir" onde o compilador procurar por
#includes
-Ldir --> adiciona um diretrio "dir" onde o linker procurar por bibliotecas
-larquivo --> Adiciona um bilioteca a linkagem (*.a, *.lib, *.so, *.dll...),
se no for uma biblioteca "padro", deve-se indicar o diretrio onde ela se
encontra, com -L
No preciso escrever o "lib" nem a extenso do arquivo, suponha que voc
quer incluir a bilbioteca /home/eu/projetos/libteste.so
O seu comando ficaria: gcc -L/home/eu/projetos -lteste
-c --> apenas compila os objetos, no efetua a linkagem
-g --> compila os objetos em debug mode, adicionando informaes que
permitem a
utilizao de um debugger.
-Wopo --> SUA OPO PREFERIDA!!!, habilita os warnings (avisos) do
compilador, so eles que te avisam onde esto os erros do seu cdigo, te
dizem at a
linha... que moleza!
Acho que para j est bom.
Compilando um cdigo
cdigo: http://codepad.org/eOqb8tH6
#include <stdio.h>
#include <stdlib.h>
typedef int TYPE;
typedef struct node
{
TYPE data;
struct node *next;

} node
//Acho que os nomes das funes so bem explicativos...
void add_node(node** root, TYPE value);
void remove_node(node** root, node* n);
node* find_node(node* root, TYPE value);
void destroy_list(node** root);
void print_list(node* root);
int main()
{
node* list = NULL;
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,

1);
2);
3);
4);
5);
6);
7)
8);
9);

print_list(list);
fprintf(stdout, "\nVamos agora apagar alguns ns (7 e
8)...\n\n");
remove_node(list, find_node(list, 7));
remove_node(&list, find_node(list, 8));
print_list(list);
fprintf(stdout, "\nVamos liberar a lista inteira...\n\n");
destroy_list(&list);
fprintf(stdout, (list == NULL)?("Lista liberada\n"):("algo
correu mal...\n"));
return 0;
}
void add_node(node** root, TYPE value)
{
if ((*root) == NULL)
{
(*root) = (node*)malloc(sizeof(node));
(*root)->data = value;
(*root)->next = NULL;
return;
}

node *curr = (*root);


node *newnode = (node*)malloc(sizeof(node));
newnode->data = value;
newnode->next = NULL;
#include <stdio.h>
#include <stdlib.h>
typedef int TYPE;
typedef struct node
{
TYPE data;
struct node *next;
} node
//Acho que os nomes das funes so bem explicativos...
void add_node(node** root, TYPE value);
void remove_node(node** root, node* n);
node* find_node(node* root, TYPE value);
void destroy_list(node** root);
void print_list(node* root);
int main()
{
node* list = NULL;
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,

1);
2);
3);
4);
5);
6);
7)
8);
9);

print_list(list);
fprintf(stdout, "\nVamos agora apagar alguns ns (7 e
8)...\n\n");
remove_node(list, find_node(list, 7));
remove_node(&list, find_node(list, 8));
print_list(list);
fprintf(stdout, "\nVamos liberar a lista inteira...\n\n");
destroy_list(&list);
fprintf(stdout, (list == NULL)?("Lista liberada\n"):("algo
correu mal...\n"));
return 0;
}

void add_node(node** root, TYPE value)


{
if ((*root) == NULL)
{
(*root) = (node*)malloc(sizeof(node));
(*root)->data = value;
(*root)->next = NULL;
return;
}
node *curr = (*root);
node *newnode = (node*)malloc(sizeof(node));
newnode->data = value;
newnode->next = NULL;
while (curr->next != NULL)
curr = curr->next;
}

curr->next = newnode;

void remove_node(node** root, node* n)


{
node *curr = (*root);
if (n == NULL)
return;
if ((*root) == n)
{
(*root) = (*root)->next;
free(curr);
return;
}
while (curr->next != n)
{
if (curr = NULL)
{
fprintf(stderr, "Node: not found in the
list\n");
return;
}
}

curr = curr->next;

curr->next = n->next;
free(n);
}
node *find_node(node* root, TYPE value)
{
node *curr = root;
for (;; curr = curr->next)

{
if (curr->data == value)
return curr;
if (curr == NULL)
{
fprintf(stderr, "value: %d not found in the
list\n", value);
return NULL;
}
}
}
void destroy_list(node** root)
{
node *curr = root;
while (curr != NULL)
{
curr = (*root);
(*root) = (*root)->next;
free(curr);
}

void print_list(node* root)


{
node *curr = root;
while (curr != NULL)
{
fprintf(stdout, "[%x] %d\n", curr, curr->data);
curr = curr->next;
}
}

O que o compilador me disse:


Quote
victor@matrix:~/Desktop/tuto$ gcc -Wall -Wextra -g code.c
code.c:13: error: expected =, ,, ;, asm or __attribute__ before void
code.c:14: error: expected ) before * token
code.c:15: error: expected =, ,, ;, asm or __attribute__ before * token
code.c:16: error: expected ) before * token
code.c:17: error: expected ) before * token
code.c: In function main:
code.c:22: error: node undeclared (first use in this function)
code.c:22: error: (Each undeclared identifier is reported only once
code.c:22: error: for each function it appears in.)
code.c:22: error: list undeclared (first use in this function)
code.c:24: warning: implicit declaration of function add_node
code.c:31: error: expected ; before add_node

code.c:34: warning: implicit declaration of function print_list


code.c:38: warning: implicit declaration of function remove_node
code.c:38: warning: implicit declaration of function find_node
code.c:45: warning: implicit declaration of function destroy_list
code.c: At top level:
code.c:53: error: expected ) before * token
code.c:75: error: expected ) before * token
code.c:105: error: expected =, ,, ;, asm or __attribute__ before * token
code.c:122: error: expected ) before * token
code.c:135: error: expected ) before * token
Notem nas opes que utilizei para compilar o cdigo:
-Wall: dizendo o compilador para mostrar TODOS os warnings
-Wextra: pedindo ainda os warnings extra
-g: debug mode.
E notem que o compilador me falou TUDO que estava errado no cdigo...
A nomenclatura muito simples:
<nome_do_arquivo>:<nmero_da_linha>:<error ou warning>: o que voc fez de errado
Depois de seguir as mensagens dele, temos outro cdigo:
http://codepad.org/tlQGsL0t
#include <stdio.h>
#include <stdlib.h>
typedef int TYPE;
typedef struct node
{
TYPE data;
struct node *next;
} node;
//Acho que os nomes das funes so bem explicativos...
void add_node(node** root, TYPE value);
void remove_node(node** root, node* n);
node* find_node(node* root, TYPE value);
void destroy_list(node** root);
void print_list(node* root);
int main()
{
node* list = NULL;
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,
add_node(&list,

1);
2);
3);
4);
5);
6);
7);
8);

add_node(&list, 9);
print_list(list);
fprintf(stdout, "\nVamos agora apagar alguns ns (7 e
8)...\n\n");
remove_node(&list, find_node(list, 7));
remove_node(&list, find_node(list, 8));
print_list(list);
fprintf(stdout, "\nVamos liberar a lista inteira...\n\n");
destroy_list(&list);
fprintf(stdout, (list == NULL)?("Lista liberada\n"):("algo
correu mal...\n"));
return 0;
}
void add_node(node** root, TYPE value)
{
if ((*root) == NULL)
{
(*root) = (node*)malloc(sizeof(node));
(*root)->data = value;
(*root)->next = NULL;
return;
}
node *curr = (*root);
node *newnode = (node*)malloc(sizeof(node));
newnode->data = value;
newnode->next = NULL;
while (curr->next != NULL)
curr = curr->next;
curr->next = newnode;
}
void remove_node(node** root, node* n)
{
node *curr = (*root);
if (n == NULL)
return;
if ((*root) == n)
{
(*root) = (*root)->next;
free(curr);
return;
}
while (curr->next != n)
{

if (curr == NULL)
{
fprintf(stderr, "Node not found in the
list\n");

return;
}

curr = curr->next;

curr->next = n->next;
free(n);
}
node *find_node(node* root, TYPE value)
{
node *curr = root;
for (;; curr = curr->next)
{
if (curr->data == value)
return curr;
if (curr == NULL)
{
fprintf(stderr, "value: %d not found in the
list\n", value);
return NULL;
}
}
}
void destroy_list(node** root)
{
node *curr = (*root);
while (curr != NULL)
{
curr = (*root);
(*root) = (*root)->next;
free(curr);
}

void print_list(node* root)


{
node *curr = root;
while (curr != NULL)
{
fprintf(stdout, "[%x] %d\n", (unsigned int)curr, curr>data);
}

curr = curr->next;
}

Ao compilar esse novo cdigo.... tcharaaaam! nenhum erro, nem warning!


maravilha!!

Mas e ao executar o programa?