I am working on writing a library of functions for use in creating applications to run on my OS. I have some functions that I have programmed in assembly and some in C. For now, I compile these into two .o files and put them together in a library file. However, these .o files are inter-dependent and I get linking errors. But, if I do not make the C functions call the assembly functions or vice versa, linking occurs successfully.
So:
1. Can object files in a library be inter-dependent, or is this a no-no?
2. If I had to put some purely assembly functions and some C functions all in one object file, is this possible? (Possible to "combine" the contents of my assembly output file with my C output file?)
I am using nasm to compile my assembly sources and gcc for my C sources.
Thanks a lot
User App Library
Re:User App Library
This is probably harder than it needs to be. It's easier to assemble/compile one source file to one object file, then let the linker sort it all out.Yes. Otherwise no program bigger than 'hello world' would link.HOS wrote: 1. Can object files in a library be inter-dependent, or is this a no-no?2. If I had to put some purely assembly functions and some C functions all in one object file, is this possible? (Possible to "combine" the contents of my assembly output file with my C output file?)
Check the name decoration. If A.O needs a function that's implemented in B.O, do [tt]nm a.o | grep " U "[/tt] to check the names that A.O wants, and [tt]nm b.o | grep " T "[/tt] to check the names that B.O provides. Make sure the names that A.O wants and B.O provides are identical. If A.O and B.O are both written in C, they will be. In A.O is written in C and B.O in assembler, then make sure that you include any _'s in B.O where appropriate.
Re:User App Library
i checked this, and the names are the same, B.O (written in C) just needed one method in A.O (written in assembly), and the underscore was prepended appropriately. i was able to get the user application to link properly by using ld to combine A.O and B.O together prior to putting them in a library (therefore putting in a single object file with no dependencies). i guess that means that the object files in a library can't be dependent on each other, because now it links fine without any code change...
Re:User App Library
No, object files within a library can be interdependent.
One thing to look out for, though, is the linker throwing away object files within a library if they're not used.
For instance, consider XYZ.A which contains files X.O, Y.O and Z.O, in that order. Z.O needs a function from Y.O. You try to link XYZ.A with A.O to create AXYZ.EXE. A.O uses functions in X.O and Z.O, but it does not use anything from Y.O.
The linker looks at each object file in turn. It gets to X.O and says, "I need to keep this file because A.O depends on it". It sees Y.O and says, "I don't need this file because nothing depends on it". It sees Z.O and says "argh! undefined external!".
So maybe that's what's happening. Try reordering the object files within the library so that the ones with the least dependencies are last.
One thing to look out for, though, is the linker throwing away object files within a library if they're not used.
For instance, consider XYZ.A which contains files X.O, Y.O and Z.O, in that order. Z.O needs a function from Y.O. You try to link XYZ.A with A.O to create AXYZ.EXE. A.O uses functions in X.O and Z.O, but it does not use anything from Y.O.
The linker looks at each object file in turn. It gets to X.O and says, "I need to keep this file because A.O depends on it". It sees Y.O and says, "I don't need this file because nothing depends on it". It sees Z.O and says "argh! undefined external!".
So maybe that's what's happening. Try reordering the object files within the library so that the ones with the least dependencies are last.
Re:User App Library
How is linking with static libraries done with ld? For instance I have prog.o and lib.a. What is the correct command line to link these?
Re:User App Library
hey, a question:
I have one library that consists of a.o abd b.o, library is named lib.a, I am creating it with:
ar r lib.a a.o b.o
there is functions that b.o needs inside a.o and there are function definitions in b.o that prog.o needs. When I try to link prog.o with lib.a like:
ld -T prog.ld -o prog.bin prog.o lib.a
ld says that there is a missing symbol that it cant find in prog.o I mean a function that prog.o calls is not implemented according to ld however, I have it in b.o. I tried to be generic as possible to not to create any confusion about code, is there an obvious error here? Also prog.ld is creating a simple flat binary, Im sure its ok. Thanx.
I have one library that consists of a.o abd b.o, library is named lib.a, I am creating it with:
ar r lib.a a.o b.o
there is functions that b.o needs inside a.o and there are function definitions in b.o that prog.o needs. When I try to link prog.o with lib.a like:
ld -T prog.ld -o prog.bin prog.o lib.a
ld says that there is a missing symbol that it cant find in prog.o I mean a function that prog.o calls is not implemented according to ld however, I have it in b.o. I tried to be generic as possible to not to create any confusion about code, is there an obvious error here? Also prog.ld is creating a simple flat binary, Im sure its ok. Thanx.
Re:User App Library
Hey, im sorry I know you have already explained it but it didnt work, my linker knowledge is not very good. im still trying to accomplish it. Well, when I assemble an asm file into coff, I can build library and try to link them but then ld complains about undefined references inside library.
When I assemble assembly file into aout then ld can find references when I try to link individual object files outside library but inside library, library get messed becuase i compile c files into coff format. I think im making a mistake and ld cannot find global references in coff files assembled from asm files??? I hope I could be clear. Thanx.
When I assemble assembly file into aout then ld can find references when I try to link individual object files outside library but inside library, library get messed becuase i compile c files into coff format. I think im making a mistake and ld cannot find global references in coff files assembled from asm files??? I hope I could be clear. Thanx.
Re:User App Library
I can't parse that last paragraph. What does "outside library but inside library" mean?
Get the whole thing to link without using libraries -- stick the whole lot on one command line. Then separate out what should be in a separate library, and run ar on those. If it fails, tweak the ordering of the object files. Repeat until it works.
Get the whole thing to link without using libraries -- stick the whole lot on one command line. Then separate out what should be in a separate library, and run ar on those. If it fails, tweak the ordering of the object files. Repeat until it works.