Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:30:09+00:00 2026-06-15T09:30:09+00:00

In a C++ project, including .h files of C source files will cause many

  • 0

In a C++ project, including .h files of C source files will cause many errors because of different standards between C and C++.
How to use C source files in a C++ project (or in main.cpp)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:30:10+00:00Added an answer on June 15, 2026 at 9:30 am

    For the maximum reliability:

    • Compile the C source with a C compiler.
    • Compile the C++ source with a C++ compiler
    • Preferably, write the main() function in C++.
    • Link the program with a C++ compiler.

    Make sure that the C headers are either themselves aware of C++ or that the C++ code includes the C headers inside an extern "C" { ... } block.

    Either (C header file cheader.h):

    #ifndef CHEADER_H_INCLUDED
    #define CHEADER_H_INCLUDED
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    ...main contents of header...
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* CHEADER_H_INCLUDED */ 
    

    or (C++ source code):

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

    Modern C style is very close to the common subset of the C and C++ languages. However, arbitrary C code is not C++ code for any of a very large number of reasons, and simply calling the C source files C++ source files (by changing the extension, or simply by compiling with the C++ compiler) is not guaranteed to be successful. In general, it is easier to compile C as C and C++ as C++ and then link the resulting object files with the C++ compiler (to ensure the correct support libraries are invoked).

    However, if the MSVC compiler is saying that programs using MFC have to be written solely in C++ (MFC requires C++ compilation (use a .cpp suffix) is the reported error), then you may have no choice but to ensure that your C code is compilable as C++ code. That means you’ll have to cast the return values from malloc() et al; you have to worry about other places where you do not use a cast to convert a void * into some other pointer type; you have to worry about sizeof('a') == 4 in C and sizeof('a') == 1 in C++; you have to ensure that every function is declared before it is used; you have to ensure your C code does not use any C++ keywords (typename, class in particular; also inline sometimes — but the complete list is quite large).

    In some circles, you’d have to worry about the use of features in C99 that are not in C++2003 or C++2011, such as flexible array members, designated initializers, compound literals, variable-length arrays, and so on. However, if the C code is for MSVC, then that probably isn’t going to be a problem; those features are not supported by the MSVC C compiler (it only supports C89, not C99).

    FWIW: I have a script to hunt down C++ keywords. It contains the following comment:

    # http://en.cppreference.com/w/cpp/keywords
    # plus JL annotations
    # and                               C (<iso646.h>)
    # and_eq                            C (<iso646.h>)
    # alignas (C++11 feature)
    # alignof (C++11 feature)
    # asm                               C (core)
    # auto(1)                           C (core)
    # bitand                            C (<iso646.h>)
    # bitor                             C (<iso646.h>)
    # bool                              C99 (<stdbool.h>)
    # break                             C (core)
    # case                              C (core)
    # catch
    # char                              C (core)
    # char16_t (C++11 feature)
    # char32_t (C++11 feature)
    # class
    # compl                             C (<iso646.h>)
    # const                             C (core)
    # constexpr (C++11 feature)
    # const_cast
    # continue                          C (core)
    # decltype (C++11 feature)
    # default(1)                        C (core)
    # delete(1)
    # double                            C (core)
    # dynamic_cast
    # else                              C (core)
    # enum                              C (core)
    # explicit
    # export
    # extern                            C (core)
    # false                             C99 (<stdbool.h>)
    # float                             C (core)
    # for                               C (core)
    # friend
    # goto                              C (core)
    # if                                C (core)
    # inline                            C (core)
    # int                               C (core)
    # long                              C (core)
    # mutable
    # namespace
    # new
    # noexcept (C++11 feature)
    # not                               C (<iso646.h>)
    # not_eq                            C (<iso646.h>)
    # nullptr (C++11 feature)
    # operator
    # or                                C (<iso646.h>)
    # or_eq                             C (<iso646.h>)
    # private
    # protected
    # public
    # register                          C (core)
    # reinterpret_cast
    # return                            C (core)
    # short                             C (core)
    # signed                            C (core)
    # sizeof                            C (core)
    # static                            C (core)
    # static_assert (C++11 feature)
    # static_cast
    # struct                            C (core)
    # switch                            C (core)
    # template
    # this
    # thread_local (C++11 feature)
    # throw
    # true                              C99 (<stdbool.h>)
    # try
    # typedef                           C (core)
    # typeid
    # typename
    # union                             C (core)
    # unsigned                          C (core)
    # using(1)
    # virtual
    # void                              C (core)
    # volatile                          C (core)
    # wchar_t                           C (core)
    # while                             C (core)
    # xor                               C (<iso646.h>)
    # xor_eq                            C (<iso646.h>)
    

    The (1) suffixes is a footnote at CPP Reference:

    • (1) — meaning changed in C++11
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where another developer is including source files from a project
I've set up a really simple project using storyboards including two views as shown
I am having trouble including a test under a cmake project. My project is
Project: http://design.vitalbmx.com/fave/news.html When I click Add to favorites button (under main pic), in Chrome
The project currently has a UIviewController called Dashboard that acts as the main view
I want to use the boost library in a c++ project I'm doing in
Has anyone ever worked on a WordPress project with multiple developers in different locations?
I'm organizing our product's source code into a bunch of different Git repositories. (And
I am adding source control to a project that had none. The problem is
Morning! I've created a small NDK project which allows dynamic serialisation of objects between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.