I have to call a c function declared in a lib file from c++. What instructions/attributes/configuration I have to set for this?
Asked
Active
Viewed 1,227 times
3 Answers
5
Do you have a header file for the library? If so it should have
extern "C" {
blah blah
}
stuff in it to allow it to be used by C programs. If not, then you can put that around the include statement for the header in your own code. E.g.
extern "C" {
#include "imported_c_library.h"
}

AlastairG
- 4,119
- 5
- 26
- 41
-
I don't think that will work! You need # before include, and no ; at the end. – TonyK Dec 01 '10 at 12:55
-
I was typing a bit too quickly. Need to re-read before hitting the button. – AlastairG Dec 01 '10 at 12:57
2
ensure you put extern "C" before the declaration of the function if it isn't already in the header.

CashCow
- 30,981
- 5
- 61
- 92
1
If you're writing the header files yourself, it's often nice to do something like this
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
so that this gets ignored by your c compiler, but picked up by c++ one. Incidentally, for a good discussion of why you need this, check out

Community
- 1
- 1

pythonic metaphor
- 10,296
- 18
- 68
- 110