-1

I have main.c,test.c,test.h and font.h files when i try to print the buffer in font.h in main.c. Its creating compilation timer error. Anything wrong I am done? Please help.

error:

tmp/ccnygNfe.o:(.data+0x0): multiple definition of `c'

/tmp/ccGk8E1o.o:(.data+0x0): first defined here

collect2: error: ld returned 1 exit status

/* Main.c*/


#include <stdio.h>
#include "font.h"
#include "test.h"

int main()
{
    printf("c=%s",c);

    return 0;
}


/*test.c*/

#ifndef _test_
#define _test_

#include "test.h"
#include "font.h"

int add(int a, int b)
{
    return (a+b);
}

#endif

/*test.h*/
extern void add(int a, int b);

/*font.h*/
#ifndef _font_
#define _font_

char c[10]="saji";
extern char c[10];
#endif
dbush
  • 205,898
  • 23
  • 218
  • 273
Jonson
  • 7
  • 4

2 Answers2

2
  1. declare variable in header file, initialize variable in source file.
  • font.h
extern char c[10];
  • font.c
char c[10] = "saji\0";
  1. there is some problem in your test.c. function signature is void, but return int.
KennetsuR
  • 704
  • 8
  • 17
  • 1
    Great answer -- but you can futher tidy it up, as you should not need the #ifndef in the header file when it only have the extern decl – Soren Jan 28 '21 at 04:04
1

In your font.h, you are declaring char c twice:

char c[10]="saji";
extern char c[10];

Move extern char c[10]; into main.c / main.h. The declaration should go into the font.h, and the extern should go into the file where you are using the variable referenced with extern.

TheEagle
  • 5,808
  • 3
  • 11
  • 39