Multiple libgcc_s libraries when linking on Solaris with studio 12.5 - linker

I'm trying to move to Developer Studio 12.5 and enable the C++11 options. My software links openssl using the Oracle provided library in /usr/sfw. This appears to depend on an older libgcc_s.so than the one now used for the C++ 11 STL. So I get the following:
CC -o binary -B direct -z origin -std=c++11 object_files.o -Lblah/lib -R\$ORIGIN/../lib -lotherlib -L/usr/sfw/lib -R/usr/sfw/lib -lcrypto -lsocket
unused $ADDVERS specification from file '/opt/developerstudio12.5/lib/compilers/CC-gcc/gcc_version.map' for object 'libgcc_s.so'
version(s):
GCC_4.2.0
Is there some way for the two libgcc_s.so instances to coexist in one binary with references from libcrypto going to one and from the STL to the other? Looking at the Oracle linker and libraries document shows some promising options like -B group but that perhaps only works for dlopen.
I realise that I could just recompile openssl (or perhaps libressl) but it would be nicer to use the one from the OS and my colleague reckons that Sun's build makes use of hardware crypto features on the SPARC.

Related

How can I compile & and run an ESQL/C program on Linux Platform?

I have checked the IBM official site for ESQL/C programming guide. I didn't find exact commands to compile and run. Do I need to install any packages to run? Can anyone tell me the commands to run these in Ubuntu?
ESQL/C (Embedded SQL in C) uses C code for the bulk of the code, but uses special markers (either $ in Informix ESQL/C or EXEC SQL in both standard and Informix ESQL/C) to indicate where SQL statements need preprocessing to be converted into an appropriate series of C library function calls and C variable definitions, etc. The esql script is the compiler that automatically converts Informix ESQL/C source into first C and then object code and an executable (under options that are mainly the same as the C compiler's options, most of which are passed to the C compiler unchanged).
You need to have the Informix ClientSDK (CSDK) installed to be able to compile ESQL/C programs. That is installed by default when the server is installed, so the chances are you're OK if you're on a machine with a working Informix server on it (if it also has a working C compiler and development environment). It is also available as a separate standalone product which you could install if you don't have, and don't want, an Informix server on your machine. There are advantages for testing if the server is local (quicker access, and less danger of damaging production systems, amongst others).
Assuming you have got CSDK installed, you need to set the environment variable INFORMIXDIR to point where the software is installed (unless you chose to install it in /usr/informix or create a symlink /usr/informix that points to to where CSDK is installed). You'll also want to add $INFORMIXDIR/bin to your PATH. You're now ready to compile:
Compile .ec (ESQL/C source) files to object with the esql command:
esql -c esqlc_source.ec
Add other C compiler options as needed. Note that -g is intercepted by the esql script and you have to work hard to get it passed to the C compiler.
Consider compiling .c (C source) files that use an ESQL/C header with the esql script too. This will pass the correct directory for the headers to the C compiler automatically. More likely, you'll use:
${CC} -c c_source.c -I${INFORMIXDIR}/incl/esql
For linking, use the esql script to do it. It will provide the correct libraries (and object files) to the compiler, which it will invoke as the linker:
esql -o program c_source.o esqlc_source.o
You can add other libraries and library directories as usual.
You have the program compiled; now you need to run it. The chances are that you won't find the libraries automatically. You will need to think about adding some directories to either LD_LIBRARY_PATH or modify /etc/ld.so.conf to pick up the extra directories, or create symlinks to the Informix libraries from a place that will be picked up automatically (e.g. /usr/lib or /usr/lib64, or perhaps /usr/local/lib) to where the libraries are installed.
You need to add at minimum:
$INFORMIXDIR/lib
$INFORMIXDIR/lib/esql
Under some circumstances, you may need to add other library directories found under $INFORMIXDIR/lib as well, but usually not.
You should then be able to run the program. Using ldd program will let you know when you've got the settings right.

Which linker options to include gcc

I compile a project and run it on my system, it does perfectly fine.
I uploaded this to git and asked my administrator to run it on server and it says undefined reference to 'clock_gettime'. This error can be resolved by linking a library with -lrt option. Similar problem happened to me earlier but that was related to linking math.h library with the help of -lm option.
Is there a way to find which all libraries to link so that ou do not run into such problems and not feel embarrassed in front of your administrator. How to decide what all libraries to link. Can there be a thought process for this?
Some man pages will mention both the headers and libraries that should be used. (This is not true of all pages unfortunately.)
For example, look at man 3 signal. On my system that page has a LIBRARY section at the top with Standard C Library (libc, -lc), followed by a SYNOPSIS section with #include <signal.h>.
Some other strategies include:
Blunt use of strings piped to egrep in a shell loop, to search for references to names among installed libraries. This can return a lot of false positives for common names, but it works very well for elaborate function names that don't match single English words.
Iterative compilation and linking. If you are trying to resolve a single function, write a tiny program that calls the function and use a shell or makefile loop to test each single library on your system (starting with a small list of most likely candidates from /usr/lib or wherever else). At some point the build will succeed and show you the right library. This method takes a little work to set up, but the infrastructure you create is also very easy to reuse in the future.
Inspect binaries iteratively using your linker's tools (e.g. otool on Mac OS X, or objdump or readelf on Solaris or Linux). For instance, try to disassemble a library's text section starting from a particular name (otool -v -t -p _symbol on Mac OS X).
For *nix based machines, ldd on the executable will print shared library dependencies.

Difference b/w llvm-ld and llvm-link

What is the difference between llvm-ld and llvm-link? I suppose llvm-ld performs link time optimization while llvm-link doesn't. Am I right?
llvm-ld is a drop-in replacement for the system linker that supports both LLVM bitcode and native code. It produces bitcode executables by default (ie the resulting executable invokes the bitcode interpreter), but can also be used to produced native executables.
I don't use llvm-ld directly as it's more convenient to use the llvmc and clang frontends, which invoke the appropriate programs of the LLVM toolchain as necessary (note: llvmc was marked experimental and appears to have been removed in the 3.0 release).
llvm-link is a more low-level tool which joins several bitcode files into a single one. The documentation doesn't mention if it does optimizations, but it doesn't appear to do so. The next optimization passes will be triggered on native code generation.

Cross Compiling On Windows?

I have the GNU GCC compiler for Windows. I read that it is able to function as a cross compiler.
How do I do this? What command option(s) will produce an shared library that can be used by MacOS/Linux platforms?
You need to build your own cross-compiler, i.e. you need to get the GCC sources and compile them with a desired target-architecture. Then you have to cross-compile all the libraries.
The process is fairly involved and lengthy. The usual GNU makefiles are pretty good at supporting this (through HOST, BUILD and ARCH variables), but if possible you should leave this to a higher-level abstraction. crosstool is one such tool that comes to mind, but I don't know if it's available for Windows.
It's possible that you'll be able to find pre-build Windows binaries of GCC on the internet that target a particular architecture.

Static library depending on shared library

I have created a communication library which is linked statically into several different applications. The library provides support for communication via different kinds of hardware. Some of the hardware is supported from the vendor via a shared library. On systems without those kinds of hardware, the shared library is not available.
Previously we handled this by compiling dual versions of the communication library and the applications. This is however not very practical, so I thought about using a more dynamic communication library which tries to load the vendor library with dlopen()/dlsym() if it is available. This seems to work well. But a problem is that everyone who use my library will need to pass the -ldl option when linking their application with my library. Even if this is a minor nuisance I wonder on how this is normally solved.
Is it somehow possible to create a static library that will automatically (at compile-time or run-time) bring in the needed shared library?
Is it considered good practice to let static libraries have dependencies on shared libraries?
EDIT: I know that libtool could probably solve this, but that would still change the build process for all applications even more, which I would prefer to avoid.
EDIT 2: Target platforms are primarily Linux and Solaris. Gcc as compiler.
I don't know about Solaris, so assume everything in my answer here applies to Linux only (although pkg-config should be usable on Solaris too).
First off, there's no supported way for a static library to pull in a link-time dependency. Sorry. Most libraries use something like pkg-config for this - that is, when you build, you add to the compiler command line:
gcc `pkg-config --cflags your-library` [...]
and when you link:
gcc `pkg-config --libs your-library` [...]
Note that the --libs parameter, in this case, would produce something like -ldl -lyourlib as output. The --cflags parameter may produce no output, or it may add some include paths.
If you absolutely need it to work with just a -lyourlib, though, and you don't mind being tied to a unsupported and unstable interface in glibc... Well, libdl is just a thin wrapper over some routines in the dynamic linker, via an undocumented vtable. If you look at the source under the dlfcn/ directory of the version of glibc in use, you should be able to replicate what it does.
HOWEVER, the interface between libdl and ld-linux is PRIVATE and UNSTABLE - it may change at any glibc release, including minor and bugfix releases. ONLY do this if you control the version of glibc deployed, and are prepared to rebuild your application when necessary. Also note that if your library is not LGPL itself, then using a private API like this may have licensing issues; not sure how things stand with this for the LGPL.

Resources