0

I Make a header file with a function that manipulate cursor positions, text color, etc., and I used it in the main.c and border.c. At first it doesn't have a errors but when I used the pyrecc.h in the border.c it produce an error in my header file

obj\Debug\main.o||In function `gotoxy':|
E:\C PROJECTS\Ropasci\pyrecc.h|10|multiple definition of `gotoxy'|
obj\Debug\border.o:E:\C PROJECTS\Ropasci\pyrecc.h|10|first defined here|

obj\Debug\main.o||In function `textcolor':|
E:\C PROJECTS\Ropasci\pyrecc.h|20|multiple definition of `textcolor'|
obj\Debug\border.o:E:\C PROJECTS\Ropasci\pyrecc.h|20|first defined here|

obj\Debug\main.o||In function `delay':|
E:\C PROJECTS\Ropasci\pyrecc.h|36|multiple definition of `delay'|
obj\Debug\border.o:E:\C PROJECTS\Ropasci\pyrecc.h|36|first defined here|

SCREENSHOT

pyrecc.h

#ifndef PYRECC_H_INCLUDED
#define PYRECC_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>

void gotoxy(int x, int y)
{
    COORD c = {0, 0};

    c.X = x; c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

    return;
}

void textcolor (int setColor)
{
    WORD wColor;

    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
    {
        wColor = (csbi.wAttributes & 0xF0) + (setColor & 0x0F);
        SetConsoleTextAttribute(hStdOut, wColor);
    }

    return;
}

void delay(unsigned int mseconds)
{
    clock_t spd = mseconds+clock();
    while(spd>clock());
}

#endif // PYRECC_H_INCLUDED

border.c

#include <stdio.h>
#include <stdlib.h>
#include "pyrecc.h"

typedef int SCOLOR;

extern const SCOLOR BTborder;
extern const SCOLOR BTtitle;
extern const SCOLOR BTropasci;

extern const SCOLOR WTborder;
extern const SCOLOR WTtitle;
extern const SCOLOR WTropasci;

extern const SCOLOR YTborder;
extern const SCOLOR YTtitle;
extern const SCOLOR YTropasci;

extern SCOLOR Dborder;
extern SCOLOR Dtitle;
extern SCOLOR Dropasci;

void border(int);

void border(int a)
{

  if (a==1)
  {
    system("cls");
  }

  int i;
  gotoxy(5,1);
  textcolor(Dborder);
  for (i = 0; i < 111; i++)
  {
    printf("%c", 205);
  }
  gotoxy(5,28);
  for (i = 0; i < 111; i++)
  {
    printf("%c", 205);
  }

  for (i = 0; i < 24; i++)
  {
    gotoxy(1,3+i);
    printf("%c", 186);
    gotoxy(118,3+i);
    printf("%c", 186);
  }

}

I tried to omit the #include "pyrecc.h" in the border.c file. It runs but it produce a warning in the border.c. E:\C PROJECTS\Ropasci\border.c|34|warning: implicit declaration of function 'gotoxy' [-Wimplicit-function-declaration]|

IM A BEGINNER I DONT IF ITS OKAY TO PUT ANOTHER HEADER IN MY OWN HEADER, I STARTED TO WORK IN MULTIPLE SOURCE FILE AND CUSTOMS HEADER ONLY YESTERDAY LOL

Pyromagne
  • 45
  • 8
  • 2
    `pyrecc.h` is poorly written. Should in general never put external definitions into header files. Headers should only contain function prototypes. The actual implementation should go into a C file, compiled into an object file and that object linked with other objects that call those functions. That is the correct way and will resolve these multiple definition errors. – kaylum Feb 15 '22 at 02:35
  • @kaylum okay so the header I need make is like a compilation of prototype or declaration of function and the definition must be in a `.c` file. My question is where I can make C file for the header? in the same project for my program? what if I want to use it again in another program? – Pyromagne Feb 15 '22 at 02:44
  • 1
    "*what if I want to use it again in another program*". Standard way is to make a library if there is common code that needs to be shared by multiple projects. – kaylum Feb 15 '22 at 02:56
  • @kaylum It is possible to make my own libraries in codeblock or it needs another languages and tools? – Pyromagne Feb 15 '22 at 03:01
  • 1
    Libraries can be made regardless of what IDE is used. But I can't help with codeblocks as I don't use that. – kaylum Feb 15 '22 at 03:22

0 Answers0