开发者

Remove module of library from linking

I have made lib in Visual Studio. Lib have two modules: A.cpp and B.cpp. In separate project i link to this library but i want to disa开发者_开发百科ble one of modules of this lib i.e B.cpp.


You may not have to split up your lib.

The necessary contents of lib is only linked in if there are unresolved symbols remaining from your executable (exe/dll). Unnecessary contents is generally speaking not linked in.

For example:

suppose your lib contains 2 obj-files ,a.obj from a.cpp and b.obj from b.cpp.

a.cpp contains among others the function void a( int i ) which calls function void b( int j ).

b.cpp contains the function void b( int j ).

And suppose your executable contains in one of its source-files an implementation of a function

void b( int k )

and in some other piece of code you have a call to void a( int ).

When you link against the lib then linker at one stage is left with 1 unresolved symbol (that of the call to function void a( int ). It will search the libraries that you have specified for the first occurence of such a function-signature and it link in that part of that lib. Since you executable code already contains an implementation of void b( int ) it doesn't need to search any lib to resolve the call of it in a.obj ,so b.obj will not be linked in.

In summary (very simplified):

as long as you don't directly or indirectly call\use anything from b.obj ,it will not be linked in (unless you specify a lib as OBJ=YourLib.lib in your link cmd ,which pulls in the total library).

If you do directly or indirectly call\use something implemted in b.obj and you implement those symbols (functions or global variables) in your executable then either those parts of b.obj will not be linked in (in case of function-level linking specified) or you will get duplicate symbol link-errors if you forget something.


Split the project into two, one of which contains A.cpp and the other containing B.cpp. Then if you need to link to both .cpp files you have to link to two .lib files. But if you want to link to just one, then you can do so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜