[cfe-dev] Why this extern "C" fails ?

Devchandra L Meetei dlmeetei at gmail.com
Mon Jul 29 23:01:28 PDT 2013


Did not find the reference telling extern( May be need a thorough searching
in the std. But quick search gave this
copy pasted from Section 7.1.1

6) The extern specifier can be applied only to the names of variables and
functions. The extern specifier cannot
be used in the declaration of class members or function parameters. For the
linkage of a name declared with
an extern specifier, see 3.5. [ Note: The extern keyword can also be used
in explicit-instantiations and
linkage-specifications, but it is not a storage-class-specifier in such
contexts. — end note ]

So the usage
extern "C" { int a = 42; }
seems to be illegal as it is neither variable and function.

But again, I am not C++ language specialist and not a compiler guy


On Tue, Jul 30, 2013 at 11:14 AM, Arji Cot <arjicot at gmail.com> wrote:

> "extern symbols should be in a global scope"
>
> Can you provide a reference for this statement ?
>
>
> 2013/7/29 Renato Golin <renato.golin at linaro.org>
>
>> On 29 July 2013 14:56, Arji Cot <arjicot at gmail.com> wrote:
>>
>>> --------------
>>>
>>> #include <iostream>
>>>
>>> int main() {
>>>   {
>>>     extern "C" { int a = 42; }
>>>     { std::cout << a << "\n"; }
>>>   }
>>>   return (0);
>>> }
>>
>>
>> Hi Arji,
>>
>> As Olafur said, extern symbols should be in a global scope, and that "a"
>> couldn't even be seen from the next line, and you'd see another error from
>> the compiler if you removed the extern.
>>
>>
>> main.cpp:5:12: error: expected unqualified-id
>>>      extern "C" { int a = 42; }
>>>            ^
>>> 1 error generated.
>>>
>>> It's not enough to me to explain why this is not working.
>>>
>>
>> Every compiler error assumes familiarity with the language, at least
>> enough to go and look at the definition of the terms involved in the error
>> message (extern "C" in this case) in a book, on the web or the standard,
>> which should give you enough information on how to fix the bug.
>>
>> Compilers are famous for not giving enough information on error messages,
>> and Clang stands out as being one of the best on that side, but no compiler
>> will ever teach you the language via error messages.
>>
>> One rule of thumb is to use a different compiler and see if it also gives
>> you an error, maybe a different error, or a better error message, or none
>> at all. If you find that other compilers can handle your code and Clang
>> cannot, maybe Clang is wrong (in which you should fill a bug), or maybe the
>> other compiler is wrong at accepting your code, (in which you should file a
>> bug in the other compiler). However, if the error is the same (which it is
>> in GCC), than there's little you can ask the Clang community to do
>> something.
>>
>> As for learning C++, I suggest you buy Stroustroup's "The C++ Programming
>> Language", read it cover to cover, and have it as your faithful companion
>> for such cases, or use comp.lang.c++ for when you can't find the answer
>> yourself, but Clang's list is really not the place for that level of
>> discussion.
>>
>> cheers,
>> --renato
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>


-- 
Warm Regards
--Dev
OpenPegasus Developer/Committer

(\__/)
(='.'=) This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130730/27cd3ec2/attachment.html>


More information about the cfe-dev mailing list