Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

Sample Make file for Solaris gcc shared library

I am looking for simple working example of a sample makefile which will compile a simple c file into a shared library using the gcc compiler on solaris. using ELF format

// sample program to create a libary

#include <stdio.h>

void ExecuteRequest(void)
    printf("Invoked application exported function\n");

  • 4
  • 3
1 Solution
<tab> gcc -c ExeccuteRequest.c
<tab> ar -cr libYourLibName.a ExeccuteRequest.o

this is a very simple makefile to create LibYourLibName.a
the first "lib" in library name is a Must.
you can use the following if you have more than one file to populate the library.

FILES = one.c \
         two.c \
         three.c \
OBJS = (FILES.c=.o)

<tab> gcc -c $< -o $@
then you can
ar -cr libYourLibName.a $(OBJS)
which will take all you object files and stick em into the library.  now OBJS is build depending on FILES. and the .c.o: will know how to produce each obj.  
MJUNGAuthor Commented:
Hello Garboua

I am look ing for one that produces a shared library
using the -fPIC ELF format ? Do I still need to use the ar ?

you do this
"gcc -shared -fPIC ExeccuteRequest.c -o libMySTUFF.so"
sorry should have read better.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

MJUNGAuthor Commented:
Another Question

You stated that the name of the library must begin with the literal lib. Why is this ?
when you link you will do something like this
"gcc mainDriver.o -lMySTUFF -o OUTPUT"
and the linker knows to look for something that begins with lib-MySTUFF-.extension ".a .so"
so there is no Way around that, it is just the way of the Penguins :-)
Take a look under /lib and /usr/lib and you will see all libraries begin with "lib" and then the extension.
MJUNGAuthor Commented:
Ok thanks for your help
I will do some more reading
but your help is greatly apprciated!
"man ld" will give you libraries option and how to include them, the difference between "-L" and "-l" etc etc
the Most single Important Document that you can have is
search for ELF and you will find TONS of info, more than you might need. it also include info about other stuff. a very good document.  short sweet to the points.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now