Quick Navigation Bar debugging techniques :: creating libraries :: tips and tricks [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
ar rcs libmylib.a objfile1.o objfile2.o objfile3.oThis will create a static library called libname.a. Rename the "mylib" portion of the library to whatever you want.
-p
option
with cp to preserve permissions.gcc -o foo foo.o -L. -lmylibThe
-L.
piece tells gcc to look in the current directory
in addition to the other library directories for finding libmylib.a.gcc -fPIC -c objfile1.c gcc -fPIC -c objfile2.c gcc -fPIC -c objfile3.c gcc -shared -o libmylib.so objfile1.o objfile2.o objfile3.oThe -fPIC option is to tell the compiler to create Position Independent Code (create libraries using relative addresses rather than absolute addresses because these libraries can be loaded multiple times). The -shared option is to specify that an architecture-dependent shared library is being created. However, not all platforms support this flag.
gcc -o foo foo.o -L. -lmylibNotice it is exactly the same as creating a static library. Although, it is compiled in the same way, none of the actual library code is inserted into the executable, hence the dynamic/shared library.
LD_LIBRARY_PATH
enviornment variable. To
display this variable, at a shell:
echo $LD_LIBRARY_PATHWill display this variable if it is already defined. If it isn't, you can create a wrapper script for your program to set this variable at run-time. Depending on your shell, simply use
setenv
(tcsh, csh) or export
(bash, sh, etc)
commands. If you already have LD_LIBRARY_PATH defined, make
sure you append to the variable. For example:
setenv LD_LIBRARY_PATH /path/to/library:${LD_LIBRARY_PATH}would be the command you would use if you had tcsh/csh and already had an existing LD_LIBRARY_PATH. An example with bash shells:
export LD_LIBRARY_PATH=/path/to/library:${LD_LIBRARY_PATH}
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.