Search code examples
windowsgccbinarymingwld

GCC (minGW - Windows 10) report that there is no reference to symbol defined in .o file when I'm trying to embed the binary into executable


creating .o file

ld -r -b binary -o baked.o baked.txt

λ nm baked.o
0000000f D _binary_baked_txt_end
0000000f A _binary_baked_txt_size
00000000 D _binary_baked_txt_start

Code:

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

extern char *_binary_baked_txt_end;
extern char _binary_baked_txt_size;
extern char *_binary_baked_txt_start;

int main(void) {      
  printf("the baked is %s", _binary_baked_txt_start);      
  return 0;
}

compiling:

λ gcc -o main baked.o main.c
C:\Users\566\AppData\Local\Temp\cc4um6Mn.o:main.c:(.text+0x12): undefined reference to `_binary_baked_txt_start'
collect2.exe: error: ld returned 1 exit status

The question is, what do I do wrong? I see the symbols into .o file, I compile it with main.c file, what could go wrong?


Solution

  • I should use just:

    binary_baked_txt_end;
    binary_baked_txt_size;
    binary_baked_txt_start;
    

    because of the COFF format on Windows machines. and the right way to do will be:

    extern char binary_baked_txt_end[];
    extern char binary_baked_txt_size;
    extern char binary_baked_txt_start[];
    

    because in that way it's clear that binary_baked_txt_end is a adress that points to a char (in my case) and it will be safer, nobody won't be able to assign new value to it.