Including C headers inside a C++ program

后端 未结 5 899
情歌与酒
情歌与酒 2020-11-30 03:19

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, devi

相关标签:
5条回答
  • 2020-11-30 03:40

    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...:)

    0 讨论(0)
  • 2020-11-30 03:41

    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>
    
    0 讨论(0)
  • 2020-11-30 03:50

    Just include them inside a extern "C" block an they should work like expected.

    0 讨论(0)
  • 2020-11-30 03:51

    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"
    }
    
    0 讨论(0)
  • 2020-11-30 03:56

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

    0 讨论(0)
提交回复
热议问题