# Mingw objcopy undefined reference

## Recommended Posts

Posted (edited)

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.

Edited by frob
Do not mark posts as solved. This is a discussion site, not a Q&A site.

##### Share on other sites

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.

##### Share on other sites

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

Cheers matey!

##### Share on other sites

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

##### Share on other sites
Posted (edited)

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.

Edited by Alberth

##### Share on other sites

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.

## Create an account

Register a new account

1. 1
2. 2
frob
15
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004236

×