Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have Project 1 with dependencies to Boost and GLM. For Boost and GLM, I've specified the 'Additional Include Directories' to reference the C++ files for each. Project 1 is created as a static library project. When I build Project 1, everything builds fine. Project 2 references Project 1 via the Reference Manager, but when I build Project 2, I get

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

for files in Project 1. Why would I get errors about Project 1 when I build Project 2? Project 1 also uses the regex library in Boost, which must be built into a .lib prior to use. How can I make my Project 1 static library incorporate the built Boost regex library and GLM include files into it? FYI, Project 2 is a test project for Project 1. I'm wanting 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

--> denotes dependencies/references and ==> denotes output.

Is this possible? I've gotten more compilation errors and linker errors than I can count as I spin my wheels on this.

like image 387
Wagan8r Avatar asked Sep 02 '12 05:09

Wagan8r


2 Answers

This is probably because some of the code (header and/or implementation) in your Project 2 is including headers from Project 1, that in turn, include external library headers that are not in the include path for Project 2. The net effect is that after expanding all of the #includes, your Project 2 source file will have a line saying something like: #include <boost/something/etc> which it wont be able to expand since its not in Project 2's include search path.

This error will happen regardless of the fact that you have statically compiled those external libraries into your project1.lib.

If its not an issue, simply add the external library include paths to Project 2's VC++ directories > include directories.

One way to work around this is to move as many external library includes outside of your Project 1 headers and hide them by using a combination of the PIMPL pattern and forward declarations. But for things like header-only or template heavy libraries, I believe you will need to include those header paths and there is no way around it unless you encapsulate the functionality or hide the implementation behind a Project 1 class/interface.

like image 148
Preet Kukreti Avatar answered Sep 18 '22 16:09

Preet Kukreti


Also the answer of @PreetKukreti is correct, after you fix your headers there is still one more dependency to external libraries(boost & GLM) because by default static libraries won't link with external dependencies. This is because of a simple case of error that I will explain here:
You use a function for example strlen from CRT and you want to merge it with your .lib output, then strlen will be merged into your .lib and then in your test project(.exe) you use strlen again, and you already know that in static libraries every thing is public, so when you link against CRT and your .lib you have to implementation of strlen and this will generate a linker error.
So by default VisualStudio won't link library dependencies into .lib files unless you tell it to do that(Solution Properties->Librarian->Link Library Dependencies) and you should not set it to yes unless you really know what are you doing there and accept the consequence of your action!!.
So in any case it is better to put the path of external libraries(boost & GLM) into the path of project 2 or build project 1 as a DLL that only expose certain indicated objects and also try to use the answer from @PreetKukreti and move your unnecessary include files into implementation(.cpp) files.

like image 38
BigBoss Avatar answered Sep 19 '22 16:09

BigBoss