[cfe-dev] Link error in clang but not g++

Holger Brandsmeier holger.brandsmeier at sam.math.ethz.ch
Wed Sep 14 09:55:07 PDT 2011


Dear clang list,

first thank you a lot for providing clang, clang is really so much
better for error messages (especially templates) than gcc!

I build a shared library essentialy with the CMake part

add_library(integration quadraturePy.cpp quadratureRulesPy.cpp)

In the file `quadratureRulesPy.cpp` the constructor for the template
QuadratureRules<double, 0> is define. It is also part of the object
file:

> nm -C CMakeFiles/integration.dir/quadratureRulesPy.cpp.o | grep "parfem::QuadratureRules<double, 0>::QuadratureRules"
00000000 W parfem::QuadratureRules<double, 0>::QuadratureRules(unsigned int)

In the file `quadraturePy.cpp` this symbol is not defined, and thus
also not part of the object file:

> nm -C CMakeFiles/integration.dir/quadraturePy.cpp.o | grep "parfem::QuadratureRules<double, 0>::QuadratureRules"
         U parfem::QuadratureRules<double, 0>::QuadratureRules(unsigned int)

In the final library both object files are combined and the symbol
appears twice:

> nm -C libintegration.so | grep "parfem::QuadratureRules<double, 0>::QuadratureRules"
         U parfem::QuadratureRules<double, 0>::QuadratureRules(unsigned int)
000ad400 W parfem::QuadratureRules<double, 0>::QuadratureRules(unsigned int)

Once this library is loaded from python, I get the following linker error:

ImportError: libintegration.so: undefined symbol:
_ZN6parfem15QuadratureRulesIdLi0EEC1Ej
(where c++filt shows that the problem is exactly this symbol.

-----------------------------------

On the other hand, if I do exactly the same thing with g++ the symbol
is only contained once in the shared library. And there is no problem
when loading the library from python.

>  nm -C libintegration.so | grep "parfem::QuadratureRules<double, 0>::QuadratureRules"
00091d60 W parfem::QuadratureRules<double, 0>::QuadratureRules(unsigned int)
0009fe40 r parfem::QuadratureRules<double,
0>::QuadratureRules(unsigned int)::__PRETTY_FUNCTION__

(I am not sure about the __PRETTY_FUNCTION__ part but I assume this
has nothing to do with the function itself.)

Am I using clang in a wrong way? Is there a special compiler flag that
I should be adding?

The compiler flags I'm using are: -O3 -g -fPIC
The linker flags are additionally: -Wl,-E -shared
For the `-Wl,-E` I followed the guide in `http://gcc.gnu.org/faq.html#dso`.

Given that I don't know better I pass the same flags to clang and g++.

Thanks for any help,
Holger

-- 
Holger Brandsmeier, SAM, ETH Zürich
http://www.sam.math.ethz.ch/people/bholger




More information about the cfe-dev mailing list