Home > other >  How would I make sure that the needed headers required in my C program are installed on the user&#
How would I make sure that the needed headers required in my C program are installed on the user&#

Time:07-25

I have a C project that uses the popular Boost library. The problem is, if someone downloads my code off say, Github and tries to build it, it won't work unless they have Boost installed, which could be an inconvenience. I'm just wondering how I would go about making sure these headers are accessible for the user so they can build the app themselves. Would I have to copy them into a separate folder in the directory? Are there better solutions?

Thanks!

CodePudding user response:

There are quite a lot of options on how to manage dependencies. In my experience many platforms define it's own conventional way of doing so. E.g. in iOS development people either use CocoaPods or SwiftPackages (and sometimes Carthage). For Android applications, developers usually define dependencies in dependencies block of their Gradle-build script (which essentially takes them from remote repos).

There isn't necessary an industry standard, but there is something other developer usually find consistent.

As far as I'm concerned, in C world the dependencies are expected to be managed via CMake. I wouldn't say that CMake itself defines conventional way of managing dependencies (there is far more than one approach of doing so), but the part which everyone expects is that when your CMake script finishes successfully, it has all dependencies set up. Here are a few options you have when managing dependencies via CMake:

  • find_package - the most widespread way. As a result, this command should end up with include dirs and a library target configured in your cmake script (against which you link your own project). I'm not a big fan of this command to be honest, because it really depends on how the host machine is configured, and you are supposed to write additional Find<Library_name>.cmake files where you give instructions for CMake how to find the given library.
  • Add your dependencies as git submodules in subdirectories of your project and use add_subdirectory on those dependencies in you CMakeLists.txt - my preferable way. First, I like it because it's highly portable, you don't expect the host machine to be configured in any specific way, second, it doesn't spread dependencies beyond the project directory. Last, but not least, the submodules keep state of the repository used, so everyone who clone your code will get exactly the same version of the dependency you use and at the same time they have a possibility to update it (pull more recent version as needed).
  • Package managers (e.g. Conan) - you have already guessed, I think, that dependency management in C is not very novice-friendly and there are still some new options being developed to address this issue. Conan is one of such options, and it's compatible with CMake (but it's not very widespread yet).

Thanks to flexibility of CMake, you also can add any intermediate steps to easy the installation of dependencies, including, but not limited to, automatic git submodules pull and update, or installing some missing tools.

  • Related