2

I'm running into an odd issue where gcc appears to be changing the function signature and discarding a const qualifier on a parmaeter.

I have a method in a namespace declared as

namespace Foo {
    bool CompressJPEG_8(const Buffer &input,
        const unsigned int nWidth, const unsigned int nHeight,
        const unsigned int nNumComponents, Buffer &compressed);
}

gcc complains that the method Foo::CompressJPEG_08(Foo::Buffer const& input ... ) is an undefined reference.

When I run nm, on the generated object code I see:

U Foo::CompressJPEG_8(Foo::Buffer const&, unsigned int, unsigned int, unsigned int, Foo::Buffer&)
0000000000192f5c T Foo::CompressJPEG_8(Foo::Buffer&, unsigned int, unsigned int, unsigned int, Foo::Buffer&)

For some reason gcc has removed the const qualifier on the first parameter and I can't figure out why. I suspect that is has something to do with the fact that the jpeg library methods are declared in anextern "C" { } block but I'm not sure why.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
bfgorski
  • 29
  • 2
  • The only `const`s it's removing are the ones that make no difference. It moved one to a different position and got rid of some `const` copies. No big deal. – chris Jun 04 '14 at 19:07
  • this should probably not be tagged `c` – Andreas Grapentin Jun 04 '14 at 19:07
  • Basically, this shouldn't concern you. Compiler is making a lot of optimization you don't need to be aware of – 101010 Jun 04 '14 at 19:08
  • I don't understand the previous comments at all. The OP is getting a linker error. How on earth is "don't concern yourself with that" a valid comment? –  Jun 04 '14 at 19:11
  • 5
    That said, either you've found a GCC bug, or there's something important missing from your question. Your question shows the declaration of your function, but it doesn't show the definition. What does that look like? If the definition is missing the `const`, you won't get any compiler diagnostic, because that's perfectly valid way to overload the function. Another less likely possibility is that some badly-written header is redefining `const`. –  Jun 04 '14 at 19:14
  • 2
    Top level `const`s are always discarded from a function's signature. For instance, `void foo(int);` and `void foo(const int);` are the same. But your first parameter should definitely be retaining the `const` – Praetorian Jun 04 '14 at 19:23
  • 2
    I notice that your given declaration is `CompressJPEG_8` but your given definition is for `Foo::CompressJPEG_08` -- could that be what's causing the trouble here? – jthill Jun 04 '14 at 19:46
  • @hvd The concern is the difference in function name (`_08` vs `_8`), not the namespace. It might just be a typo from the process of transcribing the error here, though (accidentally hitting `0` after `_`). – nobody Jun 04 '14 at 19:55
  • @AndrewMedico Oh! I completely missed that. Thanks for pointing it out. –  Jun 04 '14 at 19:57
  • @hvd I see I wrote "definition" instead of "reference". As I recall your objection it was on target for what I actually said. – jthill Jun 04 '14 at 20:03
  • I checked that the function definition and declaration match, so I think _08 and _8 was just a typo. The issue I can't make sense of is why the first const in the method signature is being dropped. I implemented a workaround but it's not ideal. – bfgorski Jun 04 '14 at 22:19

0 Answers0