Thread: extern "C"

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, in strange places, you may need to "do more" to make it work - it's pretty obvious when you do, so unless you are compiling HUGE chunks of code, it should (now that you are aware of it) be possible to rectify pretty easily.

    Of course, it's also a good idea to make typedefs of your function pointer types anyways - because it makes the function pointer easier to use generally, so the second pattern in the linked article is probably a good idea.

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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you feel it unsafe, you can always add extern "C" to your definitions as well. Then there will be no problems such as mentioned in the article.
    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. #18
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and Elysia,


    Let us discuss another topic of this thread -- extern -- other than extern "C". Currently, my understanding is,

    1. We should add extern to declaration, so what we know we are referring a function/variale from other compile unit;
    2. We should also add extern to definition so that this function/variale is visible to other compile unit. In this case, extern is on the other side of static.

    Right?


    regards,
    George

    Quote Originally Posted by matsp View Post
    Yes, in strange places, you may need to "do more" to make it work - it's pretty obvious when you do, so unless you are compiling HUGE chunks of code, it should (now that you are aware of it) be possible to rectify pretty easily.

    Mats

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    1. We should add extern to declaration, so what we know we are referring a function/variale from other compile unit;
    Yes and no. Functions are already visible in all compilation units, so no extern is required at all.
    For variables, you do indeed need to add a declaration with extern before it.

    2. We should also add extern to definition so that this function/variale is visible to other compile unit. In this case, extern is on the other side of static.
    No. All variables and functions have external linkage by default. That means you can access them from other compilation units by default. You can override this by adding "static" before them. In that case, you can't use the functions/variables in other compilation units.

    I'm only 90% sure about the functions side of the argument, but the variables should be right.
    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. #20
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    My question is answered.

    Quote Originally Posted by Elysia View Post
    Yes and no. Functions are already visible in all compilation units, so no extern is required at all.
    For variables, you do indeed need to add a declaration with extern before it.


    No. All variables and functions have external linkage by default. That means you can access them from other compilation units by default. You can override this by adding "static" before them. In that case, you can't use the functions/variables in other compilation units.

    I'm only 90% sure about the functions side of the argument, but the variables should be right.

    regards,
    George

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, 09:14 AM
  2. error C2059 with extern "C"
    By Elysia in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2008, 06: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