Where Should One Place Third Party Libraries?

2.7k views Asked by At

This may seem a bit ridiculous to ask, but I'm struggling to find a good system for this. Are there any standardized systems for storing and organizing third party development libraries? I'm plagued with OCD and consider myself to be a "neat-freak," so I tend to use tons of directories/subdirectories for organizing items, but since I'm a newer developer, I just recently started dabbling with 3rd party libraries and I'm not sure how to go about organizing them. I would use a ton of folders and make a huge hierarchy, but doing that may result in some extremely long absolute paths in the #includes of my source code. Any suggestions?

2

There are 2 answers

1
Cornelis de Mooij On BEST ANSWER

Third party libraries can go anywhere you want, as long as you make sure that both the compiler and the compiled program can find the files that they need. So you can go wild with organizing your files in whatever you want, as long as you make sure that you tell this to the compiler and the compiled program.

Exactly how to do this depends on the IDE (integrated development environment) that you are using. I use visual studio, so the exact terminology might be different if you use something else. In visual studio, you have to go to the properties of your project to do this.

For the header files of your external libraries, go to Configuration Properties > C/C++ > General and click on "Additional Include Directories". If you edit the value of this field, you can add the paths to the include directory of your external library, which should hold all the header files of the external library. By using the Macros that Visual Studio provides, you can make these paths relative, so that you don't have to do this all over whenever you move your project. Make sure you don't remove "%(AdditionalIncludeDirectories)". Also make sure that the selected configuration and platform at the top of the window match the configuration and platform for which you are trying to compile. Using the macros you can set this up for all the configurations and platforms simultaneously, which is a bit harder but it will save you time in the long run. An example from one of my projects:

$(SolutionDir)dependencies\SDL2_image-2.0.1\include;$(SolutionDir)dependencies\SDL2-2.0.5\include;%(AdditionalIncludeDirectories)

For the library files, you need to go to Configuration Properties > Linker > General and click on "Additional Library Directories". Edit this value to add the paths to the lib folders of your external libraries. You can again use macros here.

$(SolutionDir)dependencies\SDL2-2.0.5\lib\x64;$(SolutionDir)dependencies\SDL2_image-2.0.1\lib\x64;%(AdditionalLibraryDirectories)

Next, go to Configuration Properties > Linker > Input and edit "Additional Dependencies" to add the names of the .lib files that you need. Just the filenames this time, you don't need the path here. An example from one of my projects:

SDL2main.lib;SDL2.lib;SDL2_image.lib;%(AdditionalDependencies)

Finally, you need to make sure that your compiled program can find the .dll files of your external libraries. For this you can mess around with system variables like PATH and so on, but I don't recommend that. I prefer to use a Post-Build Event. An event like this is basically a sequence of command line commands that are carried out after your program has been compiled. You can add this event by going to Configuration Properties > Build Events > Post-Build Event and editing "Command Line". An example of what you can put here, from one of my projects, is shown below:

copy /Y "$(SolutionDir)dependencies\SDL2_image-2.0.1\lib\$(PlatformTarget)\*.dll" "$(TargetDir)*.dll"
copy /Y "$(SolutionDir)dependencies\SDL2-2.0.5\lib\$(PlatformTarget)\SDL2.dll" "$(TargetDir)SDL2.dll"
xcopy /Y /S /E /I "$(SolutionDir)assets" "$(TargetDir)assets"

Note that I use macros again: $(SolutionDir), $(PlatformTarget), $(TargetDir) are replaced by the solution directory, platform target and the target directory respectively for each combination of Configuration and Platform.

0
Maciej Chałapuk On

You have 2 options:

  1. Use system-installed libraries (and headers), tell compiler where to find them.
  2. Use a folder in your project (use folder ./external to store external sources, e.g. ./external/boost-asio for Boost ASIO library), download sources when initializing the project (I prefer to use git submodules to download external sources), and build them with your project.

CMAKE is a build tool which can help you in achieving both.

Either way, as jtbandes wrote, don't use absolute paths.