The correct way of adding third-party code into a C or C++ project

cdependenciesopen sourcethird-party-libraries

I want to incorporate some open source libraries into my C project. This is the first time I do that, so I don't know what is the right or most common way of doing it.

I see two possible paths I can take:

  1. Download the code of each library I'll use, add them to my project
    folder and modify them as I need.
  2. Just list each library as a dependency. So the user would download
    them to its machine and include them to the project with (maybe)
    some makefile configuration.

I think option #1 is better for two reasons:

  1. I release the user from the task of downloading and configuring the
    dependency.
  2. In my project two libraries have overlapping code. Part of one
    library is a subset of the other. They have the same functionalities
    and the same identifiers names (variables, functions, #defines). So
    I would have some conflicts that I don't know how to fix.

But I just think option #1 is better. I really don't know what's the best. Maybe someone knows how to fix the conflicts between the libraries and then my opinion may change. I'm asking this question because I don't know what is the path programmers usually take. I also don't know what are the pros and cons of each alternative. And I also don't know if there are other alternatives.

Update: The expected users are programmers.

Best Answer

@17of26 gave a lot of good reasons why #2 is dangerous, and why #1 makes more sense. But I think there is one thing missing, for which I recommend the following:

  • download the code of the library you will use,
  • add it to the project folder,
  • but don't modify it arbitrarily!!

Avoid modifications if possible. If that is not possible, try to keep the direct modifications down to a minimum and keep everything else in separate files. If necessary, use script files or patch files. So make sure all changes to the libs or to the makefile configuration are documented and can be reproduced.

This gives you a way better chance to update to newer versions of those libs at a later point in time, if needed.

Of course, there will be no guarantee the patches or configuration changes applied to version 1.0 of a lib can be directly applied to version 2.0 of that lib, too. But the chances are the much higher when there are only a few number of patches/changes, and the effort to apply them will be likely smaller.

Related Topic