Call C++ class member functions in a static library from MATLAB - c++

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?

Related

Include curl library for C++ in dll file

I'm writing a dll as a mod for Skyrim, however I need to use an external library as well. The library I need is the curl library for C++ (curlpp). Skyrim will only load the one DLL I create, therefore I need to have the entire library be compiled into the DLL file along with my code. I've never done this before, so I'm kind of lost on how to do this. Could I just copy the files from the curlpp library into my project?
If anyone could clarify the process needed I'd be very grateful.
So to recap, I'll have my own code, and the library. Those 2 should be merged into a single DLL when compiling.
Thanks!
You could include the libcurl library code directly into your DLL, as libcurl is open-source.
Or you could compile libcurl into its own DLL (or find a 3rd party precompiled DLL) and have your DLL statically link to it. That way, if libcurl fails to load at runtime, your mod DLL will also fail to load, which Skyrim can detect and handle as needed.

How to use FFTW DLL import library .lib as a static .lib in my project?

I've knowledge of C++ and compiling small plug-ins (always based on a similar and laid out workflow). Though currently I need to compile the latest version of FFTW into a static library (or get it from the compiled version which should supposedly be a lot easier) and it's giving me an insanely hard time. Probably because I don't know the ins and outs of compiling. As a note I'm working on Windows with Visual Studio.
Since I really want to know how this should work I'm asking the question here.
Basically I need a static .lib of fftw3f library on Windows that I can include in another Visual Studio project.
The first I did was download the 64 bit dll's of FFTW from the website (hoping I could use this).
http://www.fftw.org/install/windows.html
Then I've done the accompanying step they stated, that is:
Run the following in lib.exe.
lib /def:libfftw3-3.def
lib /def:libfftw3f-3.def
lib /def:libfftw3l-3.def
I've done this as well and got a .lib file that I could use in my project correctly. I've been able to compile my project, yet the output build apparently dynamically linked to the library instead of including it as a static library.
I'm compiling my project to .dll but require the fftw3f library to be statically included in my output.
Using lib.exe /list libfftw3f-3.lib I was able to find out that the lib file was referencing the libfftw3f-3.dll.
According to my google results this would mean the .lib file that I created is a DLL import library instead of static library.
It's hard to figure out how to phrase my question because I'm not sure what the terminology is and what's going on behind the scenes. I guess:
How can I use the libfftw3f-3.lib file created from lib.exe as a static library in my own project. So that it is included in my output .dll instead of dynamically linked?
Based on what I learn from comments/answers I'll be happy to update/edit/rephrase my question to make more sense for most other users as I'm likely way of with the terminology
You cannot expect to convert a DLL into a static library.
In order to create a static library you need to re-compile the library from its source code, with an output target that is a static library. If you cannot obtain the source code, then your goal cannot be achieved.

How to reuse static library code which is already linked into a DLL with another C++ application in visual studio 2010?

I'm working on a C++ solution in Visual Studio 2010. I've a DLL file which is using some standard C++ library functions (such as string or file functions). For some portability reasons I have to compile this DLL with /MT option, so all required runtime library functions will be linked to the released DLL file.
I've another C++ project which is a windows application, this project also compiles with /MT option and generates an standalone exe file. The second project also uses the same standard C++ library functions that are already linked in my DLL (The executable also uses some DLL exported methods).
Now here is my question: Is there any way to tell linker that don't link common runtime functions which already linked to DLL file & don't link these shared parts again in the exe file (e.g. reuse same code for string functions which already linked to my DLL)?
No, you can't do that. Although executable depends on DLL, they can still be considered as separate and stand-alone binary artifacts, each of which should contain required symbols for proper execution.
This is one of the reasons why dynamic linking is preferred. Furthermore, I don't see any problem to link dynamically and redistribute the runtime with your application.
Although Microsoft Visual C Runtime is included on most platforms, there are many different versions of it, some of which are buggy or/and break backward compatibility. Thus, it is always a good idea to distribute the version of msvcr*.dll that you know works for sure with your application.

Wrapping C++ library in XCode

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...

How to use dll's?

I know that if I have an .a or .so file and a header file for that library (for example for SystemC), I should
1. include header file
2. link the appropriate library.
But I can't handle with only .dll file as I can link it as well, but don't have a hearer file to include and to use commands. Con someone explain me what kind of .dll-s exist and how they are possible to use? Is it possible to use any .dll file or it should be a specific kind of .dll to be able to integrate to my application?
A DLL is functionally equivalent to a .so file (also know as a 'shared object' or 'shared library'). You need a header to declare the functions that are available inside the DLL and you need to link against a library which handles the business of loading and executing DLL calls (mostly delegated to the OS).
It is possible to use a DLL without any sort of header. You can directly call Win32 API's which will dynamically load a DLL into your programs virtual address space and call other API's which will give you what are essentially function pointers. However, you need to know the signatures of the function pointers to use the properly so what you're effectively doing in that case is declaring a tiny subsection of the actual DLL header for your use.
This wikipedia article may help, especially the section on shared libraries
Unlike Linux, Windows libraries are seperated into two forms: DLL (for runtime linking) and LIB for symbol declarations. link.exe (the windows linker) expects .lib files to resolve symbols being used by your program's headers during build time. More information here:
http://msdn.microsoft.com/en-us/library/ba1z7822(VS.71).aspx
Note that if you load a DLL compiled in C++, you hvae to avoid passing object pointers across the interface, as they are in general not portable. You have to keep to basic C calls and calling conventions, as that is what is defined by the Windows or Linux platform ABI.