Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a object file

Status
Not open for further replies.

bsuribabu

Programmer
Apr 4, 2002
25
IN
Hi,

I am working on Turbo C for DOS .

First I have made a sample.c which contains sample() function and i have generated the objected file sample.obj.

In another file ( test.c) i have opened this file with fopen and this address is casted to funcp function pointer and called the that function pointer.

Is this the correct way of doing? Is this the way how DLLs are work in core level ( Actually i dont know how DLLs work in background )

Following is the my source code


First File :

#include <stdio.h>
void main()
{
printf(&quot;Hai Suri&quot;);
return;
}

Second File :

#include <stdio.h>

FILE *fp;

void main()
{
void (*funcp)();
fp = fopen(&quot;sample.obj&quot;,&quot;rb&quot;);
if ( !fp )
printf(&quot;File Cannot be opened&quot;);
else
{
funcp = ( void (*)()) fp;
funcp();
}
fclose(fp);
}

anyway this program is running but going in infinite loop


Please clarify me .
 
...errm someone somewhere can probably help you a lot better with this than I can.
So far as I know, the object file, because it hasn't been through the linker, has totally unresolved far calls etc. In effect, it's half-baked code, and if you try to treat it like full code, it will contain undefined jumps to unknown locations.

Actually, come to think of it, even near jumps must be unresolved at this stage, because the Turbo linker will optimise and remove functions that are not called anywhere in a program.

Also I'm sure an obj file must contain other, non code stuff so that the linker can get the references of exported variable right. I don't know the file structure for where this information is held.

If you want to use C to make a non-exe file that nevertheless contains runable code, I'd suggest putting all the code you want in a stand-alone C &quot;program&quot;, where all the functions are referred to by pointer somewhere to hoodwink the linker into including all of them, and to make you a table of entry points for the functions you'd like to call. Then compile it to a full DOS exe (if you're working in dos). You can then cut off the header (512 bytes) and you have your own code file that you can treat like a function library.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top