GCC Optimization Leads to “Undefined Character” at Run Time

I now have a problem that confuses me quite a bit: I have a piece of software written in C ++ and related to a library in C. I include header classes using regular

extern "C" { #include <libheader.h> } 

Everything works fine until I use gcc optimization. As soon as I turn on even -O1, thus the first level of optimization, at runtime I get an “undefined” error for a character from this library. However, the name went through mangling, which must be disabled by extern "C" .

A function that calls the corresponding character is built-in, if that matters. The compiler used is gcc 4.4.3.

I honestly don’t even know what to look for, so I would be very grateful if any of you could give me a reason for this behavior.

Thank you for your support.

+6
c ++ optimization gcc
source share
4 answers

Is it possible that the header that defines the calling inline function includes the library header without the extern "C" shell, and wherever wrapping strings are used?

Have you tried other levels, like -O2 ?

Have you tried to parse your function?

+1
source share

You say that you include the libheader.h file in the extern "C" block, but the character the linker is looking for has been malformed by name.

Which indicates that libheader.h also included outside the extern "C" block (inclusion inside the extern "C" block is probably nop due to the inclusion of guards in libheader.h ).

Look for other ways to enable libheader.h . GCC -E and / or various -M options may or may not help with this. Or (if only for the test) move the extern "C" block inside libheader.h :

 // at start of libheader.h: #ifdef __cplusplus extern "C" { #endif /* existing contents of libheader.h */ // ... // at end of libheader.h: #ifdef __cplusplus } #endif 

Note that binding specifications can go into the socket, so you don't need to delete existing extern "C" blocks on #include sites.

I don’t know why the problem will occur only for optimized collections, except that maybe the .c or .cpp file containing the non-queued version of the calling function gets the headers on the right, and there should only be one translation unit, which fills the headers incorrectly and inserts the calling function to see the problem.

+4
source share

If you have a function that is defined, if it is not inline, but not defined during insertion, the problem should be fairly simple to define.

You used the function somewhere without including its header file.

Browse through all the files calling this function and make sure the header is enabled.

+3
source share

Wrapping the C header with extern "C" {...} does not always work; as a rule, headers should be designed to work with both languages ​​for them to work with both languages.

In this case, without any details, it’s hard to say exactly what is happening, but the inline means different things in C and C ++; I would not expect a header with inline functions to work this way. (At first, I would not expect the symptoms that you get, but they do not particularly surprise me.)

The proper way to handle this is to insist that the library provider provide a header that is designed to work in both languages.

Otherwise, the right way to handle this is to write your own C, which includes the title and wraps all the functions you need and write your own title for this C code, which is designed to be included in both languages. Which, I admit, is a lot of work; get the supplier to do their job, not do half of it for him.

0
source share

All Articles