How to add already created .a file while creating .a file - static

can anyone tell how can I include already existing .a file while creating .a with this?
ar rcs libcrypt.a
I need to add libssl.a and libcrypto.a files together into a libcrypt.a file. Can you help me to solve this?

man ar tells you how to extract
all the members from an archive, and it tells you how to insert members
into an archive.
So extract all the members from libssl.a and libcrypto.a
and insert them all into libcrypt.a, taking care to do the extracting
in an empty directory.
$ mkdir scrap
$ cd scrap
$ ar -x ../libssl.a
$ ar -x ../libcrypto.a
$ ar rcs ../libcrypt.a *
$ cd ..
$ rm -r scrap/

Related

Script to create a static library from all .c file in my working directory

I am trying to write a script that create a static library call libwork.a in the working directory from all the .c files in the directory:
#!/bin/bash
gcc -c *.c | ar cr libwork.a *.o
But as I run my script, it only creates the object files. The libwork.a does not get created. I tried both sourcing and executing my script but it still only creates object files only.
Why is it not creating the archive?
You are piping the messages printed by gcc (most surely none) to ar (which does not read anything). This is nonesense, ar should run after gcc.
The file listing generated by "*.o" is passed before "gcc" finished.
The solution is to remove that pipe and simply run the commands one after the other.
#!/bin/sh -e
gcc -c *.c
ar cr libwork.a *.o
Note the "-e". This tells the shell to abort if one of the commands fails, so if gcc fails ar will not execute.
Also, have a look at this one:
#!/bin/bash
gcc -Wall -pedantic -Werror -Wextra -c *.c
ar -rc libwork.a *.o
ranlib libwork.a

What do the following commands do in makefile?

$(TARGET): build $(OBJECTS)
ar rcs $# $(OBJECTS)
ranlib $#
What are "ar rcs" and "ranlib"?
ar is used to create libraries (that are archives containing compiled code)
man ar:
ar -- create and maintain library archives
...
-r Replace or add the specified files to the archive
-c Whenever an archive is created, an informational message to that effect is written to standard error. If the -c option is speci-
fied, ar creates the archive silently.
-s Write an object-file index into the archive, or update an existing one, even if no other change is made to the archive. You may
use this modifier flag either with any operation, or alone. Running `ar s' on an archive is equivalent to running `ranlib' on
it.
The first argument is the name of the resulting libraries, the others are the object files to insert in it.
ranlib generates an index to the contents of an archive and stores it in the archive.

List of relocatables integrated into static library

Below archive file(shuffler.a) is created with below command:
$ go install github.com/myhub/cs61a
$
$
$ file pkg/linux_amd64/github.com/myhub/cs61a/shuffler.a
pkg/linux_amd64/github.com/myhub/cs61a/shuffler.a: current ar archive
$
$
But there is more than one than file(relocatable) integrated in archive file:
$ ar -t pkg/linux_amd64/github.com/myhub/cs61a/shuffler.a
__.PKGDEF
_go_.o
$
$
_go_.o is a relocatable binary form of src/github.com/myhub/cs61a/shuffler/shuffle.go source code
What does __.PKGDEF signify? ar –rcs libourown.c one.o two.o in C world does not add this file
Since go code is organized by package, while C code is not, and since go libraries/binaries are compiled package by package, I would take the wild guess that PKGDEF has information about the go language package from which the code was compiled.

How to create one static library from several others static libraries in C on Linux? [duplicate]

This question already has answers here:
How to merge two "ar" static libraries into one?
(9 answers)
Closed 3 years ago.
For example,
we have three following libraries:
1.1.......................lib_A.a
1.2.......................lib_B.a
1.3.......................lib_C.a
Now I want to create one library which consists of all the above static libraries.
I have used following command:
ar -crs lib_Z.a lib_A.a lib_B.a lib_C.a
When I create final executable with lib_Z.a, the compiler gives me the following error:
error adding symbols: archive has no index; run ranlib to add one
How to solve this issue?
Your lib_Z.a contains no object files; it only contains other .a files, and the loader doesn't know how to handle that. Running ranlib wouldn't help; it wouldn't find any object files in the lib_Z.a archive. You have to extract the object files from the separate libraries and then build them all into the result:
mkdir Work
cd Work
ar -x ../lib_A.a
ar -x ../lib_B.a
ar -x ../lib_C.a
ar -crs ../lib_Z.a *.o
cd ..
rm -fr Work
The only trouble you can run into is if two of the libraries contain the same object file name (e.g. lib_A.a contains config.o and so does lib_B.a but they define different configurations). You would have to rename one (or both) of the object files:
…
ar -x ../lib_A.a
mv config.o A_config.o
ar -x ../lib_B.a
mv config.o B_config.o
…

C : How to link all o file into one file

I try to integrate a C library into my project. The custom makefile of this project try to compile all source files, each file into one object file (*.o).
I need to link all of those *.o files into one file, for example so file, for easily to use it. Do I need this ? And if yes, how can I link all of *.o files into one library file. Which lines I will add to Makefile ?
(Notes that, this C library include many sub-directory, and a big makefile will go into all sub directories, and use Makefile in each directory to run it)
Thanks :)
see this is best document.
Lets see i have 2 .o files. ctest1.o ctest2.o
so i will make static library as follows way.
ar -cvq libctest.a ctest1.o ctest2.o
For dynamic .so file
gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
This creates the library libctest.so.1.0 and symbolic links to it.

Resources