On Solaris, every program uses the C and Solaris runtime support library libc.so.1, so there is no macro to show it is being used -- it is always used. You can run the command
man libc
for more information.
Sun compilers predefine macros to show that you are using a Sun compiler, and showing the version of Solaris on which the compiler is running. The Users Guide for the compiler you are using lists these macros.
If I haven't answered your question, it would help to explain what problem you are trying to solve.
Let me clarify one thing first. __GLIBC__ is defined in features.h which apparently exists in the GNU C library but not Sun's C library. Therefore if I have the following:
[code]
#include <features.h>
int main()
{
#ifdef __GLIBC__
printf("glibc\n");
#else
printf("sun libc\n");
#endif
}
[/code]
and compile and run it on my Linux system, which uses glibc, I will get the output:
glibc
But if I compile it on Solaris, I will get a compilation error:
features.h: No such file or directory
Now, when you say "On Solaris, every program uses the C and Solaris runtime support library libc.so.1, ... -- it is always used", do you mean Sun's libc is a default but one can change the default to glibc when configuring the system, or do you mean you must use Sun's libc and cannot somehow change to glibc?
Back to my original question: I am writing a middleware, portable to Linux and Solaris, and want to print out what C library (and what version) that the binaries of the middleware is compiled with. So, when I execute this middleware, a message of either
Middleware XYZ compiled with gcc 4.1 glibc 2.1
or
Middleware XYZ compiled with gcc 4.1 sunlibc 1.2.3
is displayed.
Hi
> and compile and run it on my Linux system, which uses
> glibc, I will get the output:
> glibc
> But if I compile it on Solaris, I will get a
> compilation error:
> features.h: No such file or directory
What do you expect from code that uses non-standard headers? I mean, would you expect code that uses proprietary Microsoft or Apple headers to compile on Solaris? I can see no mention of features.h in SUSv3.
> Now, when you say "On Solaris, every program uses the
> C and Solaris runtime support library libc.so.1, ...
> -- it is always used", do you mean Sun's libc is a
> default but one can change the default to glibc when
> configuring the system, or do you mean you must use
> Sun's libc and cannot somehow change to glibc?
Even if you did manage to build glibc on Solaris, you would also have to rebuild ALL of the system dynamic libraries that your app would need to link with so that they use your glibc. That would be an enormous task, especially if your app does anything but basic console IO. As far as I'm aware, even the "linux distro" style versions of OpenSolaris - benelix and nexenta - use Sun libc and not GNU glibc.
> Back to my original question: I am writing a
> middleware, portable to Linux and Solaris, and want
> to print out what C library (and what version) that
> the binaries of the middleware is compiled with. So,
> when I execute this middleware, a message of either
> Middleware XYZ compiled with gcc 4.1 glibc 2.1
> or
> Middleware XYZ compiled with gcc 4.1 sunlibc 1.2.3
> is displayed.
For the compiler version, you can use the __SUNPRO_CC (for C++) or __SUNPRO_C (for C) macros.
However (and perhaps I'm reading a bit too much between the lines of your post) you probably do not need any of this. Solaris isn't Linux, and does not suffer from the rampant and chronic incompatibility between versions of compilers and C runtime libraries. What this means is that if you compile your app library on a baseline system (where the baseline is defined by the patchlevel required by the compiler suite), then your app will run on any system at or above the same baseline (i.e., more recent patches or a more recent OS). Also, if you are producing a library, it will be compatible with more recent compilers.
The only possible exception to this is if you use the C++ STLport library, which isn't part of the base OS, and you have to ship with your app anyway, and you can't mix with libCstd.
Paul
Some parts of libc (both Sun libc and glibc) are simple utility
functions where any normal implementation could be used *in theory*.
Other parts of the library (both glibc and Sun libc) are very interwoven with
the operating system and the runtime linker. Because of this it's
normal for all Solaris programs (even portable ones, even Linux-designed
software) to use the implementation of libc that comes built-in to Solaris,
when they are running on Solaris. As far as I know, there is no easy #define
to detect that you are using Sun's libc. You could use the combination
of: 1) running on Solaris AND 2) not using GLIBC. Even though I would
not expect to find any/many programs using glibc on Solaris, it's better
to check for both flags.
--chris
> What do you expect from code that uses non-standard
> headers? I mean, would you expect code that uses
> proprietary Microsoft or Apple headers to compile on
> Solaris? I can see no mention of features.h in
> SUSv3.
I do not expect the code would compile. I wrote it down here just for the record, showing people how __GLIBC__ works if they want to know.
> Solaris isn't Linux, and does not
> suffer from the rampant and chronic incompatibility
> between versions of compilers and C runtime libraries.
OK. You educated me about the stability and robustness of Solaris. But what if someone really wants to know the version of Sun's C library?
By the way, Solaris's man pages indicate certain functions require linking of certain libraries. For example, pthread_sigmask() needs the linking of -mt. The SYNPOSIS states:
cc -mt [ flag... ] file... -lpthread [ library... ]
Does this (-mt) requirement go with the use of the Sun C compiler or with the Sun C library? I mean, if I use gcc rather than Sun's cc, while using the Sun libc, should I specify -mt?
The -mt option on Sun compilers tells the compiler to generate code suitable for MT, and it also causes the necessary libraries to be linked. That is "cc" is a driver program that invokes a suite of programs to perform the compilation and linking steps. The -mt option causes the driver to do the right things for multi-threaded programs.
gcc probably has an equivalent option. Consult the gcc documentation.
It's better to start a new thread when you ask a new question.
The -mt flag turns on -D__REENTRANT and also links in the
threads library. On Solaris 10, threads are in libc, so you don't
need to link with -lthread unless you want to. Turning on __REENTRANT
usually won't hurt anything. It affects the way some library routines
are declared in header files to add some MT-safe versions, and
make some functions slightly slower, but MT safe.
--chris