How do you include external libraries into Haxe? - c++

I'm trying to write a program for which I would like to use some external C++ libraries in Haxe. I can't really figure it out since the official documentation is old (http://old.haxe.org/doc/cpp/ffi) and I'm not familiar with C++ either.
So how do you do that in Haxe? I suppose I'll need to install hxcpp via haxelib, but that's about as much as I know.

For a start, check out C++ Magic by Hugh Sanderson. Much of the mentioned Compiler Metadata in that talk can be found at the Built-in Compiler Metadata page in the Haxe Manual.
He then talks briefly about metadata magic. He provides metadata for classes and functions. For classes you have the following:
#:headerClassCode(...) which injects member variables and inline functions.
#:headerCode(...) which includes external headers.
#:headerNamespaceCode(...) which allows you to add to the global namespace.
#:cppFileCode(...) which allows you to include external headers only in C++ files.
#:cppNamespaceCode(...) which implements static variables.
#:buildXml(...) which allows you to add to the build.xml file.
For function metadata you get the following:
#:functionCode(...)
#:functionTailCode(...)
Hugh states these are largely redundant as you should use untyped
__cpp_(...) instead.

Related

C++ How to find class by it's name?

How can I find a class to call it's methods by knowing class name and method names?
Details:
I'm trying to write library to replace some functions from another program which is also written using C++ by LD_PRELOAD functionality.
I need to have an ability to call functions from program in which this library gonna be integrated.
C++ loses all class naming information during the compile process!
You can`t do something like:
Class.forName("MyClass");
Like you know it from Java.
http://en.cppreference.com/w/cpp/language/class
This technique is called reflection and its not suported by C++
You can use a Framework (e.g. Boost) to help you out there, but the methods you want to call have to be declared and defined with this Framework.
C++ does not support reflection so you cannot search for functions/classes by name after compile time.
It sounds like if possible the design needs to be reworked. Ideally you should update the library to include what it needs from the programs. Either pull out the common logic from the programs into a third library - or put the functions that needs to be called by the programs into the current library and just pass the relevant data that will be manipulated into the library.
If this is not possible you can pass a function pointer from your programs into your library - this allows the library to have access to the functions it needs without any real knowledge of where it is coming from.
ie
void library_function1(std::function<void(int)> func)
{
func(1);
}

Libraries vs classes in c++

I came across a terminology in C++ which is a library. The book I am reading states that iostream is a library , and it is a system library. Then After that it says in the book, "later, you will create your own library with the extension .h". Is a library the same as class because clearly, when I included my .h library, I actually had created a class.
If a library is the same as a class, what can we say about in C language , is it a class ?
Thank you.
Please note that this is just a simplistic explanation so you can wrap your head around it, not a pedantic or an exact and complete definition of a library.
A library is a collection of functions, classes and other stuff packaged together.
For instance the C++ standard library is (conceptually) composed of many libraries, e.g.:
string library
algorithm library
input/output library
etc.
The IO library contains some classes like:
std::iostream (actually a typedef to std::basic_iostream)
std::ios
std::istream
std::ostream
etc.
In order to use a library you basically need two things: the library headers in order to have access to the declarations and a library which needs to be linked to your project in order to have access to the symbols exported by such library. The OS comes with the C++ Standard Library preinstalled and the compiler -- when in C++ mode -- automatically links to it, so all you have to do is include the necessary headers.
In order to understand what a header is and what's it's role you first need to understand the difference between declaration and definition. You can reefer to What is the difference between a definition and a declaration? or any other reading material.
Then you need to understand the concept of compilation units. You can read What is a “translation unit” in C++ or How is compilation unit defined in c++.
Using all the above you should to be able to compile multiple source files into one executable and understand the basics mechanism involved. You can read Using multiple .cpp files in c++ program or How to use multiple source files to create a single object file with gcc
From here there is a small step to be able to create and use your own static library. You can start here: How to create a static library with g++?
Another important concept to understand is compilation/linking: How does the compilation/linking process work?
...or alternatively all you need to know is that in order to use std::iostream you need to include <iostream>. Ultimately it's up to you how much you want to absorb as "because that's how it's done" and how much you want to understand. Progress can be made only if you chose one of the options (spoiler: it's the latter).

Best practice to partially change a C++ library while keeping the rest of the library intact

What is the best practice to add or modify a single class method in a well-established C++ library like OpenCV, while still reusing the remaining of the library code, preferably in the lib format.
At this point the only way I know is to copy all the source and header files that belong to the specific library (let's say OpenCV's core library) to the current source folder, modify that one function and recompile the module with the rest of the code. Ideally, I want to be able to link all the current .lib files the way they are, but simply define a new method (or modify a current method) for a class defined inside those libs in a way that my implementation of the method supersedes the implementation of the default library files.
Inheritance doesn't always seem to be an option, since sometimes the base class has private members that are required for the correct inherited class implementation.
I'm not aware of a clean way in C++ to accomplish what you're asking. What you're really asking to do (given that you need to use or modify private methods) is violate encapsulation, and the C++ language is designed to not let you do that.
A few options exist:
A .lib file is simply a collection of .obj files. Your compiler toolchain should have a command-line program for adding, deleting, and replacing .obj files in a .lib, so you could build one or two .obj files and merge them into the .lib. I suspect that this solution would be ugly and fragile.
If there's something that the library doesn't do and should do, then there's always a chance that you can submit a patch or feature request to the library authors to get that change made. Of course, this can take a while, if it works at all.
As #fatih_k suggests, adding your changes as friend classes would work. If the only change you make to OpenCV is to add a friend line to the header file, then the library's ABI will be unchanged, and you won't have to touch the .lib.
The cleanest option is to simply accept that you need to modify the OpenCV library and track its source code, along with your modifications, along with the source code you develop yourself, and build it along with the source code you build yourself. This is a very common approach, and various patterns and techniques exist to help you do it; for example, Subversion has the concept of vendor branches. This approach is more work to set up but is definitely the cleanest in the long run.
If the library is already compiled, there is not much you can do portably and cleanly.
If you know the specific target architecture the program will be runnning on, you could get the pointer to the member function and monkey patch the instructions with a jmp instruction to your own version of the method. If the method is virtual, you can modify the vtable. Those requires a lot of compiler-specific knowledge and would not be portable.
If the library ships in dynamic link archive, you could extract the archive and replace the method with your own version, and repack the archive.
Another method is you can copy the class' declaration from the header and add a friend declaration. Alternatively, you can do #define private public or #define private protected before including a header file. These will give you access to their private members.
With any of the above, you need to be careful that your changes does not modify the ABI of the library.
Well, OpenCV is licensed under BSD so you can make your changes without worries about republishing them.
You could always follow the Proxy design pattern and add the new method external to the library, and call into the library from there. That means you don't need to worry about maintaining your own version of OpenCV and distributing it as well. There's more information about Proxy patterns on Wiki to get you started.

Any tutorial for embedding Clang as script interpreter into C++ Code?

I have no experience with llvm or clang, yet. From what I read clang is said to be easily embeddable Wikipedia-Clang, however, I did not find any tutorials about how to achieve this. So is it possible to provide the user of a c++ application with scripting-powers by JIT compiling and executing user-defined code at runtime? Would it be possible to call the applications own classes and methods and share objects?
edit: I'd prefer a C-like syntax for the script-languge (or even C++ itself)
I don't know of any tutorial, but there is an example C interpreter in the Clang source that might be helpful. You can find it here: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/
You probably won't have much of a choice of syntax for your scripting language if you go this route. Clang only parses C, C++, and Objective C. If you want any variations, you may have your work cut out for you.
I think here's what exactly you described.
http://root.cern.ch/drupal/content/cling
You can use clang as a library to implement JIT compilation as stated by other answers.
Then, you have to load up the compiled module (say, an .so library).
In order to accomplish this, you can use standard dlopen (unix) or LoadLibrary (windows) to load it, then use dlsym (unix) to dynamically reference compiled functions, say a "script" main()-like function whose name is known. Note that for C++ you would have to use mangled symbols.
A portable alternative is e.g. GNU's libltdl.
As an alternative, the "script" may run automatically at load time by implementing module init functions or putting some static code (the constructor of a C++ globally defined object would be called immediately).
The loaded module can directly call anything in the main application. Of course symbols are known at compilation time by using the proper main app's header files.
If you want to easily add C++ "plugins" to your program, and know the component interface a priori (say your main application knows the name and interface of a loaded class from its .h before the module is loaded in memory), after you dynamically load the library the class is available to be used as if it was statically linked. Just be sure you do not try to instantiate a class' object before you dlopen() its module.
Using static code allows to implement nice automatic plugin registration mechanisms too.
I don't know about Clang but you might want to look at Ch:
http://www.softintegration.com/
This is described as an embeddable or stand-alone c/c++ interpreter. There is a Dr. Dobbs article with examples of embedding it here:
http://www.drdobbs.com/architecture-and-design/212201774
I haven't done more than play with it but it seems to be a stable and mature product. It's commercial, closed-source, but the "standard" version is described as free for both personal and commercial use. However, looking at the license it seems that "commercial" may only include internal company use, not embedding in a product that is then sold or distributed. (I'm not a lawyer, so clearly one should check with SoftIntegration to be certain of the license terms.)
I am not sure that embedding a C or C++ compiler like Clang is a good idea in your case. Because the "script", that is the (C or C++) code fed (at runtime!) can be arbitrary so be able to crash the entire application. You usually don't want faulty user input to be able to crash your application.
Be sure to read What every C programmer should know about undefined behavior because it is relevant and applies to C++ also (including any "C++ script" used by your application). Notice that, unfortunately, a lot of UB don't crash processes (for example a buffer overflow could corrupt some completely unrelated data).
If you want to embed an interpreter, choose something designed for that purpose, like Guile or Lua, and be careful that errors in the script don't crash the entire application. See this answer for a more detailed discussion of interpreter embedding.

How to document the functionality in C++20 modules?

When providing a library to others, documentation of e.g. class member functions can be included in the header files which contain the declarations. If properly formatted, this documentation can also be interpreted and displayed by IDEs.
Using C++ modules, how can I pass on information about the functionality in the module without falling back to external documentation, e.g. Doxygen? I assume comments do not make it as such into the compiled interface file?
You are right, this question is actually pointless. I thought that - unless provided with the complete source code - libraries would be provided completely in binary format when using modules. Although the idea of deploying just one library file without additional files has its charme.