How to implement runtime load dynamic library in llvmir? - llvm

I know that there is two relative library : the dlfcn.h in C and the dynamicLibrary.h in llvm. but the former is only UNIX, the latter is a c++ class. How should i do use a class in llvmir ? Or Is there other c library for cross platflat dynamic link?

Related

is it possible to build an application with external libraries of different ABI's

I have currently the problem that i need to build a application where it seems that the libraries are build with different c++ versions.
When i build i got the undefined reference std::__cxx11::basic_string errors at one library and when i build with the -D_GLIBCXX_USE_CXX11_ABI=0 i get undefined reference errors at std::string of the other library.
They are both external libraries which i unfortunatly can't rebuild myself. Is there a workaround for this?
The only possible work-around I can think of is not very nice. Say you have libraries A and B with different ABI versions, vA and vB. Now, create a wrapper for library A which exposes an interface which is ABI-independent (plain C, i.e. all exposed functions should be declared with extern "C"). Then build this with ABI version vA (make sure to statically link needed vA libraries). Now you can build your application using ABI version vB linking with the wrapped library A (which should now be ABI-independent) and library B.
Of course, depending on the interface of library A, it may take some creativity to create a plain C interface, but it should be possible.
Disclaimer: I have not tried this myself, I recommend starting with a minimal test.
You can't mix different values of GLIBCXX_USE_CXX11_ABI in a single file but you should be able to link files with different settings into a single library.
So if you are using the c++11 library in class A and the c++03 library in class B and class A and class B are in separate files and don't have std::string or std::list in their headers then it should be possible to link class A and class B into a single library.
Use of the pimpl pattern may be useful/required to achieve this.
The simplest approach is probably to produce a static library which wraps your c++03 library and is compiled with -D_GLIBCXX_USE_CXX11_ABI=0 then you can compile the rest of your code as normal as long as you only interface with your c++03 library through the static library.

Create DLL (run-time link library) for Standard C/C++ Library

Is it possible to create DLL (run-time link library) for Standard C/C++ Library like GNU Scientific Library, LibModbus, GNU Multiple Precision Arithmetic Library, LibQrEncode using Mingw that can be linked at run-time? If yes, how?
Using the make method creates files that can only be statically linked.
Why this is being done :
Separating user files/code from standard library
Multiple user program/process working independent/minimal synchronization from each other access similar set of functions but are developed by 3 separate developers
Modifying and updating files will be simpler

How to embed a C++ library in a C library?

I have a question related to embedding one library in another.
I have a code that is pure C and my users rely on that, they don't want to depend on C++ libraries. However, the need arose to embed a 3rd party library (ICU) into mine. None of the ICU functions would be exported, they would only be used internally in my library. Unfortunately ICU is a C++ library, though it does have a C wrapper. ICU does not use exceptions, but it does use RTTI (abstract base classes).
The question is how could I create my static library so that
ICU is embedded in my library (all references to ICU functions are resolved within my library)
all references to libstdc++ are also resolved and the necessary code is embedded into my library
if a user does not even have libstdc++ installed on their system things work just fine
if a user does happen to use my library within a C++ project then there are no conflicts with whatever libstdc++ (presumably the system libstdc++) he uses.
Is this possible at all? The targeted platforms are pretty much everything: windows (there my library is dynamic), and all sort of unix versions (linux, solaris, aix, hpux - here my library needs to be static).
gcc-4.5 and later does have --static-libstdc++, but as far as I understand it is only for creating shared libs or executables, and not static libs.
Thanks for any help!
The solution to this problem is pretty simple, but may not fall inside the parameters you have set.
The simple rules are:
You can dynamically link a C++ library to a C caller, but you have to wrap the library inside an extern C layer. You design the extern C API and implement it using C++ internals, which are forever hidden from view. [You can also use COM or .NET to do this on Windows.]
You cannot statically link a C++ library to a C caller. The libraries are different and the calling sequences/linker symbols are different. [You often can't even statically link between different versions of the same compiler, and almost never between different compilers.]
In other words, the solution is simple: use dynamic linking. If that's not the right answer, then I don't think there is one.
Just to make things interesting, you can even implement your own plug-in architecture. That's just another name for dynamic linking, but you get to choose the API.
Just to be clear, the only viable portable option I can see is that you link ICU inside its own dynamic library (DLL or SO). Its symbols, C++ libs, RTTI and exceptions all stay inside that. Your static lib links to the ICU dynamic lib by extern C. That's exactly how much of Windows is built: C++ inside DLL, extern C.
You can debug across the boundary, but you cannot export type information. If you need to do that, you will have to use a different API, such as .NET or COM.
I don't know if this will work, but let me at least suggest that you try it!
The excellent LLVM project (origin of clang compiler) has many front-ends and back-ends for different languages, such as C++ and C. And according to this S.O. question it should be possible for LLVM to compile C++ into C which in turn can be compiled as normal.
I imagine this route is a bumpy one, but if it works, it might solve your problem without the need to link dynamically. It all depends on if ICU will compile with LLVM C++.
If you do decide to give it a go, please let us know how you fare!

msvcprt and crt

I have editbin msvcprt and look for scanf for instance but i can't find it.
Does it mean crt is not encapsulate in msvcprt ?
Does it mean cin function doesn't use scanf ?
There are two libraries: the C Run-Time library (called the "CRT") and the Standard C++ Library. The scanf function is a part of the CRT, not the Standard C++ Library.
Both the CRT and the Standard C++ Library are linked in automatically when you compile a C++ program. You can read the details about which libraries get linked in when on the C Run-Time Libraries documentation on MSDN.
In this specific case, the corresponding CRT library for the msvcprt.lib version of the Standard C++ Library (/MD / Multithreaded DLL) is msvcrt.lib.
You are using the wrong tool and the wrong file. Use dumpbin.exe and msvcrt.lib

Difference between C/C++ Runtime Library and C/C++ Standard Library

Can you guys tell me the difference between them?
By the way, is there something called C++ library or C library?
The C++ Standard Library and C Standard Library are the libraries that the C++ and C Standard define that is provided to C++ and C programs to use. That's a common meaning of those words, i haven't ever seen another definition of it, and C++ itself defines it as this:
The C++ Standard Library provides an extensible framework, and contains components for: language support, diagnostics, general utilities, strings, locales, containers, iterators, algorithms, numerics, and input/output. The language support components are required by certain parts of the C++ language, such as memory allocation (5.3.4, 5.3.5) and exception processing (clause 15).
C++ Runtime Library and C Runtime Library aren't so equally used. Some say a runtime library is the part that a program uses at runtime (like, the code that implements std::type_info or the code supporting signal handlers) as opposed to stuff that they only use at compile time (like macro definitions). Other people say that a runtime library is one that is linked to a program at load time dynamically, as opposed to statically at compile time, though this use is very seldom. shared library or dynamically linked library are better terms for that.
C++ Library and C Library are very broad terms. They just mean that a library is written in C++ and/or C.
The above is not only limited to C++ and/or C. There are python libraries and there is a python Standard Library too.
According to https://en.wikibooks.org/wiki/C_Programming/Standard_libraries#Common_support_libraries, there is a very important difference between Standard Library and Runtime Library. While the Standard Library defines functions that are (always) available to the programmer (but not part of the (initial) specification of the programming language, at least in C), the Runtime Library contains functions that are necessary to actually run a program on a given platform (and are platform-specific / vendor-specific).
For example, printf() is part of the C Standard Library, while program startup (which is in many cases invisible to the programmer) is implemented in the Runtime Library. So for example, you could write a C-program which does not use the Standard Library but you always need the Runtime Library because otherwise, your program could not be executed. But to be honest, this would be of little use because a C-program without the Standard Library could not do input/output so it could not tell you something about its impressive results.
What leads to confusion concerning the difference between those two is:
In every case, the Runtime Library is needed/used and in (nearly) all cases, the Standard Library is used. Furthermore, the Standard Library could be dependent on the Runtime Library and is most probably developed by the same vendor. Therefore, the distinction is not clear and in most cases not necessary.
Microsoft has put the C Standard Library and C Runtime Library together and just calls it C Run-Time Library.
C++ standard library is a term to define the standard library that a minimum conforming compiler/toolset should have. C++ runtime library is the library shipped with the toolset to provide standard library functionality, and probably some internal stuff the compiler might need. In fact, those terms are often interchangeable.
Introduction
C/C++ Standard Library is any implementation of all the required set of functionalities needed to accomplish what ISO C/C++ standard requires. (Wikipedia definition of a C++ Standard Library)
A Runtime Library is any implementation of a set of functionalities that are usually offered in form of SDK that are required to be installed or statically linked to let a program using that SDK to be run having all that it could need to use that SDK. For these reasons Runtime Library is usually strictly related to the SDK used and the compiler version used. (Wikipedia definition of a generic Runtime Library)
C/C++ Runtime Library
A C/C++ Runtime Library has thus to contain all the functionalities required to execute what is required by the Standard Library (header only functionalities of the specific Standard Library implementation can be excluded because they are resolved within the program itself) plus a set of functionalities offered by the SDK of the specific implementation (again functionalities offered as header only can be excluded).
The Microsoft Case
Before MSVC140: recent Microsoft VC++ Runtime used to have an installable version of the C/C++ Runtime (VCRedist), that version was the same for all the OSes.
Starting from MSVC140: starting from the next MSVC140 compiler, the Runtime library has been split into two parts:
UCRT (Universal C Runtime): shipped with the OS and related to it, distributed through updates or OS images
VCRedist: the part that is expected to change with the compiler being used and that is common among the different OSes versions (managed like before MSVC140).
Here is a link to MS C Runtime reference documentation.
Here is a link to MS C Runtime download page and install instructions.
C++ Standard Library consists of two main parts, namely the Standard Template Library (STL) and the runtime library. STL is implemented in header files only, while an implementation of the runtime library contains both header files and binaries (i.e., .lib and .dll files on Windows platforms).
Another aspect, maybe not exactly the case of C/C++, but according to the wikipedia: Runtime library
In computer programming, a runtime library (RTL) is a set of low-level routines used by a compiler to invoke some of the behaviors of a runtime environment, by inserting calls to the runtime library into compiled executable binary.
To be concise:
Runtime library is meant to be used by the compiler and standard library is meant to be used by the programmer.
C++ runtime library contains functions and objects supplied in C++, such as cout, fstream and so on.
C runtime library contains C functions such as printf, scanf, fopen, and so on.
The standard library is a particular set of defined names and headers as
defined in the C++ standard document, a runtime library is a blob of
binary stuff that is used as a part of the compiled program but is not
included in your program executables because it is so commonly needed.
Instead, those pieces of functionality are included on the host machine
(although you might need to ask your customers to install an updated
runtime if they have an older service pack) so they get included into
your program only at "runtime".
Ref. links:
http://msdn2.microsoft.com/en-us/library/cscc687y(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/59ey50w6(VS.80).aspx