Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build fails with LTO due to ODR violations #157

Open
eli-schwartz opened this issue Aug 1, 2024 · 3 comments
Open

Build fails with LTO due to ODR violations #157

eli-schwartz opened this issue Aug 1, 2024 · 3 comments

Comments

@eli-schwartz
Copy link

I tried to build with the following *FLAGS to optimize the build: -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing

Note the -Werror=* flags are used to help detect cases where the compiler tries to optimize by assuming Undefined Behavior cannot exist in the source code -- if it does exist, ordinarily the code would be miscompiled, and this says to make the miscompilation a fatal error.

I got this error:

libtool: link: x86_64-pc-linux-gnu-g++  -fPIC -DPIC -shared -nostdlib /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/13/crtbeginS.o  src/base/.libs/libctemplate_la-arena.o src/.libs/libctemplate_la-per_expand_data.o src/.libs/libctemplate_la-template.o src/.libs/libctemplate_la-template_annotator.o src/.libs/libctemplate_la-template_cache.o src/.libs/libctemplate_la-template_dictionary.o src/.libs/libctemplate_la-template_modifiers.o src/.libs/libctemplate_la-template_namelist.o src/.libs/libctemplate_la-template_pathops.o src/.libs/libctemplate_la-template_string.o src/htmlparser/.libs/libctemplate_la-htmlparser.o src/htmlparser/.libs/libctemplate_la-jsparser.o src/htmlparser/.libs/libctemplate_la-statemachine.o   -Wl,--as-needed -L/usr/lib/gcc/x86_64-pc-linux-gnu/13 -L/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib/gcc/x86_64-pc-linux-gnu/13/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../lib64/crtn.o  -march=native -fstack-protector-all -O2 -fdiagnostics-color=always -frecord-gcc-switches -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Werror=format-security -Wl,-O1 -Wl,-z -Wl,pack-relative-relocs -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Wl,--defsym=__gentoo_check_ldflags__=0   -pthread -Wl,-soname -Wl,libctemplate.so.3 -Wl,-retain-symbols-file -Wl,.libs/libctemplate.exp -o .libs/libctemplate.so.3.0.0
./src/ctemplate/template_cache.h:61:8: error: type 'struct TemplateCache' violates the C++ One Definition Rule [-Werror=odr]
   61 | class  TemplateCache {
      |        ^
./src/ctemplate/template_cache.h:61:8: note: a different type is defined in another translation unit
   61 | class  TemplateCache {
      |        ^
./src/ctemplate/template_cache.h:357:16: note: the first difference of corresponding definitions is field 'mutex_'
  357 |   Mutex* const mutex_;
      |                ^
./src/ctemplate/template_cache.h:357:16: note: a field of same name but different type is defined in another translation unit
  357 |   Mutex* const mutex_;
      |                ^
./src/base/mutex.h:168:7: note: type name 'ctemplate::Mutex' should match type name 'Mutex'
  168 | class Mutex {
      |       ^
./src/ctemplate/template_cache.h:48:7: note: the incompatible type is defined here
   48 | class Mutex;
      |       ^
lto1: some warnings being treated as errors
lto-wrapper: fatal error: x86_64-pc-linux-gnu-g++ returned 1 exit status
compilation terminated.
/usr/libexec/gcc/x86_64-pc-linux-gnu/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:1779: libctemplate.la] Error 1

Originally reported downstream: https://bugs.gentoo.org/924637
Full build log: build.log

@OlafvdSpek
Copy link
Owner

How is a forward declaration of the type incompatible?

@eli-schwartz
Copy link
Author

There is nothing in this error message that is intrinsically about a forward declaration? The compiler is simply asserting the claim that they are different types with different names. One is ctemplate::Mutex and the other is Mutex.

template_cache.h.in declares Mutex one line below a namespace for ctemplate; mutex.h declares Mutex inside of a ctemplate namespace.

@OlafvdSpek
Copy link
Owner

class Mutex; is the forward declaration. It's not about the name. If it had a different name it'd be a different type.
I'll check what's going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants