How can I inherit project dependencies / links from one project to a dependent project in Visual Studio

I have project 1 with dependencies on Boost and GLM. For Boost and GLM, I specified "Additional Include Directories" to link to C ++ files for each. Project 1 is created as a static library project. When I build Project 1, everything builds perfectly. Project 2 references Project 1 through the link manager, but when I create Project 2, I get

fatal error C1083: Cannot open include file: 'boost/something/etc.

for files in a project 1. Why do I need errors in Project 1 when creating Project 2? Project 1 also uses the regex library in Boost, which must be built into .lib before use. How can I create a Project 1 static library with a built-in Boost regular expression library and GLM files in it? FYI, Project 2 is a test project for project 1. I want something like this:

(Boost regex lib + GLM includes) -> Project 1 ==> Project_1.lib

(Boost unit test lib + Project_1.lib) -> Project 2 ==> Project_2.exe

--> stands for dependencies / links and ==> stands for output.

Is it possible? I got more compilation errors and linker errors than I can count when I spin my wheels on it.

+6
source share
2 answers

This is probably due to the fact that part of the code (title and / or implementation) in your project 2 includes headers from Project 1, which, in turn, include headers of external libraries that are not included in the inclusion path for project 2. The network effect is that after expanding all #include s in the Project 2 source file there will be a line with the inscription: #include <boost/something/etc> , which it cannot expand, since it does not include the search path in Project 2.

This error will occur regardless of the fact that you statically compiled these external libraries into your project1.lib .

If this is not a problem, just add an external library, including the paths to the V2 ++ Project 2> include directories.

One way around this is to move as much of the external library as possible outside of your Project 1 headers and hide them using a combination of PIMPL and forward declarations. But for things like libraries with a header or template, I believe that you will need to include these header paths, and there is no way around this unless you encapsulate the functionality or hide the implementation behind the Project 1 class / interface.

+5
source

Also @PreetKukreti's answer is correct, after correcting the headers there is still another dependency on external libraries (boost and GLM), because by default static libraries will not link to external dependencies. This is due to a simple error case, which I will explain here:
You use a function like strlen from CRT, and you want to combine it with your .lib output, then strlen will be combined into your .lib, and then in the test project (.exe) you use strlen again, and you already know that in static libraries every thing is publicly available, so when you contact CRT and your .lib, you need to implement strlen and this will result in a linker error.
Thus, by default, VisualStudio will not associate library dependencies with .lib files if you do not tell it about it (Solution Properties → Librarian-> Link Library Dependencies), and you should not set it to yes unless you really know what you do there and accept the consequences of your actions !!.
Therefore, in any case, it is better to put the path of external libraries (boost and GLM) in the path of project 2 or build project 1 in the form of a DLL that will output only certain specified objects, and also try to use the answer from @PreetKukreti and move your unnecessary include files to files implementations (.cpp).

+1
source

Source: https://habr.com/ru/post/924382/


All Articles