0

I have a lot of binary files from bin2c and when I want to access same array from two c files I get this error (I'm using gcc and make)

/tmp/ccLmMGZb.o:(.data+0x154f4): multiple definition of `first_array'
/tmp/ccDt1r4t.o:(.data+0xd5b8): first defined here
/tmp/ccXFhI7r.o:(.data+0x0): multiple definition of `first_array'
/tmp/ccDt1r4t.o:(.data+0xd5b8): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:120: all] Error 1

here is first.h

unsigned char first_array[] =
{160, 16, 0, 0, 0, 8,....blahblahbla };

file1.c

#include"pathto/first.h"

function(&irelevant1,&irelevant2,&first_array,sizeof(first_array)); //this is decompresion wrapper btw

and file2.c is the same.

I tried to make output bin2c .c(instead of .h) files and make one single header like this

#include"pathto/first.c"

extern unsigned char first_array[];

and include it in file file1.c and file2.c without first.h but it only made it worse,all arrays now got multiple definition

then I made this:

#ifndef FIRST
#define FIRST
#include"pathto/first.c"
extern unsigned char first_array[];
#endif

but result is exaclty the same as in first try.

I tried also lot of random stuff which i don't remember but it also didn't helped,or make even more problems with sizeof(first_array). Any help would be appreciatedn,thanks.

TL;DR i try to include same header in more files and it don't work.

Alireza
  • 100,211
  • 27
  • 269
  • 172
qawsedrf
  • 9
  • 1
  • Don't define the array in the header. Declare it only! `extern unsigned char first_array[];` in the header; your current definition in one of the source files. – Jonathan Leffler Jul 10 '17 at 13:46
  • well that is what i tried,but it don't work – qawsedrf Jul 10 '17 at 13:52
  • There is a header with `#include "pathto/first.c"` in it, which leads to the variable being defined each time the header is included. Drop that `#include` and you should be OK — unless there's something else eccentric going on. Remember: `#include` is basically text copying, and the result of a source file and its `#include`d material is a translation unit (TU). If two different TUs define the variable, you've got problems. – Jonathan Leffler Jul 10 '17 at 13:54
  • yep,thanks it works,just now need handwrite size of array in declaration. – qawsedrf Jul 10 '17 at 13:59
  • No; don't write the size of the array in the declaration. Use the empty bracket notation. At least, don't worry about getting the number in unless it is easy — the compiler won't make use of the information even if you supply it. If you need to know the array size, then you may find `extern size_t first_array_size;` in the header and `size_t first_array_size = sizeof(first_array) / sizeof(first_array[0]);` in the same file that defines the array is more appropriate. – Jonathan Leffler Jul 10 '17 at 14:02

0 Answers0