Mingw objcopy undefined reference

Started by
5 comments, last by Anri 5 years, 10 months ago

Using Mingw for Windows 10, and looking into solutions to add a file as a binary for linking.  I thought I would start simple by adding a text file, called text.txt that contains the message "Hello, world!" , to my program.  I'm using objcopy.exe to do this.

I've managed to create the binary of text.txt okay and using objdump -t text.o, it produces the following symbol table...


text.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    d  .data  00000000 .data
00000000 g       .data  00000000 _binary_text_txt_start
0000000c g       .data  00000000 _binary_text_txt_end
0000000c g       *ABS*  00000000 _binary_text_txt_size

 

...and the main source file contains the following...


#include <stdio.h>

extern char _binary_text_txt_start;
extern char _binary_text_txt_end;
extern char _binary_text_txt_size;

void main()
{
       printf( "\n\nFILE SIZE: %d\n\n", _binary_text_txt_size );
}

 

 

...but then compiling the project produces the following error...


 undefined reference to `_binary_text_txt_size'  collect2.exe: error: ld returned 1 exit status

...which has me confused as the symbol table clearly has _binary_text_txt_size defined.  Just wondered if anyone can shed light on what I am overlooking?

 

Cheers.

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

Advertisement

You're probably being hit by name mangling.  Don't include the initial underscore in your code, the underscore is being silently added by the compiler. 

 

That was definitely part of the problem, Frob.  The other half was forgetting to include the text.o file when compiling.

Cheers matey! ^_^

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

I guess somewhat more self-contained solution is to use incbin from within inline asm.

See e. g. https://www.devever.net/~hl/incbin and  https://gist.github.com/mmozeiko/ed9655cf50341553d282 .

Another option is to generate a C/C++ file with the actual text in a string literal or an array. Works for every C/C++ compiler at every platform without the need for object file tools.

Thanks for the additional suggestions. The text file was merely a test for other kinds of file and even custom ones.  Although I was using Windows 10, I needed a resource-like solution that would work for Linux as well.

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

This topic is closed to new replies.

Advertisement