How to force inclusion of an object file in a static library when linking into executable?

前端 未结 6 766
春和景丽
春和景丽 2020-12-02 23:10

I have a C++ project that due to its directory structure is set up as a static library A, which is linked into shared library B, which is linked in

6条回答
  •  猫巷女王i
    2020-12-02 23:49

    If you can use C++0x features of gcc (-std=c++0x), then the function default template arguments may do the trick. As of the current c++ standard, default arguments are not allowed for function templates. With these enabled in c++0x, you can do something like :-

    In some header file of static library ...

    template< class T = int >
    void Af()
    {
    }
    

    Then in its corresponding cpp file use explicit template instantiation...

    template void Af();
    

    This will generate the symbols for the function Af though it is not yet called/referenced. This won't affect the callers due to the fact that because of the default template argument, you need not specify a type. Just add the template before the function declaration and explicitly instantiate it in its implementation file.

    HTH,

提交回复
热议问题