5
Contributors
9
Replies
13
Views
5 Years
Discussion Span
Last Post by Moschops
Featured Replies
  • WaltP has challenged me on this, with his reputation comment. I stand by my original line. I can take a C source file, compile it into an object, and I'm done - that object file is then a library. A standard *.a file is no more than a bunch of … Read More

0

How to do that all depends on the compiler and/or IDE you are using. Basically, a library is nothing more than a collection of compiled functions that are in a file with *.lib or *.a file name. *nix computers also have something called shared libraries, which has a *.so file name.

Edited by Ancient Dragon: n/a

1

WaltP has challenged me on this, with his reputation comment.

I stand by my original line. I can take a C source file, compile it into an object, and I'm done - that object file is then a library. A standard *.a file is no more than a bunch of these object files stuck together; a single object file is perfectly usable as a library.

Comments
I agree
1

So there is some interpretation there. Sure, an object file can be compiled into an executable but it cant be 'linked' to in the traditional sense without converting it to a .a archive. (I'm talking purely Linux here as I am less familiar with how Windows handles this)

So an object file isn't technically a library even though it can be compiled into an executable in certain contexts.

0

@Ancient Dragon: I'm curious how you do this.

On Linux I get the following:

# Compiling object file directly into binary
$ gcc -c t1.c
$ gcc t2.c t1.o

# Trying to link to .o file
$ gcc -c t1.c -L. -lt1.o
/usr/bin/ld: cannot find -lt1.o
collect2: ld returned 1 exit status

$ cp t1.o libt1.o
$ gcc -c t1.c -L. -lt1
/usr/bin/ld: cannot find -lt1
collect2: ld returned 1 exit status

# Create the static library
$ ar -cvq libt1.a t1.o
$ gcc -c t1.c -L. -lt1

Is there another way I am not considering?

0

Is there another way I am not considering?

> gcc -c foo.c
> gcc -c bar.c
> gcc -o baz foo.o bar.o
>

Of course an object file can be linked. In fact, only the object files are linked.

A library is a convenient way to package a collection of objects. They are not linked directly. The linker extracts required objects from the library and links them.

Edited by nezachem: n/a

0

Is there another way I am not considering?

The gcc call does an awful lot for you when you ask it to take care of the linking. Here's the longhand linker call:

gcc -c func.c
gcc -c main.c

and now calling the linker...
/usr/bin/ld --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. -L/usr/lib/x86_64-linux-gnu -v main.o func.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o


The -l option for the linker tells the linker that the file specified is an ar format.

Edited by Moschops: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.