0

I have working code that runs fine on Linux but when I try to compile it in Windows 7 with Visual Studio 12 I get this error:

error C3646: '__attribute__' : unknown override specifier
error C2065: 'weak' : undeclared identifier
error C2072: 'operator <<' : initialization of a function

Does anyone know what could be causing this?

Napalidon
  • 135
  • 2
  • 11
  • 7
    `__attribute__` is a non-standard extension specific to GCC. It is not supported by MSVC. – Igor Tandetnik Sep 10 '13 at 18:36
  • @IgorTandetnik is there a way to make this code run on VS12? – Napalidon Sep 10 '13 at 18:53
  • That rather depends on what "this code" is. I can't help but notice that you haven't actually shown any. – Igor Tandetnik Sep 10 '13 at 18:55
  • @IgorTandetnik the code is just too big to be placed on the post it has several files, but the line it gets called on is 'ostream& operator<<(ostream&, const HCNode&) __attribute__((weak));' does that help? – Napalidon Sep 10 '13 at 19:05
  • I don't believe there's any equivalent in MSVC of `__attribute__((weak))`. Why do you want your method to be marked this way? What purpose is this supposed to achieve? I suspect you can solve this problem if you simply define away all uses of `__attribute__`, as in `#define __attribute__(X)` – Igor Tandetnik Sep 10 '13 at 19:08
  • @IgorTandetnik the problem is that this line was in the code given to us to do the assignment, so I don't really know what the intention of the code was but in the comments it says 'shut the linker up' if I remove it I get: error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class HCNode const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV0) already defined in compress.obj' error so I guess it makes this error go away but how to do this in VS12 I don't know :( – Napalidon Sep 10 '13 at 19:12
  • 2
    I see. Someone defined a function in a header, got linker errors, and decided to use a non-standard extension to work around the problem, rather than fixing the code. The correct solution: declare a function in a header file, as in `ostream& operator<<(ostream, const HCNode&);`. Implement it in exactly one source file, as in `ostream& operator<<(ostream, const HCNode&) { /* implementation goes here*/ }` – Igor Tandetnik Sep 10 '13 at 19:16
  • @IgorTandetnik Igor, just one more question. What if I have to implement operator<< in several different files? – Napalidon Sep 10 '13 at 19:32
  • 1
    @Napalidon then you have to tell the linker which one it will use somehow. Which is what the attribute at the end did. If you don't care which one then just prepend `__declspec(selectany)` (may not actually work for functions). If you do care, then you're probably best off putting each definition into it's own static library and using the linkers `/DEFAULTLIB` directive to choose the one you want. But honestly WHY do you need multiple definitions? – PeterT Sep 10 '13 at 19:37
  • @PeterT I probably don't just wanted to have an idea on what to do if later in my analysis of the code I find out I do. Thank you. – Napalidon Sep 10 '13 at 19:40
  • @PeterT now I get this weir error: error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class HCNode const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVHCNode@@@Z) referenced in function "public: void __thiscall HCTree::encode(class HCNode *,class BitOutputStream &)const " (?encode@HCTreeBitOutputStream@@@Z) 1>C:\Users\Bulldozer\documents\visual studio 2012\Projects\Hoffman\Debug\Hoffman.exe : fatal error LNK1120: 1 unresolved externals – Napalidon Sep 10 '13 at 20:01
  • @Napalidon do you have at least one definition in your code remaining? – PeterT Sep 10 '13 at 20:05
  • @PeterT I don't by my partner has almost the same code as I do and she doen't have it either however her code compiles and mine throws this weird error :( – Napalidon Sep 10 '13 at 20:17
  • 1
    @Napalidon well, if it's only "almost the same code" the error may lie in that difference or in the fact that she uses a different compiler/linker or different settings. – PeterT Sep 10 '13 at 20:19
  • @PeterT Ohhh brother, I feel so dumb, I just implemented operator<< in my code and everything works. Thanks again! – Napalidon Sep 10 '13 at 20:26

1 Answers1

3

__attribute__ Is GCC specific. It's a non standard extension.

That's why MSVC is complaining.

Here is a good answer on how to find a workaround to have the equivalent working on Visual Studio for.

In fact, it depends of your usage of the __attribute__ extension. But it is difficult to find an equivalent in MSVC.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • @PierreFourfeaud I tried reading the link you provided but unfortunately it is way over my head. I'm just starting out in C++ and have no idea how I would apply that info to my case. Is there an easier way like making all structs into classes? – Napalidon Sep 10 '13 at 18:53
  • @Napalidon it's used in a lot of [different ways](http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Variable-Attributes.html#Variable-Attributes), what are the actual parameters with which `__attribute__` gets called? You will need to change the source code to make it compile or use GCC on windows. – PeterT Sep 10 '13 at 18:58
  • @PierreFourfeaud not sure about the parameters but, I see in the error messages for VS12 that it is called on operator<< the code is 'ostream& operator<<(ostream&, const HCNode&) __attribute__((weak));' is the parameter 'weak' or 'operator<<'? – Napalidon Sep 10 '13 at 19:02
  • 1
    @Napalidon well the page defines it as `weak The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.` So you're safe to just delete the `__attribute__((weak))` if you don't override the function. – PeterT Sep 10 '13 at 19:06
  • @PierreFourfeaud But then I get a linker error: error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class HCNode const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVHCNode@@@Z) already defined in compress.obj – Napalidon Sep 10 '13 at 19:09
  • @Napalidon well then you actually do override it. Then you just need to find the equivalent in VC++. The answers to [this Question](http://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio) should help you – PeterT Sep 10 '13 at 19:14
  • @PeterT I was actually writing that [this answer](http://stackoverflow.com/a/11529277/1394283) would probably help him :) – Pierre Fourgeaud Sep 10 '13 at 19:15
  • Thanks guys, I'll read these posts and hopefully will figure it out. Appreciate you help! – Napalidon Sep 10 '13 at 19:17
  • @PierreFourgeaud well, that seems to be the most awkward solution, especially since it uses linker internal names apparently and I wouldn't know how to do the name mangling manually. I would refrain from using undocumented internal linker features. – PeterT Sep 10 '13 at 19:28