I'm currently working on a scheme compiler. At the moment im writing the runtime with the help of Boehm GC, with the goal to be able to call the functions from llvm-ir later on. What is the best way to link everything together in the end?
Currently I am building a static libary out of my C++ runtime which uses extern "C" in order to prevent name mangling.
Are there good examples which demonstrate how to do it?
Thanks in advance
You can generate llvm-ir for your static library using clang++. Then link that with your compiler's output (i.e the llvm-ir from which you intend to call the library functions) using llvm-link. And you've got your final ir.
Related
I have an msvc dll that has some functions that I want to call with go. I understand that it's not going to be reasonable to wrap all of the functionality of a large project, but I only need a subset of functionality and the time spent on wrapping the c++ with SWIG is reasonable for me.
The potential issue that I've discovered is that the c++ ABI is not compatible between mingw/gcc and msvc, so if I compile the generated wrapper with gcc as specified in the SWIG documentation, it probably won't be able to call the functions in the DLL.
The first question is whether this understanding is correct.
The second question is that assuming the above is true, my plan is to "cdecl(dllexport)" all of the "externs" in the swig wrapper and then "cdecl(dllimport)" all of the "externs" in the cgo section in the generated go file, then compile the DLL with msvc.
My preference, however, would be to compile the object file with msvc and then rename it to .syso so that the go linker will automatically pick it up.
The second question is whether either of these two options are reasonable and/or likely to be successful.
Thanks in advance.
In C# I can use CSharpCodeProvider to take in a file and compile it on the fly.
I want the same thing for C++. Essentially I'm trying to compile a .dll from a file specified at runtime and dynamically link it to the executing program.
I'm sure there's some crazy library out there that does this, but what I was hoping is that there is a library in either the Standard Library or Boost which does this. Does anyone know of one?
No, there is nothing like this in the standard library or boost.
There is however clang which is a full C++ compiler built on LLVM which is organized as a library that you can (with "some" work) use in your program.
#Perreal also pointed out correctly that if you are using C++/CLI (which is usually not included when talking about C++ in general), you can access a .NET component that will allow you to compile C++/CLI code - but not native C++ code.
I have a C++ header file and the corresponding static library (.lib file). No source file.
The code comprises several classes, each with functions of its own.
I would like to be able to call these functions from MATLAB.
Would anyone be kind enough to point me in the right direction? Examples would be appreciated.
Thanks.
EDIT: Would it be a good idea to write a DLL to act as an interface between the static library and MATLAB? Then I can access the functions in my static library from the DLL and use calllib to access the DLL from MATLAB.
If so, how would I call the class member functions in the static library from a DLL?
Update
So I've decided to write a DLL to which I will link the static library and call the functions from MATLAB through that using calllib
The problem is, the static library is already compiled and I do not have the source. I try to link the .lib file (presumably built using Visual Studio) and build my DLL using MINGW32 but this doesn't work possibly due to (lack of?) interoperability of libraries created by different compilers.
This is the MinGW command I use to link: g++ -c -o -DBUILDING_INTERMEDIATE_DLL intermediate_dll.cpp -TaccClient.lib
intermediate_dll.cpp is the source file for my DLL and TaccClient.lib is the static library I'm using.
Does anyone have any suggestions as to how this could work, or will I have to wait and try it on visual studio later?
MATLAB offers MATLAB coder functionality that allows you to create MEX files from your C/C++ source and library files. There is also an option to called C shared libraries when building the MEX file for your application. I have used SIMULINK (with SIMULINK Coder) and Embedded Coder to do my library linking and it helps you to do it via GUI, so very neat job. However, you can still do it with MATLAB as long as you have a supported C/C++ compiler.
Using the C shared library option, you should be able to load, call, view the library and functions. For more details on what other functionlities are available, see this - http://uk.mathworks.com/help/matlab/using-c-shared-library-functions-in-matlab-.html
A similar question was answered here. May be you will find it useful?
I need some help with wrapping C++ libraries in XCode.
What I want to achieve is to create new library in XCode, import C++ library (I have .a and .h files), wrap it to Obj-C so I can import that library to MonoTouch.
The reason why I do it round way is that when I try to import C++ lib into MonoTouch, because of name mangling I keep getting WrongEntryPoint exceptions. Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler.
Thank you in advance
Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler.
Technically you could. Many compilers share the same mangling syntax, maybe the most useful and long-lasting gift from Itanium ;-)
However it will bring it's own pain (e.g. non-primitive types, other compilers) and maintenance issues as you update your C++ code.
You'll better served by:
writing an ObjectiveC wrapper and use MonoTouch's btouch tool to generated bindings;
writing a C wrapper and using .NET p/invoke to call your code;
The choice it yours but if you think about reusing the C++/C# code elsewhere (e.g. Mono for Android) then using C and p/invoke will be reusable.
I would definitely recommend going the route of wrapping the library in an Obj-C library and using btouch to import the library into MonoTouch. I have recently done this for a C++ library that implemented a Sybase database engine. If you look at my questions you will find quite a few pertaining to wrapping C++ libraries as I posted a few times regarding issues I encountered.
Specifically, you can look at these questions:
Linking to a C++ native library in MonoTouch
Wrapping a C++ library in Objective-C is not hiding the C++ symbols
Application with static library runs on simulator but not on actual device
Undefined symbols when linking PhoneGap static library in MonoTouch
Linker options 'Link all assemblies" and "Link SDK assemblies only" causes undefined symbols in 3rd party static library
I would also recommend, if you are going to go the route of an Obj-C wrapper, that you get btouch to output code and include that in your project rather than including a dll from btouch. From my experience, the code worked more reliably than the dll, although the issues with the dll may have been resolved by now. But take a look at this question regarding the btouch issue:
Exception System.InvalidCastException when calling a method bound with btouch that returns an object. MonoTouch bug?
If you have specific questions/problems in building the Obj-C wrapper then ask them here and post some code and I am sure that I or other members of the community would be able to help you with it.
Bruce, as you assumed, I have problems with wrapping C++ code. After hours and hours of reading and trying, I couldn't wrap the C++ code.
Anyway, I managed to create a simple Obj-C library made of some dummy class, and then import it into another library. That worked fine. However, following same pattern, I included C++ .a file along with .h file (I'm not sure whether .h is mandatory because we can link header files in build options, right??) and when I compiled it, it went fine, the build succeeded, but XCode didn't produce new .a library.
I added linker flags: -ObjC -lNameOfLib
Does C++ Standard Library Type in Build - Linking has to be Static? And Symbols Hidden By Default as well?
It would be great if we could write step-by-step tut, since there are tons of various instructions, but I haven't been able to push it through the end.
I'm confused a bit..
Thank you guys...
I have a C++ dll file that uses a lot of other c++ librarys (IPP, Opencv +++) that I need to load into matlab. How can I do this?
I have tried loadlibrary and mex. The load library does not work.
The mex finds the linux things (platform independent library) and tries to include them. And that does not work.
Does anyone have any good ideas?
loadlibrary should work. I use it all the time to call functions from dlls written in C++ with C wrappers.
What errors are you getting when you try to use loadlibrary?
Make sure that the exported functions of the dll are C functions, not C++ functions. If not, then write C wrappers.
More info on exactly what you are doing when using loadlibrary would be helpful.
As mentioned by others, you should first wrap your C++ library as a C library - mathworks used to advise not to use C++ code directly in mex (dlopening C++ core directly is complicated), maybe it is still true.
Your description is quite unclear, too: what do you mean by "mex finds the linux thing", but that does not work. Saying that it does not work is not helpful: the exact commands and error message are.
You could go for the Java approach (since Matlab runs on a JRE and can access Java objects/methods -- just be aware that the Matlab JRE is not as up-to-date as the latest JRE, the one I'm running uses Java 1.5) and use JNA to access your DLL.
Or, if you wrote the top-level DLL, you could go for the COM/ActiveX approach.
I've had good success architecting the interface to my C++ functions as COM/ActiveX libraries -- you don't have to bother with that .h stuff.
See the External Interfaces guide on COM clients, particularly the part about managing/converting data.
It would be extra work to add the COM/ActiveX layer, but would make your library more portable within the Windows world and probably more easily used in MATLAB.
If you have a lot of function calls to your DLL, the COM/ActiveX approach might be faster (not sure), but otherwise I think the JNA approach would be easier.