libraries and linking
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: libraries and linking

  1. #1
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Question libraries and linking

    Hi folks,
    I'm building a library A which calls functions in library B. A depends on B.
    I'm making an application P which calls functions in library A. That is, P depends on A. I want to arrange things so that P depends only on A, and P does not depend on B. How do I arrange this?
    This is C++ on Visual Studio 2008. I get the impression that when I build A, devstudio ignores the setting in librarian - general - link library dependencies and copies anything it can into A from other libraries. What is meant to happen?

    w.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: libraries and linking

    If you want P not to depend on B you have to copy the code from B into A but why is the dependency chain an issue at all?

    No VS doesn't copy anything from one library to another. Why do you think it does that?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: libraries and linking

    Quote Originally Posted by S_M_A View Post
    No VS doesn't copy anything from one library to another. Why do you think it does that?
    i) if I tell project A where B's library files are, it produces an A.lib file which is bigger
    ii) if I build app P against A, it will only do so successfully if project A knows about B's library files.

    These two points clearly demonstrate that project A is copying B's code and putting it into A.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: libraries and linking

    Quote Originally Posted by c94wjpn View Post
    i) if I tell project A where B's library files are, it produces an A.lib file which is bigger
    ii) if I build app P against A, it will only do so successfully if project A knows about B's library files.

    These two points clearly demonstrate that project A is copying B's code and putting it into A.
    In the end, the linker is going to need all the code necessary to build the final executable. You either pay for it now by making A bigger by including B , or pay for it later by specifying A and B when you build your application.

    As to your question, either

    1) make sure that modules in A do not call any modules in B, or

    2) take the source code from B, add it to the project that builds A, and then you only have A to build.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: libraries and linking

    Quote Originally Posted by Paul McKenzie View Post
    In the end, the linker is going to need all the code necessary to build the final executable. You either pay for it now by making A bigger by including B , or pay for it later by specifying A and B when you build your application.

    As to your question, either

    1) make sure that modules in A do not call any modules in B, or

    2) take the source code from B, add it to the project that builds A, and then you only have A to build.

    Regards,

    Paul McKenzie
    indeed, Paul, but you're not answering my questions. (1) is not going to happen. (2) is not viable either.
    Last edited by c94wjpn; March 29th, 2012 at 11:29 AM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,434

    Re: libraries and linking

    Quote Originally Posted by c94wjpn View Post
    indeed, Paul, but you're not answering my questions. (1) is not going to happen. (2) is not viable either.
    I did answer your question. That is how you get rid of B.

    Otherwise, what magic are you going to use on the linker when you call a function that resides in the B library?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: libraries and linking

    Well you didn't mention that you actually linked A source with B library but yes in that case that's what will happen.

    The "normal" way to do it is to create A and B as separate libraries and leave the linking to when P is built. "Normal" since everything depends on what you actually want to do...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: libraries and linking

    Quote Originally Posted by c94wjpn View Post
    I want to arrange things so that P depends only on A, and P does not depend on B. How do I arrange this?
    Do you mean (a) actually getting rid of the dependency between P and B or are you just asking (b) how to prevent having to include B.lib in project P (or (c) add a dependency between projects P and B) in your solution? Note that in (b) or (c) P is still dependent on B.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  9. #9
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Re: libraries and linking

    Quote Originally Posted by D_Drmmr View Post
    Do you mean (a) actually getting rid of the dependency between P and B or are you just asking (b) how to prevent having to include B.lib in project P (or (c) add a dependency between projects P and B) in your solution? Note that in (b) or (c) P is still dependent on B.
    b is what I'm aiming for. I seem to have achieved this, but I'd like confirmation that I'm doing it the right way: how do I build A.lib so as it copies everything it needs from B?

    Further, will I get linker errors if someone (not me) then builds P against A.lib and B.lib?

  10. #10
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: libraries and linking

    Quote Originally Posted by c94wjpn View Post
    how do I build A.lib so as it copies everything it needs from B?
    Don't know. Never understood the reason for that magic copies.
    Further, will I get linker errors if someone (not me) then builds P against A.lib and B.lib?
    Yes. some functions will be in lib A and also in lib B and the linker won't be able to decide what to use.
    Kurt

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: libraries and linking

    Please see the simplistic sample I made. It presumably imitates your case. Try to analyze two ways of building A and see the difference in A internals as well as resultant .exe dependencies on libs.
    Attached Files Attached Files
    Best regards,
    Igor

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: libraries and linking

    So, no comments, no questions? Seems I need to add some more to my last reply.

    i) if I tell project A where B's library files are, it produces an A.lib file which is bigger
    This means that librarian extracts all required objects from B and archives them to A.
    ii) if I build app P against A, it will only do so successfully if project A knows about B's library files.
    Of course it will. Remember, A depends on B, and when building P all the external dependencies, direct or indirect, need to be resolved.

    There are two situations possible with P, A and B successful build:

    Prerequisite: Library A functions require some functions from library B.

    1. A is built extracting required functions from library B. Executable P depends only on functions from library A (as the one already includes all necessary functions from B). Once A is built this way, P never needs B to be included to the project. Please note, in this case A needs B to be included in A project.

    2. Library A is build without including B in A project, so it contains references to B functions unresolved. In this case when linking P, functions from A still need the functions from B to be resolved, and this results in extra dependency on B for P. Please note, in this case P needs B to be included in P project.

    In first case rebuilding A will need path to B to be known in A project. Rebuilding P will depend only on path to A.
    In second case rebuilding P will need both A and B path to be known in P project.

    In both cases some indirect or direct dependency on B exists. Just because A functions depend on B functions. The only way to eliminate dependency on B is moving B source code to A. This is what Paul already mentioned before.

    These two points clearly demonstrate that project A is copying B's code and putting it into A.
    Well, rephrasing you, object code from B appears archived to library A. Yes, that's right.

    I want to arrange things so that P depends only on A, and P does not depend on B. How do I arrange this?
    You already did. P depends only on A. If you never rebuild A, P is going to build with no problem ever.
    Best regards,
    Igor

  13. #13
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Thumbs down Re: libraries and linking

    Quote Originally Posted by Igor Vartanov View Post
    So, no comments, no questions? Seems I need to add some more to my last reply.

    This means that librarian extracts all required objects from B and archives them to A.
    Of course it will. Remember, A depends on B, and when building P all the external dependencies, direct or indirect, need to be resolved.
    you miss the point of my statement "if I build app P against A, it will only do so successfully if project A knows about B's library files." To make it clearer for you, perhaps I should state it thus: "I build app P against A and only A. P builds successfully if A knows about B. It does not build if A does not know about B."

    Further, no-one has commented yet on the purpose of the "link library dependencies" option.
    Last edited by c94wjpn; April 3rd, 2012 at 03:28 AM.

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: libraries and linking

    Quote Originally Posted by c94wjpn View Post
    you miss the point of my statement "if I build app P against A, it will only do so successfully if project A knows about B's library files." To make it clearer for you, perhaps I should state it thus: "I build app P against A and only A. P builds successfully if A knows about B. It does not build if A does not know about B."
    No I do not miss anything. In case your library A does not archive required objects from B, it is naturally for P build to fail. Please re-read my comments carefully.
    Best regards,
    Igor

  15. #15
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Talking Re: libraries and linking

    Quote Originally Posted by Igor Vartanov View Post
    No I do not miss anything. In case your library A does not archive required objects from B, it is naturally for P build to fail. Please re-read my comments carefully.
    ... you missed the bit that said

    Further, no-one has commented yet on the purpose of the "link library dependencies" option.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center