Jump to content
  • Advertisement
Sign in to follow this  
Anri

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 this post


Link to post
Share on other sites
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. 

 

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!