Reputation: 1471
I've had a bunch of confusion today about how including libraries in c/c++ works. In some places I've red that including a file just replaces the #include statement with the code from that file and in others I've red that .lib files are generated and I need to link them.. at this point I have no clue what to think.
Lets say I have library A, library B and executable C. All separate projects but within the same solution, all have their .h and .c files, no .lib files yet, just to be clear. Library B requires Library A and executable C requires Library B to function.
What is the proper way to make this happen? The obvious one is to add the header directory of the required header to the dependent project into its "additional include directories", but then what?
Upvotes: 0
Views: 721
Reputation: 4804
As you say the first thing to do is set up the include directories of each project so each can see the .h header files it needs. The header files just contain code that declares the names and parameters of functions (and exported variables) contained within the libraries. This lets you write code that refers to these functions, and it will compile.
Now you need to help out the linker by telling it where to find the actual function implementations.
2.Do I mark it as dependent in solution dependency manager?
You don't technically need this to make the projects link together, but you will find it helpful. Setting up the dependencies properly (i.e. project C depends on project B, and project B depends on project A) tells Visual Studio what order to build the projects in when you hit "build all". Dependencies also tell it what projects need to be rebuilt when changes are made. E.g. if you change something in project B and rebuild, it knows it also needs to rebuild project C, but not project A.
3.Do I add the hypothetical .lib files and the directories they would appear in to linker settings?
Yes. The .lib files contain the actual function implementations needed to build the final executable.
4.Do I do these things to the project thats immediately dependent on those resources or is this dependency inherited in other projects that refer to this one? (so for example if B needs A and C needs B then C needs A and B)
Either. They are not actually linked together until the final executable C is linked, so you could just add the A and B libs to the C linker settings. Or, you can add lib A to the project B linker settings, and B to the C linker settings, and the linker will work out the 'inherited' requirement.
Now for the good news...
1.Do I add a reference to the required project?
You don't need to use references at all. But if you do setup references (so C has a reference to B, and B has a reference to A) then Visual Studio sorts out dependencies, and build order, and linker paths for you! So if you do (1) you don't need to worry about your questions (2), (3) or (4). Sometimes you might need the finer grained control though so it's useful to understand what is happening "under the hood".
Upvotes: 1