开发者

Three levels of project dependencies cause not everything to be linked in VC++ 2008

开发者 https://www.devze.com 2023-02-25 10:26 出处:网络
1) Solution contains two projects. Project 2 depends on project 1(checked in \"Project Dependencies\" checkbox group).

1) Solution contains two projects. Project 2 depends on project 1(checked in "Project Dependencies" checkbox group). A part of classes in project 1 are declared and implemented but never used in code (any instance is created). To use these class in lua they are bound using luabind. Binding function is declared as static method for each class which has to be bound. It is called automatically using c开发者_如何学Pythonode in cpp and uses something like "const bool is_bound = ClassName::Bind()". Buld results are: static lib for project 1 and executable for project 2.

All the classes are bound and available in scripts.

2) I've added project 3, dependent on project 2. Buld result of project 2 is static lib and build result of project 3 is executable. As a result - all or part of classes, defined in project 1 aren't being linked to binaries at all. The problem - after launching the program they aren't bound ant can't be used in scripts.

How could this problem be solved and what's the reason causing it? Thanks in advance.

P.S. I tried to solve it manually using forced linking by "imitation of using" (as an experiment). I used functions like this (I know, its totally ugly)

template <class T, class A1, class A2>
void ForceLinking()
{
   boost::function<void(A1, A2)> f =
       boost::bind<T>(boost::lambda::constructor<T>(), _1, _2);
}
ForceLinking<ClassName, const string&, bool>();

As a result - the part started to be linked and the other part not.


When Visual Studio links an EXE or DLL, it tends to include every static object in the main project and every exported function of the main project. It then traces dependencies in that project and any added libraries.

It doesn't grab static objects in the referenced libraries, and this probably should be called a bug. To get past this, we must explicitly reference the desired objects in the main project. If fact, you only need to reference the object files and the linker will take care of the rest.

I ran into this issue with Google Test, and the solution I learned is like this: In each affected source file of the lib project, add:

int LinkFileName() { return 0; }

In any file of the DLL or EXE project, add:

int linkFileName = LinkFileName();

It's rather ridiculous, but it works.

0

精彩评论

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

关注公众号