2
I have a program that’s divided into your main
, city.c
and city.h
.
Main:
#include "city.h"
int main()
{
FILE *entrada;
FILE *saida;
Cidade *cidades;
entrada = fopen("entrada.c", "r");
int numeroCidades = LeNumeroDeCidades(entrada);
LeCidades(entrada, &cidades, numeroCidades);
fclose(entrada);
saida = fopen("saida.c", "w");
Possibilidades(saida, numeroCidades);
int sequencia[numeroCidades];
int totalViagens = TotalViagens(numeroCidades);
MenorRota(saida, cidades, sequencia, numeroCidades, totalViagens);
fclose(saida);
return 0;
}
City. c:
#include "city.h"
(Todas as funções e seus corpos)...
City. h:
#ifndef CITY_H_INCLUDED
#define CITY_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define INFINITO 9999999
/*----STRUCT----*/
typedef struct
{
int x;
int y;
} Cidade;
/*---------FUNÇÕES---------*/
void CriaCidade(Cidade *, int , int);
int LeNumeroDeCidades(FILE *);
void LeCidades(FILE *, Cidade **, int);
float Distancia(Cidade *, int, int *);
int TotalViagens(int);
void Troca(int *, int *);
void GeraRotaseDistancias(FILE *, Cidade *, int *, int, int, int, float *, float *);
void CriaSequencia(int, int *);
void MenorRota(FILE *, Cidade *, int *, int, int);
void Possibilidades(FILE *, int);
#endif // CITY_H_INCLUDED
When compiling, I get the same problem for all functions:
||=== Build: Debug in city (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
C:\Users\windows\city\main.c|13|undefined reference to `LeNumeroDeCidades'|
C:\Users\windows\city\main.c|14|undefined reference to `LeCidades'|
C:\Users\windows\city\main.c|19|undefined reference to `Possibilidades'|
C:\Users\windows\city\main.c|21|undefined reference to `TotalViagens'|
C:\Users\windows\city\main.c|22|undefined reference to `MenorRota'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|
I would like to know what is causing this problem.
Link error. Know Makefile?
– Jefferson Quesado
I don’t know, Jefferson
– Renan
Compile using Codeblocks
– Renan
If your project is only these 3 files, and you are using Linux, try running this line in the project folder, where the files are:
gcc -o main city.c main.c
and see if it works– gfleck
It’s good to know the life cycle of compiling a program in C. This answer talks about this: https://answall.com/a/213804/64969 I think you can teach codeblocks how to use a Makefile to compile (the Makefile is the focus of the linked answer question). I believe that codeblocks also has a project configuration without any Makefile, which in this case is not configured to link the
main
with other objects files– Jefferson Quesado
The strange thing is that I have done this way of separating the files . c and . h other times and never gave this kind of problem. But with this example, it was wrong. This is normal?
– Renan
Placing the function bodies in the file . h, the problem ceases to exist. There is an explanation?
– Renan
Yes, there is an explanation. You read the answer I wrote? There is the explanation, and also why this is a freak of nature. In short? Let header have statement only, no implementation
– Jefferson Quesado
Related: https://answall.com/a/243516/64969
– Jefferson Quesado
You created the project in Codeblocks or just tried to compile/run main. c?
– Rafael Coelho
I created the project yes. I managed to fix the problem. Thank you all
– Renan