none
Is it possible to statically link libraries from another solution? RRS feed

  • Question

  • Hi,

    Under one VS solution, we can have multiple VS projects.  Some projects can export C++ classes for other projects to use.  These projects can be set to depend on one another and can be statically linked.  If I move some of these projects to another solution, is it possible to maintain the same dependencies among them?  I can set the two solutions' output directory the same so that all dlls will be built into the same location.  At the run time, the dlls can remain at the same directory.

    Your help is much appreciated.

    JD

    Thursday, September 19, 2019 6:02 AM

Answers

  • Hello,

    if I understand you correctly, you have a lib1 which calls functions or uses classes from another lib2. Now you want to add the lib1 solution to another project, but you don't want to add the lib2 solution into this project. This is possible. You only must tell lib1 where it can find the lib2-file in the include directories of your project settings.

    But what do you mean with dlls? You ask about static libraries, which are lib files. Dlls cannot be statically linked, they are "dynamic link libraries". If you have dlls too, then you must set the include directories to the dlls too.

    Have a look at your project settings.

    Regards, Guido

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:02 PM
    Thursday, September 19, 2019 6:57 AM
  • These projects can be set to depend on one another and can be statically linked. If I move some of these projects to another solution, is it possible to maintain the same dependencies among them? I can set the two solutions' output directory the same so that all dlls will be built into the same location. At the run time, the dlls can remain at the same directory.

    A little food for thought here. If you look in the project's .vcxproj file, there is no reference to what solution it is part of. What's more, a project does not have to be in a subdirectory of a solution. So have you tried adding the project that you want to use to multiple solutions?

    Things like the output directory macro are based upon the solution director after all, so if you change the solution then you don't need to change the project's output directory.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:02 PM
    Thursday, September 19, 2019 7:40 AM
  • Hi,

    >> How about the third item?  What is that for? 

    The third item to add the import library to your project.

    I suggest you could refer to the link: https://docs.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019#consuming-static-libraries

    Best Regards,

    Jeanine Zhang

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 9:16 AM
    Moderator
  • Hi RLWA32,

    Thanks for the reminding.  Will do.

    JD

    • Marked as answer by jdyng77 Thursday, September 19, 2019 5:59 PM
    Thursday, September 19, 2019 5:59 PM

All replies

  • Hello,

    if I understand you correctly, you have a lib1 which calls functions or uses classes from another lib2. Now you want to add the lib1 solution to another project, but you don't want to add the lib2 solution into this project. This is possible. You only must tell lib1 where it can find the lib2-file in the include directories of your project settings.

    But what do you mean with dlls? You ask about static libraries, which are lib files. Dlls cannot be statically linked, they are "dynamic link libraries". If you have dlls too, then you must set the include directories to the dlls too.

    Have a look at your project settings.

    Regards, Guido

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:02 PM
    Thursday, September 19, 2019 6:57 AM
  • Because import libraries have the .lib extension the same as static libraries, people get confused over static linking and compile time dynamic linking.

    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, September 19, 2019 7:35 AM
  • These projects can be set to depend on one another and can be statically linked. If I move some of these projects to another solution, is it possible to maintain the same dependencies among them? I can set the two solutions' output directory the same so that all dlls will be built into the same location. At the run time, the dlls can remain at the same directory.

    A little food for thought here. If you look in the project's .vcxproj file, there is no reference to what solution it is part of. What's more, a project does not have to be in a subdirectory of a solution. So have you tried adding the project that you want to use to multiple solutions?

    Things like the output directory macro are based upon the solution director after all, so if you change the solution then you don't need to change the project's output directory.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:02 PM
    Thursday, September 19, 2019 7:40 AM
  • Hi,

    Thank you for posting here.

    >>Is it possible to statically link libraries from another solution?

    As far as I'm concerned, you could statically link libraries from another solution.You just need to make sure the project can find the .lib files and header files it needs.

    1,Add the path to the header file to the Additional Include Directories(property ->c/c++ -> General -> Additional Include Directories)
    2,Add an external library search path to the Additional Library Directories (property -> linker -> General -> Additional Library Directories)
    3,Add an external dependency library to Additional Dependencies (property -> linker -> input -> Additional Dependencies)

    Best Regards,

    Jeanine Zhang

    Thursday, September 19, 2019 8:07 AM
    Moderator
  • I am sorry that I confused everybody.  Yes, I meant to say dll (dynamic linked library).  Say I have a VS solution (sln, say sln1) which has a VS project (vcproj, say prj1) that produces a dll.  The dll exposes/exports a C++ class.  I have a second solution (say sln2) which has another project (say prj2).  My question is whether prj2 can import the C++ class that prj1 exports.  Based on Jeanine's suggestion, this scenario is possible as long as I make prj2 to depend on prj1 in terms of .h and .lib.  Hi Jeanine, I suppose that your second item is for .lib.  How about the third item?  What is that for?  Thanks for your further assistance.

    JD

    Thursday, September 19, 2019 8:52 AM
  • Hi,

    >> How about the third item?  What is that for? 

    The third item to add the import library to your project.

    I suggest you could refer to the link: https://docs.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019#consuming-static-libraries

    Best Regards,

    Jeanine Zhang

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 9:16 AM
    Moderator
  • After you edit the project properties for prj2 so that the VS can find the prj1 .h and .lib files during the build process for prj2 you still need to ensure that the prj1 dll is available to the prj2 executable at runtime.  So don't forget to copy the prj1 dll to the output directory of the solution that contains the prj2 project.


    • Edited by RLWA32 Thursday, September 19, 2019 9:29 AM
    Thursday, September 19, 2019 9:28 AM
  • Hi RLWA32,

    Thanks for the reminding.  Will do.

    JD

    • Marked as answer by jdyng77 Thursday, September 19, 2019 5:59 PM
    Thursday, September 19, 2019 5:59 PM
  • Hi Jeanine,

    Thanks for your information, plus the helpful link.

    JD

    • Marked as answer by jdyng77 Thursday, September 19, 2019 6:01 PM
    • Unmarked as answer by jdyng77 Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 6:00 PM