Priyanshu
Priyanshu

Reputation: 211

Using "C" function and variables in "C++" code

I need to call "C" function which is having one argument variable "struct" (which is defined in "C" file), from "C++" file.

I used 'extern "C"', but getting linking error.

Please suggest me some way to do it ?

Thanks Priyanshu

Upvotes: 2

Views: 161

Answers (2)

Lazer
Lazer

Reputation: 94820

You should read this thread How to mix C and C++ for understanding the ins and outs of mixing C and C++ code.

When done, you can also have a look at the corresponding FQA thread.

Upvotes: 0

Chubsdad
Chubsdad

Reputation: 25497

Since you have not come back, here is a possible file arrangement and header file you probably should be looking at.

// Here is Header file (myh.h)

struct S{};

#ifdef __cplusplus
extern "C" {
#endif

void fn(S s);

#ifdef __cplusplus
}
#endif

// Here is CPP file (ZCPP.cpp)

#include "myh.h"

int main(){
   S s;
   fn(s);
}

// Here is C file (ZC.c)

#include <stdio.h>
#include "myh.h"

void fn(S s){
   printf("Hi\n");
}

[prompt@test ~]$ g++ zcpp.cpp zc.c

[prompt@test ~]$ ./a.out

Hi

[prompt@test ~]$

Upvotes: 4

Related Questions