Ich habe ein C-Projekt und ich verstehe wirklich nicht, wie man .c und .h verwaltet. Ich habe eine Methode seit Jahren verwendet, aber jetzt muss ich Codes ohne Warnungen erstellen, also muss ich die Art ändern, wie ich es tue.Wie passt man .c und .h richtig an?
Hier ist, wie ich es so mache:
Jeder .c eine .h mit dem gleichen Namen haben. Sogar main.c hat ein main.h.
In jeder .c, schließe ich die passende .h, also habe ich nur eine Include pro .c. In main.h schließe ich alle Bibliotheken ein, die ich brauche, wie zB stdio stdlib und so weiter. Ich erkläre auch dort meine Aufzählungen und Strukturen.
Schließlich schließe ich in jeder anderen .h, die Main.h, die die Strukturen, enums und Bibliotheken enthält.
Ich verstehe das, indem ich main.h viel Zeit rufe, aber ich benutze die #ifndef
und #define
in jedem .h. Auch ich .h enthält jeweils die Prototypen der Funktionen in ihren entsprechenden .c
Wie soll ich .c und .h verwalten?
habe ich vergessen zu erwähnen, dass das Problem ist, dass ich eine Warnung für jede einzelne Funktion bekommen: „implizite Deklaration der Funktion“
Hier ist ein Beispiel mit vier Dateien:
main.c:
#include "main.h"
int main()
{
// code
}
int save(Player players[2], int currentPlayer)
{
// code
}
int load(Player players[2], int* currentPlayer)
{
// code
}
main.h:
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <string.h>
#include <math.h>
typedef enum Direction Direction;
typedef enum Orientation Orientation;
typedef enum ShipType ShipType;
typedef enum CaseType CaseType;
typedef enum Status Status;
typedef enum PlayerType PlayerType;
enum Direction
{
Nord=0, Est=1, Sud=2, Ouest=3
};
enum Orientation
{
None=0, Horizontal=1, Vertical=2
};
enum ShipType
{
SousMarin=1, Destroyer=2, Croiseur=3, Cuirasse=4
};
enum CaseType
{
Undiscovered=0, Empty=1, Hit=2, Detected=3, Discovered=5
};
enum Status
{
Undamaged=0, Damaged=1, Drowned=2
};
enum PlayerType
{
Human=0, Computer=1
};
typedef struct Map Map;
typedef struct Ship Ship;
typedef struct Player Player;
struct Map
{
int width, height;
int* cases;
};
struct Ship
{
int x, y, length, firstShoot, color;
int hasBeenDiscovered;
Orientation orientation;
ShipType type;
Status status;
};
struct Player
{
int totalShips;
int activeShips;
Map map[2];
char lastMoves[5][128];
Ship* ships;
PlayerType type;
int shipcolor[4];
int shipNumber[4];
int color;
};
int save(Player players[2], int currentPlayer);
int load(Player players[2], int* currentPlayer);
#endif // MAIN_H
IA.c:
#include "IA.h"
int IA(Player* IA, Player* enemy)
{
// code
}
// And all my other functions
IA.h:
#ifndef IA_H
#define IA_H
#include "main.h"
int IA(Player* IA, Player* enemy);
void endIATurn(Player* IA);
int enemyShipDiscovered(Map map, int* x, int* y, int SousMarinSpecific);
Ship* bestFriendlyActiveShip(Ship* ships, int totalShips, int SousMarinSpecific);
int friendlyShipDiscovered(Ship** ship, Ship* ships, Map map, int totalShips, int SousMarinSpecific);
int hitShipButNotDrowned(Player* IA, Player enemy, int totalShips, int* x, int* y);
int hasAtLeastOneMoveableShip(Player player);
int hasUndiscoveredCases(Player player);
void getUndiscoveredCoordoonnees(Map map, int* x, int* y);
int findNextCaseToShootAt(Map map, Ship ship, int* a, int* b);
#endif // IA_H
All meine andere .h sind wie IA.h
In Anbetracht der Art und Weise, wie Sie mit den Dateien umgehen, erwartete ich eher eine * doppelte * Deklaration ... Könnten wir einen Ausschnitt aus einem '.c' und dem dazugehörigen' .h' sehen? –
Und Sie haben diese Warnungen für _years_ erhalten und nicht gedacht, bis jetzt etwas darüber zu tun? Wenn Sie eine "implizite Deklaration" haben, kann der Compiler die Deklaration zum Zeitpunkt, an dem eine Funktion aufgerufen wird, nicht sehen - irgendwie haben Sie den Header nicht eingefügt oder der Header ist nicht korrekt.Es ist bei weitem nicht klar aus Ihrer Textbeschreibung, was Sie tun; poste einige Beispiele. – Clifford
post a [mcve] bitte – bolov