简体   繁体   中英

Including C headers inside a C++ program

I have a C++ program (.cpp) inside which I wish to use some of the functions which are present inside the C header files such as stdio.h, conio.h, stdlib.h, graphics.h, devices.h etc.

I could include the stdio.h library inside my cpp file as : #include <cstdio> . How do I include the other library files?

How do I add the graphics.h library?

I'm using Microsoft Visual Studio 6.0 Enterprise Edition and also Turbo C++ 3.0.

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

For other headers, use

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

If you put this inside your headers:

#ifdef __cplusplus
extern "C"
{
#endif

// your normal definitions here

#ifdef __cplusplus
}
#endif

Then it will work for both C and C++ without any problem ...

Hope this helps...:)

I'm not sure what you need exactly, but if you want to use old fashioned C functions inside you C++ program, you can easy include them by removing the .h and add a "c" prefix.

for example if you want to include math.h use

#include <cmath>

只需将它们包含在extern "C"块中,它们就可以按预期工作。

You can #include them using their original names. #include <stdio.h> works just fine in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM