5
I have a problem related to modularization of my project. Do not pay too much attention to the logic of the program, because the biggest problem I face is the fact that the program does not compile. I think the problem lies in the linkage among the archives source and header but I’m not sure.
The project files are below.
// main.cpp
#include <stdio.h>
#include "geometria.h"
#include "interface.h"
int main(void){
char carac ;
unsigned valor, raio, altura , lado1, lado2;
Apresentacao();
printf("***MENU DE OPCOES***\n");
printf("1-Area de um circulo\n");
printf("2-Volume de um cilindro\n");
printf("3-Volume de um cone\n");
printf("4-Area de um retangulo\n");
printf("5-sair do programa\n");
scanf("%u", &valor);
valor = LeOpcao( 1, 5 );
valor = LeValor();
while( valor != 5 ) {
printf("\nOvalor lido foi : %u\n", valor);
carac = (char) valor;
switch( valor ){
case '1': printf("Digite o valor do raio: ");
scanf("%u", &raio);
printf("\n\nA area do circulo e %u\n", AreaCirculo( raio ) ) ;
break;
case '2': printf("Digite o valor do raio: ");
scanf("%u\n", &raio);
printf("Digite o valor da altura: ");
scanf("%u", &altura);
printf("\n\nO Volume do cilindro e de %u\n", VolumeCilindro( raio ,altura ));
break;
case '3': printf("Digite o valor do raio: ");
scanf("%u", &raio);
printf("Digite o valor da altura: ");
scanf("%u", &altura);
printf("\n\nO Volume do cilindro e de %u\n", VolumeCone( raio ,altura ));
break;
default: printf("Didite o valor do primeiro lado: ");
scanf("%u\n", &lado1);
printf("Didite o valor do segundo lado: ");
scanf("%u", &lado2);
printf("\n\nA area do quadrado e de %u ", AreaRetangulo( lado1 , lado2 ));
}
}
return 0;
}
.
//geometria.c
#include <stdio.h>
#include "geometria.h"
static unsigned area, volume ;
unsigned AreaCirculo( unsigned raio ){
area = 2*3,14*raio*raio ;
return area;
}
unsigned VolumeCilindro( unsigned raio , unsigned altura ){
volume = 2*3,14*raio*raio*altura ;
return volume;
}
unsigned VolumeCone( unsigned raio , unsigned altura ){
volume = 2*3,14*raio*raio*altura/3 ;
return volume;
}
unsigned AreaRetangulo( unsigned lado1 , unsigned lado2 ){
area = lado1*lado2 ;
return area;
}
.
//geometria.h
#ifndef GEOMETRIA_H_INCLUDED
#define GEOMETRIA_H_INCLUDED
unsigned AreaCirculo( unsigned raio );
unsigned VolumeCilindro( unsigned raio , unsigned altura );
unsigned VolumeCone( unsigned raio , unsigned altura );
unsigned AreaRetangulo( unsigned lado1 , unsigned lado2 );
#endif // GEOMETRIA_H_INCLUDED
.
// interface.c
#include <stdio.h>
#include <stdarg.h>
#include "interface.h"
void LimpaBuffer(void){
int valorLido;
do{
valorLido = getchar();
} while ((valorLido != '\n') && (valorLido != EOF));
}
void ApresentaMenu( int nItens , int menorOpcao , ... ){
int i;
va_list argumentos;
/*inicia lista de argumentos variáveis*/
va_start ( argumentos , menorOpcao );
for( i =0 ; i < nItens ; ++i ){
printf("%c-%s", menorOpcao++ , va_arg(argumentos, char * ) );
}
va_end(argumentos);
}
void Apresentacao(void){
printf("\n\n\n");
printf("***GEOMETRIA***");
printf("\n\n\n");
printf(" Esse programa tem como proposito fazer calculo de uma serie ");
printf("de opcoes apresentadas abaixo no menu. \n\n");
}
int LeOpcao( int menorValor, int maiorValor ){
int op;
while(1){
if( op >= menorValor && op <= maiorValor ){
LimpaBuffer();
break;
}
else{
printf("\nOpcao invalida. Tente Novamente.");
printf("\nA opcao deve estar entre %c e %c.\n", menorValor, maiorValor);
LimpaBuffer();
}
}
}
long unsigned LeValor(void){
long valor;
unsigned teste;
teste = scanf("%ld", &valor);
while( !teste || valor < 0 ){
if(teste){
printf("\nO valor %ld nao e valido", valor);
}
else{
printf("\nO valor introduzido nao e valido");
}
printf("\nIntroduza um numero maior que zero: ");
LimpaBuffer();
teste = scanf("%ld", &valor);
}
LimpaBuffer();
return valor;
}
.
//Interface.h
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
void LimpaBuffer(void);
void ApresentaMenu( int nItens , int menorOpcao , ... );
void Apresentacao(void);
int LeOpcao( int menorValor, int maiorValor );
long unsigned LeValor(void);
#endif // INTERFACE_H_INCLUDED
I have tried everything to resolve the following problems listed below. All related to functions imported to the main file. c.
$ ls
bin geometria.cbp geometria.layout main.c
geometria geometria.depend interface.c main.cpp
geometria.c geometria.h interface.h obj
$ gcc -o testit main.c
/tmp/cccNrsAz.o: na função `main':
main.c:(.text+0x18): referência indefinida para `Apresentacao'
main.c:(.text+0x79): referência indefinida para `LeOpcao'
main.c:(.text+0x7e): referência indefinida para `LeValor'
main.c:(.text+0xe9): referência indefinida para `AreaCirculo'
main.c:(.text+0x158): referência indefinida para `VolumeCilindro'
main.c:(.text+0x1c7): referência indefinida para `VolumeCone'
main.c:(.text+0x233): referência indefinida para `AreaRetangulo'
collect2: error: ld returned 1 exit status
I don’t know what the real problem is that linkage archival source and header. I’m currently using Code Blocks, but I’ve tried creating in separate files (outside the IDE project, by gedit) and linka-them by the console, but without success.
The real problem is that everyone’s first program is something simple, just the
main()
, in a file. There you add things little by little and see what happens. That’s how you learn. That’s how you learn to find mistakes that are more important than writing code. Then even if you can’t find the problem it becomes easier to ask somewhere what the problem is that is most contained and you don’t need to put long codes that don’t matter much to the problem.– Maniero
You showed several files, minus the
main.c
- which is where the problem is occurring! Are sure that’s the same file you want to compile, and not themain.cpp
?– mgibsonbr
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero