I'm trying to use C-sourced functions in my C++ code. And I have some difficulties, when I try to instantiate in my C++ code structure which is declared in C-header, and then transfer it to C-function by value. Example:
dummyc.h:
#ifndef _DUMMY_C_H_
#define _DUMMY_C_H_
typedef struct {
int a;
int b;
int c;
int d;
}DUMMYS;
int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy);
#endif
dummyc.c:
#include "dummyc.h"
int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy){
return 1;
}
dummycpp.cpp:
extern "C"{
#include "dummyc.h"
}
int main(){
DUMMYS s = {0,0,0,0};
return dummyFunction(50,50,1,s);
}
During dummyFunction execution I see that data on stack is incorrect. It seems like they where shifted?? How I can do this correctly??? Im using GCC 4.3.4 for ARM.
The 'extern "C"' directive really only matters for the function declarations. This is because C++ has function overloading. I've always embedded the directive around the function signatures in my header files, using '#ifdef __cplusplus'.
#ifndef _DUMMY_C_H_
#define _DUMMY_C_H_
typedef struct {
int a;
int b;
int c;
int d;
}DUMMYS;
#ifdef __cplusplus
extern "C" {
#endif
int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy);
#ifdef __cplusplus
}
#endif
#endif
There's no difference in how C and C++ puts members into structs (when the struct is entirely composed of things that are legal in both C and C++).
When you make this change in dummyc.h, you can remove the 'extern "C"' around the #include directive in dummycpp.cpp.
Related
I have a global variable which i want to share in the whole project.
// A.h
int a;
But i only write it in one place, and other place only need to read.
// B.h : only place can modify a
extern int a;
void mod(int b) {
a = b;
}
// C.h: only read
extern int a; // extern const int a doest work
void show() {
printf("%d", a);
}
Can i extern it in other place with const?
I tried in my code, but seems not work.
Can you give me some suggestions? is there any other method to achieve this if extern doest work?
The following piece of code compiles as well as executes fine.
What exactly does the extern int a statement mean after static int a.
Note that If i write static int a after extern int a, the compiler throws error as tests.cpp:6: error: a was declared extern and later static
#include<iostream>
//#include "testh.h"
using namespace std;
static int a;
extern int a;
int main()
{
int a;
a=3;
cout<<a<<endl;
cout<<::a<<endl;
return 0;
}
You can declare a variable static then extern, but not extern then static. The result is that the global a still has internal linkage. There is a very similar example (using b as the variable name) in the language standard doc, section [dcl.stc], that states this.
I tried to compile and link the second example (see below), in the second FAQ in this link in isocpp.org.
Naturally, this works only for non-member functions. If you want to
call member functions (incl. virtual functions) from C, you need to
provide a simple wrapper. For example:
// C++ code:
class C {
// ...
virtual double f(int);
};
extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}
Now C::f() can be used like this:
/* C code: */
double call_C_f(struct C* p, int i);
void ccc(struct C* p, int i)
{
double d = call_C_f(p,i);
/* ... */
}
After several trials, I succeeded executing the example in VS2015. But I'm still not convinced about the declaration extern "C" struct C *p = &c; that I had to use in other.cpp (I simply couldn't make the code to work with anything different than this). Note that the C++ compiler emits the following warning for the alluded declaration:
warning C4099: 'C': type name first seen using 'class' now seen using
'struct'
main.c was compiled with the C compiler and other.cpp with the C++ compiler.
main.c
/* C code: */
#include <stdio.h>
extern struct C *p;
double call_C_f(struct C* p, int i);
void ccc(struct C* p, int i)
{
double d = call_C_f(p, i);
printf("%f", d);
}
int main()
{
ccc(p, 1);
}
other.cpp
// C++ code:
class C {
public:
virtual double f(int i) { return i; };
} c;
extern "C" struct C *p = &c; // This is the declaration that I'm concerned about
// Is it correct?
extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}
The line extern "C" struct C *p = &c is IMHO more confusing than really useful. In the same C++ compilation unit you first declare C to be a class, then a struct. As already said in comment with refs in that other question, declaring a class once with struct and once with class may lead to mangling issues in C++ code.
And it is useless, because as C is not a POD struct (it contains method and even a virtual one) it cannot be used from C as a struct, and in fact you only use a pointer to C from c code as an opaque pointer.
So the line should be written:
extern "C" class C *p = &c;
declaring to the compiler that you are defining a pointer to class C with extern linkage named p, pointing to c. Perfectly defined from C++ perspective.
Next from C point of view, you declare the extern pointer p, pointing to an undefined struct C. C will only allow you to use it almost a void pointer, meaning you can affect it and pass it to function, but p->xxx would cause an error because struct C is not fully declared in that compilation unit.
In fact the following code does exactly the same as yours without any warning:
main.c
/* C code: */
#include <stdio.h>
extern struct D *p;
double call_C_f(struct D* p, int i);
void ccc(struct D* p, int i)
{
double d = call_C_f(p, i);
printf("%f", d);
}
int main()
{
ccc(p, 1);
return 0; /* never return random value to environment */
}
other.cpp
// C++ code:
class C {
public:
virtual double f(int i) { return i; };
} c;
extern "C" C *p = &c;
extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}
The usage of struct D is not a typo. Of course I should have used struct C to avoid confusion for the reader, but it is not a problem for the compiler not for the linker: p in main.c is just a pointer to an opaque not fully declared struct. That's the reason why, it is common to declare such opaque pointers as void *.
Is there any solution to run a function/macro that name is created by concatenation of two strings?
I just want to do something like that:
template <char *A, char *B, int C>
int function_that_run_other_function(void){
// Here is the main point
char function_name[80];
strcpy (function_name, "function");
strcat (function_name, A);
strcat (function_name, B);
return function_name(C);
}
I can do this using macro:
#define macro_that_run_function(A,B,C) \
function_##A##B##(C);
But I don't want to use macro because of many problems with macros.
Is it possible to do in C++ without any additional libraries?
I got curious and after a little while I ended up with this ungodly mess and general mainentace nightmare:
main.cpp:
#include <map>
#include <iostream>
#include <sstream>
#include <functional>
#include <cstring>
typedef std::function<void(int)> func;
typedef std::map<std::string, func> FuncMap;
template <char* A, char* B, int C>
void runner(FuncMap funcs){
std::stringstream ss;
ss <<A <<B;
return funcs[ss.str()](C);
}
void ABC(int val) {
std::cout <<"Woo: " <<val <<"\n";
}
extern char a[]; //due to external linkage requirement
extern char b[];
int main(...) {
FuncMap funcs;
strcpy(a, "A");
strcpy(b, "B");
funcs["AB"] = std::bind(&ABC, std::placeholders::_1);
runner<a, b, 0>(funcs);
return 0;
}
vars.cpp:
char a[5] = {""};
char b[5] = {""};
So yes with enough force you can make c++ do something along the lines of what you want, but I really wouldn't recommend it.
No, C++ does not allow the compile or run time manipulation or inspection of symbol names (barring the implementation specified type info stuff).
Dynamic libraries often export names (mangled for C++, almost unmangled for `extern "C"``), and libraries for loading them usually (always?) allow them to be loaded by string value.
I have 2 projects decoder and dec in my visual studio. One has C code and other has C++ code using stl respectively.How do I instantiate the c++ classes in my c code inside decode project?
for e.g.
//instantiating object
reprVectorsTree *r1 = new reprVectorsTree(reprVectors1,8);
//using one of its function
r1->decode(code);
What do I need to do for this?
How do I access files from another project?
How do I make use of existing c++ code in C files?
--------edit----------
I have a class like this
class Node//possible point in our input space
{
public:
std::vector<float> valuesInDim;//values in dimensions
std::vector<bool> code;
Node(std::vector<float>value);
Node::Node(float x, float y);
Node::Node(std::vector<float> value,std::vector<bool> binary);
};
How do I use the above class in c++?
If C only allows structs how do I map it to a struct?
Give the C++ module a C interface:
magic.hpp:
struct Magic
{
Magic(char const *, int);
double work(int, int);
};
magic.cpp: (Implement Magic.)
magic_interface.h:
struct Magic;
#ifdef __cplusplus
extern "C" {
#endif
typedef Magic * MHandle;
MHandle create_magic(char const *, int);
void free_magic(MHandle);
double work_magic(MHandle, int, int);
#ifdef __cplusplus
}
#endif
magic_interface.cpp:
#include "magic_interface.h"
#include "magic.hpp"
extern "C"
{
MHandle create_magic(char const * s, int n) { return new Magic(s, n); }
void free_magic(MHandle p) { delete p; }
double work_magic(MHandle p, int a, int b) { return p->work(a, b); }
}
Now a C program can #include "magic_interface.h" and use the code:
MHandle h = create_magic("Hello", 5);
double d = work_magic(h, 17, 29);
free_magic(h);
(You might even want to define MHandle as void * and add casts everywhere so as to avoid declaring struct Magic in the C header at all.)
In simple terms, you just do these:
Write an interface function to convert all the class functions (constructor, destructor, member functions) as pure functions, and encapsulate them as extern "C"{ }
Convert the pointer to the class as pointer to void, and carefully use type-cast wherever you define the "pure functions"
Call the pure functions in the C-code.
For example here is my simple Rectangle class:
/*** Rectangle.h ***/
class Rectangle{
private:
double length;
double breadth;
public:
Rectangle(double iLength, double iBreadth);
~Rectangle();
double getLength();
double getBreadth();
};
/*** Rectangle.cpp ***/
#include "Rectangle.h"
#include <iostream>
extern "C" {
Rectangle::Rectangle(double l, double b) {
this->length = l;
this->breadth = b;
}
Rectangle::~Rectangle() {
std::cout << "Deleting object of this class Rectangle" << std::endl;
}
double Rectangle::getLength() {
return this->length;
}
double Rectangle::getBreadth() {
return this->breadth;
}
}
Now here is my interface to convert the class functions to pure functions. Notice how the pointer to the class is handled!
/*** RectangleInterface.h ***/
#ifdef __cplusplus
extern "C" {
#endif
typedef void * RHandle;
RHandle create_Rectangle(double l, double b);
void free_Rectangle(RHandle);
double getLength(RHandle);
double getBreadth(RHandle);
#ifdef __cplusplus
}
#endif
/*** RectangleInterface.cpp ***/
#include "RectangleInterface.h"
#include "Rectangle.h"
extern "C"
{
RHandle create_Rectangle(double l, double b){
return (Rectangle*) new Rectangle(l, b);
};
void free_Rectangle(RHandle p){
delete (Rectangle*) p;
}
double getLength(RHandle p){
return ((Rectangle*) p)->getLength();
}
double getBreadth(RHandle p){
return ((Rectangle*)p)->getBreadth();
}
}
Now I can use these interface functions in my ".c" file as shown below. I just have to include the RectangleInterface.h function here, and the rest is taken care by its functions.
/*** Main function call ***/
#include <stdio.h>
#include "RectangleInterface.h"
int main()
{
printf("Hello World!!\n");
RHandle myRec = create_Rectangle(4, 3);
printf("The length of the rectangle is %f\n", getLength(myRec));
printf("The area of the rectangle is %f\n", (getLength(myRec)*getBreadth(myRec)));
free_Rectangle(myRec);
return 0;
}
Make wrapper for instantiating C++ objects using C++ exported functions.And then call these functions from C code to generate objects.
Since one is function oriented and other is object oriented, you can use a few ideas in your wrapper:-
In order to copy class member, pass an equivalent prespecified struct from C code to corresponding C++ function in order to fetch the data.
Try using function pointers, as it will cut the cost, but be careful they can be exploited as well.
A few other ways.
you would need to write a wrapper in C.
something like this:
in class.h:
struct A{
void f();
}
in class.cpp:
void A::f(){
}
the wrapper.cpp:
#include "wrapper.h"
void fWrapper(struct A *a){a->f();};
struct A *createA(){
A *tmp=new A();
return tmp;
}
void deleteA(struct A *a){
delete a;
}
the wrapper.h for C:
struct A;
void fWrapper(struct A *a);
A *createA();
the C program:
#include "wrapper.h"
int main(){
A *a;
a=createA();
fWrapper(a);
deleteA(a);
}