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

  • Home
  • SEARCH
  • 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 7571275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:38:09+00:00 2026-05-30T15:38:09+00:00

Why shouldn’t extern C be specified for a function that needs to be defined

  • 0

Why shouldn’t extern "C" be specified for a function that needs to be defined as a C function? What effect would that have on the compiler when compiling the file as a C source?

If there is no effect on the C compiler, can’t we just define a function in a header file as below by removing the #ifdef __cplusplus check?

extern "C" {
    int MyFunc();
}

An answer to another question says that the #ifdef is needed, but I don’t understand why:

Regarding #2: __cplusplus will be defined for any compilation unit that is being run through the C++ compiler. Generally, that means .cpp files and any files being included by that .cpp file. The same .h (or .hh or .hpp or what-have-you) could be interpreted as C or C++ at different times, if different compilation units include them. If you want the prototypes in the .h file to refer to C symbol names, then they must have extern "C" when being interpreted as C++, and they should not have extern "C" when being interpreted as C — hence the #ifdef __cplusplus checking.

  • 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-05-30T15:38:10+00:00Added an answer on May 30, 2026 at 3:38 pm

    The construct extern "C" is a C++ construct and is not recognized by a C compiler. Typically, it will issue a syntax error message.

    A common trick is to define a macro, for example EXTERN_C, that would expand to different thing depending on if you compile using C or C++. For example:

    In a common header file:

    #ifdef __cplusplus
    #define EXTERN_C extern "C" {
    #define EXTERN_C_END }
    #else
    #define EXTERN_C
    #define EXTERN_C_END
    #endif
    

    In other files:

    EXTERN_C
    int MyFunc(void);
    EXTERN_C_END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an external function like this: extern C __declspec(dllexport) int __cdecl Identify(BSTR* bstrTemplates,
I know that extern C means the function should be compiled in C style
I have a C++ method signature that looks like this: static extern void ImageProcessing(
Take this file as example,there are many non-extern structures like: struct list_head source_list; How
I found some code recently where extern C was added in source file also
I have a standard c function with the following prototype extern void lcd_puts(const char
My question is about when a function should be referenced with the extern keyword
Shouldn't this be a pretty straightforward operation? However, I see there's neither a size()
Shouldn't the compiler automatically cast to a double in the following? At least according
Can an extern C function accept or return C++-specific data types, such as references,

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.