As @EduardoFernandes said, the only way is to use a DLL. But you have to follow other steps. Let's say you downloaded MinGW (or TDM) GCC for Windows. Create the file in C ++:
#ifdef BUILDING_DLL
#define __DLL__ __declspec(dllexport)
#else
#define __DLL__ __declspec(dllimport)
#endif
//Totalmente necessário (não sei por que, mas sei que funções em C++ causam crash). Só com wrappers você pode usar classes e outras coisas de C++ em sua aplicação C#.
extern "C"
{
/* coloque as funções aqui. Sempre use o seu prefixo __DLL__ (os nomes que eu escrevi são como FOO e BAR, pode escolher o que você quiser. */
int __DLL__ func() { return 0; }
}
Compile with GNU GCC g ++ :
g++ -c -DBUILDING_DLL -LC:\CaminhodaLib -lSuaLibParaLinkar arquivo_dll.cpp -o arquivo_dll.o
-D specifies our preprocessor , -c specifies the use to generate a non-executable file and -o is the name of the output file. Soon after a .o
file to append in applications or be compiled. Then proceed with the following command:
g++ -shared arquivo_dll.o -o sua_dll.dll -mwindows --out-implib
. -shared specifies to generate a dll, -mwindows is required (of course, C # ...), --out-implib means "compile to an external deployment library", which I think is the most logical thing to do. p>
When it will be used in your application, use it as follows:
[DllImport("sua_dll.dll")]
public class Implementacao
{
public extern int func();
public void uso_da_dll()
{
func();
}
}
It is good to remember that this is not valid for DLLs that have C ++ classes. It is necessary that you use some tool for this. One that I know of is the PInvoke Interop SDK .