Bahram
Bahram

Reputation: 1642

some problems with c and c++ header file

I have a confusing problem in using c functions in c++ class functions.

I have a class named A which is defined in A.h and implemented in A.cpp. And also I have B.h and B.c which has declared and implemented some functions.

Inside A functions I have called functions which are defined in B.h and B.c (there is no class B), I think this is a usual stuff but I get compiler error which says Unresolved reference or something else which pointing to the functions of B.

I have #include "B.h" at the start of A.cpp and, my compiler is GCC under Linux (opensuse 12.3), and I am sorry that I can not show you the codes because of copyright.

This is a confusing to me, I am not a C++ pro but I know the way that C++ header and source files working together, so just I am asking for help if someone have similar experience about this.

Thanks

Upvotes: 2

Views: 907

Answers (3)

simonc
simonc

Reputation: 42165

You need to compile B.c as well as as include its functions

gcc -Wall A.cpp B.c -o my_prog

If you're doing that, make sure to add the following guard to B.h to avoid name mangling of the C functions

#ifdef __cplusplus
extern "C" {
#endif

/* declare your C functions here */

#ifdef __cplusplus
}
#endif

Upvotes: 3

Paul Evans
Paul Evans

Reputation: 27567

You want:

extern "C" {
    #include "B.h";
}

Upvotes: 3

Abhishek
Abhishek

Reputation: 882

Create a seperate header --> S.h, include function()'s of both A.c and B.c in it.. and include it wherever you want to..

or

For a list of C standard C headers (stdio, stdlib, assert, ...), prepend a c and remove the .h. For example stdio.h becomes cstdio.

For other headers, use

extern "C"
{
  #include "other_header.h"
}

Upvotes: 0

Related Questions