[CMake] How to get suffix of shared library file?

Alan W. Irwin irwin at beluga.phys.uvic.ca
Wed Jun 13 23:53:21 EDT 2007


On 2007-06-14 08:58+0800 Clark J. Wang wrote:

> Shared libs have different suffixes on different OSes. For example on Linux
> the suffix is `.so' and on Mac OS X it's `.dylib'. I want to use `dlopen()'
> in my code so I want to get the suffix and then define it in a header file.
> Can I do that in CMake? And how?

You could write a CMake test to create a shared object, gets its full name,
and parse for the suffix.  However, that is a non-trivial amount of cmake
script development (at least it would be for me) so let me discuss an
alternative that might work a little easier for you.

Before getting to specifics, note on Unix there is a distinction between a
shared library (a shared object opened at the start of run time by the
run-time loader) and what I will call a plug-in (a shared object that is
"dlopened" by an executable or library _some time_ during the course of run
time). For example, there are naming conventions that must be used by shared
libraries, (e.g., the library name must start with "lib"), but AFAIK those 
naming conventions don't apply to plug-ins.  So what I have done for the
PLplot plug-ins is always build them with a suffix of ".so" regardless of
platform.  The resulting plug-ins seems to work fine both on Linux and Mac
OS X.  Note, we use the libltdl library (the only remnant of autotools still
left in our CMake build system for PLplot) to provide a uniform wrapper for
dlopen and its various equivalents on non-Linux platforms.  That library
seems happy with the *.so names for plug-ins for the limited testing
(I think just Linux and Mac OS X) that we have done so far.

Anyway, you may want to give the *.so name for plug-ins idea a try (with or
without libltdl) if you don't want to fiddle around creating a cmake test to
find out what the shared object suffix is on each of your platforms.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list