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 579681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:23:31+00:00 2026-05-13T14:23:31+00:00

I found some code recently where extern C was added in source file also

  • 0

I found some code recently where extern “C” was added in source file also for functions. They were also added in the header files where they were declared.

I was under the assumption that adding ‘extern “C” in header files was sufficient.

Where should extern “C” blocks be added?

UPDATE:
Suppose I am compiling my C code using a CPP compiler and have added extern “C” guards for all the functions in header files (i.e. all my functions have their prototypes in headers), but in source files I have not added the same. Will this cause a problem?

  • 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-13T14:23:32+00:00Added an answer on May 13, 2026 at 2:23 pm

    Since you mean

    extern "C" { ... }
    

    style guards, these declare some functions to be of “C” linkage, rather than “C++” linkage (which typically has a bunch of extra name decoration to support things like overloaded functions).

    The purpose, of course, is to allow C++ code to interface with C code, which is usually in a library. If the library’s headers weren’t written with C++ in mind, then they won’t include the extern "C" guards for C++.

    A C header written with C++ in mind will include something along the lines of

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    ...
    
    #ifdef __cplusplus
    }
    #endif
    

    to make sure C++ programs see the correct linkage. However, not all libraries were written with C++ in mind, so sometimes you have to do

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

    to get the linkage correct. If the header file is provided by someone else then it’s not good practice to change it (because then you can’t update it easily), so it’s better to wrap the header file with your own guard (possibly in your own header file).

    extern "C" isn’t (AFAIK) ANSI C, so can’t be included in normal C code without the preprocessor guards.

    In response to your edit:

    If you are using a C++ compiler, and you declare a function as extern “C” in the header file, you do not need to also declare that function as extern “C” in the implementation file. From section 7.5 of the C++ standard (emphasis mine):

    If two declarations of the same
    function or object specify different
    linkage-specifications (that is, the
    linkage-specifications of these
    declarations specify different
    string-literals), the program is
    ill-formed if the declarations appear
    in the same translation unit, and the
    one definition rule
    applies if the declarations appear in
    different translation units. Except
    for functions with C++ linkage, a
    function declaration without a linkage
    specification shall not precede the
    first linkage specification for that
    function. A function can be declared
    without a linkage specification after
    an explicit linkage specification has
    been seen; the linkage explicitly
    specified in the earlier declaration
    is not affected by such a function
    declaration.

    I’m not convinced it’s good practice though, since there’s the potential for the linkage specifications to diverge by accident (if, for example, the header file containing the linkage specification isn’t included in the implementing file). I think it’s better to be explicit in the implementation file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer These variables show your current runtime status on windows: @rem… May 14, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer TimeCategory has some methods for getting a duration. You could… May 14, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer It's certainly possible to reduce the repetition: private bool Check(List<MyItem>… May 14, 2026 at 6:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.