开发者

Using boost with explicit template instance

开发者 https://www.devze.com 2023-04-11 19:22 出处:网络
We have built a C++ library using Boost and when we try to integrate the library in a binary we are having problem in linking because of “--instances=explicit” option used for building the binary.

We have built a C++ library using Boost and when we try to integrate the library in a binary we are having problem in linking because of “--instances=explicit” option used for building the binary.

The problem is, when we use --instances=explicit option (of Sun CC compiler) it expects us to instantiate the templates explicitly. As Boost internally uses templates when we use Boost with this option the linker is throwing “Undefined symbol” errors - which is expected as we are not instantiating the templates used in Boost explicitly.

Though I understand why the linker is throwing the error here, it looks tricky to instantiate all boost templates explicitly in the code. So, want to check if there any solution/workaround to use Boost with “-instances=explicit” option of Sun CC compiler.

Any help is much appreciated.

We can reproduce the problem with simple sample code as -

#include <iostream>
#include <boost/lexical_cast.hpp>
int main()
{
    std::cout<<boost::lexical_cast<int>(“8”)<<std::endl;
}

With “--instances=explicit” option

Undefined                       first referenced
symbol           开发者_运维技巧                  in file
__type_0 boost::detail::lexical_cast<unsigned,std::string,false,char>(__type_0,__type_3*,unsigned)     lexical.o
ld: fatal: Symbol referencing errors. No output written to a.out

Without “--instances=explicit” option this works fine.


You can not combine explicit template instantiation and advanced template programming. Don't even try!

Historically, other languages, such as ADA, had templates before C++, but they used explicit template instantiation. C++ was the first mainstream language to use implicit template instantiation. It was this change (from explicit to implicit instantiation) that made modern template programming techniques possible. Without implicit template instantiation there would have been no STL and no Boost.


The error message tells you which template you must explicitly instantiate (e.g. boost::detail::lexical_cast<unsigned,std::string,false,char>) It will be a lot of work, but hey, you promised the compiler to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消