Call a C function from C++ code
Compile the C code like this:
gcc -c -o somecode.o somecode.c
Then the C++ code like this:
g++ -c -o othercode.o othercode.cpp
Then link them together, with the C++ linker:
g++ -o yourprogram somecode.o othercode.o
You also have to tell the C++ compiler a C header is coming when you include the declaration for the C function. So othercode.cpp
begins with:
extern "C" {
#include "somecode.h"
}
somecode.h
should contain something like:
#ifndef SOMECODE_H_
#define SOMECODE_H_
void foo();
#endif
(I used gcc in this example, but the principle is the same for any compiler. Build separately as C and C++, respectively, then link it together.)
Is it possible to call a C function from C#.Net
The example will be, for Linux:
1) Create a C
file, libtest.c
with this content:
#include <stdio.h>
void print(const char *message)
{
printf("%s\\n", message);
}
That’s a simple pseudo-wrapper for printf. But represents any C
function in the library you want to call. If you have a C++
function don’t forget to put extern C
to avoid mangling the name.
2) create the C#
file
using System;
using System.Runtime.InteropServices;
public class Tester
{
[DllImport("libtest.so", EntryPoint="print")]
static extern void print(string message);
public static void Main(string[] args)
{
print("Hello World C# => C++");
}
}
3) Unless you have the library libtest.so in a standard library path like “/usr/lib”, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: export LD_LIBRARY_PATH=pwd
credits from here
EDIT
For Windows, it's not much different.
Taking an example from here, you only have yo enclose in your *.cpp
file your method with extern "C"
Something like
extern "C"
{
//Note: must use __declspec(dllexport) to make (export) methods as 'public'
__declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam)
{
printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam);
}
}//End 'extern "C"' to prevent name mangling
then, compile, and in your C# file do
[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
and then just use it:
using System;
using System.Runtime.InteropServices;
public class Tester
{
[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
public static void Main(string[] args)
{
ushort var1 = 2;
char var2 = '';
DoSomethingInC(var1, var2);
}
}
How to call a C function from Assembly?
You should add something like
extern _printer
on the top of your assembly and use call with this name
call _printer
Correct name of function depends on naming convention of your C compiler. Compiler may add some characters to the C name of the function.
Correct "extern" keyword depends on your assembler and it could be ".extern" or so.
Edit 1:
In Turbo Assembler and for that case with function without parameter, it should be just
extrn printer
or
extrn printer:NEAR
I am not familiar with TASM.
How to call C++ function from C?
You need to create a C API for exposing the functionality of your C++ code. Basically, you will need to write C++ code that is declared extern "C" and that has a pure C API (not using classes, for example) that wraps the C++ library. Then you use the pure C wrapper library that you've created.
Your C API can optionally follow an object-oriented style, even though C is not object-oriented. Ex:
// *.h file
// ...
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
typedef void* mylibrary_mytype_t;
EXTERNC mylibrary_mytype_t mylibrary_mytype_init();
EXTERNC void mylibrary_mytype_destroy(mylibrary_mytype_t mytype);
EXTERNC void mylibrary_mytype_doit(mylibrary_mytype_t self, int param);
#undef EXTERNC
// ...
// *.cpp file
mylibrary_mytype_t mylibrary_mytype_init() {
return new MyType;
}
void mylibrary_mytype_destroy(mylibrary_mytype_t untyped_ptr) {
MyType* typed_ptr = static_cast<MyType*>(untyped_ptr);
delete typed_ptr;
}
void mylibrary_mytype_doit(mylibrary_mytype_t untyped_self, int param) {
MyType* typed_self = static_cast<MyType*>(untyped_self);
typed_self->doIt(param);
}
How to invoke function from external .c file in C?
Use double quotes #include "ClasseAusiliaria.c"
[Don't use angle brackets (< >
) ]
And I prefer to save the file with .h
extension In the same directory/folder.
TLDR:
Replace #include <ClasseAusiliaria.c>
with#include "ClasseAusiliaria.c"
Calling C# method in C code
I have two options in mind:
- Check your build settings x86 or x64 for both projects.
- Verify calling convention. I think that in VC++ default is
cdecl
, so try to specify inDllImport
attribute as well as on the delegate declaration.
Calling a C function from within a function in a .mm file
Somehow I managed to figure out the problem. It had to do with a missing file. The solution I used I found here.
How can I call a C++ function from C?
Unfortunately, my first attempt answered the wrong question....
For the question you did ask...
You can, as someone point out, pass around void *
's. And that's what I would also recommend. As far as C is concerned, pointers to C++ objects should be totally opaque.
C++ functions can be labeled extern "C"
as well if they are in the global namespace. Here is an example:
myfunc.hpp:
#ifdef __cplusplus
extern "C" {
#endif
extern int myfunction(int, void *ob);
#ifdef __cplusplus
}
#endif
myfunc.cpp:
#include "myfunc.hpp"
void myfunction(int x, void *vobptr)
{
ClassType *ob = static_cast<ClassType *>(vobptr);
}
afoofile.c
#include "myfunc.hpp"
void frobble(int x, void *opaque_classtype_ptr) {
myfunction(x, opaque_classtype_ptr);
/* do stuff with buf */
}
The other option is to do basically the same thing with creative use of typedef
s in C. This, IMHO, is quite ugly, but here is an example anyway:
myfunc.hpp:
#ifdef __cplusplus
extern "C" {
#else
typedef void ClassType; /* This is incredibly ugly. */
#endif
extern int myfunction(int, ClassType *ob);
#ifdef __cplusplus
}
#endif
myfunc.cpp:
#include "myfunc.hpp"
void myfunction(int x, ClassType *ob)
{
// Do stuff with ob
}
afoofile.c
#include "myfunc.hpp"
void frobble(int x, ClassType *opaque_classtype_ptr) {
myfunction(x, opaque_classtype_ptr);
/* do stuff with buf */
}
Related Topics
Determining 32 VS 64 Bit in C++
Removing Leading and Trailing Spaces from a String
What Does a Colon Following a C++ Constructor Name Do
How to Provide a Swap Function For My Class
How to Set Up Google C++ Testing Framework (Gtest) With Visual Studio 2005
Why Should I Declare a Virtual Destructor For an Abstract Class in C++
How to Deal With Mutexes in Movable Types in C++
Why Is 'This' a Pointer and Not a Reference
How to Remove Certain Characters from a String in C++
Copy Constructor For a Class With Unique_Ptr
C/C++ Check If One Bit Is Set In, I.E. Int Variable
Rotate an Image Without Cropping in Opencv in C++
What Are the Pointer-To-Member Operators -≫* and .* in C++
How to Get the Md5 Hash of a File in C++
How to Get Available Memory C++/G++
Why Can't I Have a Non-Integral Static Const Member in a Class