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

时间:2018-12-19 11:28:53

标签: windows gcc binary mingw ld

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?

1 个答案:

答案 0 :(得分:1)

我应该只使用:

binary_baked_txt_end;
binary_baked_txt_size;
binary_baked_txt_start;

因为Windows计算机上为COFF格式。 正确的做法是:

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

因为这样,很明显binary_baked_txt_end是一个指向char的地址(在我的情况下),它会更安全,没有人无法为其分配新值。

相关问题