extern "C"

This is a discussion on extern "C" within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Just to confirm what is the most correct way beyond a just working function. 1. We need to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    extern "C"

    Hello everyone,


    Just to confirm what is the most correct way beyond a just working function.

    1.

    We need to add extern "C" to both variable/function definition/declaration? Or only need to add to the variable/function declaration?

    2.

    How about extern? Declaration only or both declaration and definition are required?

    BTW: previously, I only add to declaration, but after reading more and more code which add to both declaration and definition, I come to here to ask this question.


    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    extern "C" basically tells the compiler not to mangle the names so you can call them from C instead of C++.
    Last edited by Elysia; 02-06-2008 at 07:40 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Elysia View Post
    Yes, you need to add it to both declaration/definition.
    extern "C" basically tells the compiler not to mangle the names so you can call them from C instead of C++.
    hmmm never used extern "C" in the cpp file - only in h-file

    If a function has more than one linkage specification, they must agree; it is an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program — one with a linkage specification and one without — the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration.
    So enough to declare the function as extern "C" once in a header and include this header before any other declaration/definition of the function to make its name C-styled
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    I don't know if compilers can treat it differently, though?
    Perhaps the declaration is enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The declaration is enough. It just has to be the first declaration.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    C functions should be placed in separate .c files. extern "C" is not enough to make the function compile as a C function; it only links it as a C function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    It's possible to call the a C++ function through C if you add extern "C".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on how you mean. You can use "extern C" to make a function that is callable from C, and this function then does C++ stuff, e.g.:
    Code:
    // someheader.h
    extern "C" void func1(void);
    
    // somefile.c
    class X
    {
    public:
      void doStuff();
      ...
    }
    
    void func1(void)
    {
       X x;
       x.doStuff();
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    Yes. That's how I see the extern "C" should be used.
    Otherwise if you're just going to compile C code, you might as well just compile a C dll.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    somefile.c
    That should be somefile.cpp, of course. .c will most likely be compiled as C, and no 'extern "C"' in the world will make a C compiler recognize classes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    That should be somefile.cpp, of course. .c will most likely be compiled as C, and no 'extern "C"' in the world will make a C compiler recognize classes.
    Naturally, sorry for the typo.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hello everyone,


    From your discussion above, I learned a lot. But are there a conclusion now, whether we should add extern or extern "C" to declaration only or both declaration and definition?

    Quote Originally Posted by matsp View Post
    Naturally, sorry for the typo.

    --
    Mats

    regards,
    George

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Declaration only.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    We are in unison - declaration only.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Looks like only add extern "C" to declaration is not safe, see http://www.glenmccl.com/ansi028.htm.


    Quote Originally Posted by Elysia View Post
    We are in unison - declaration only.

    regards,
    George

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern "C" problem
    By CodeBugs in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2009, 10:14 AM
  2. error C2059 with extern "C"
    By Elysia in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2008, 07:16 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Unsure of how to properly use extern "C"
    By INFERNO2K in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2005, 11:54 AM
  5. extern "C"
    By viaxd in forum C Programming
    Replies: 6
    Last Post: 12-19-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21