简体   繁体   English

我如何确保在我的 C++ 程序中所需的头文件安装在用户的机器上?

[英]How would I make sure that the needed headers required in my C++ program are installed on the user's machine?

I have a C++ project that uses the popular Boost library.我有一个使用流行的Boost库的 C++ 项目。 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.问题是,如果有人下载我的代码 Github 并尝试构建它,除非他们安装了 Boost,否则它将无法工作,这可能会带来不便。 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谢谢

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.根据我的经验,许多平台都定义了自己的传统方式。 Eg in iOS development people either use CocoaPods or SwiftPackages (and sometimes Carthage).例如,在 iOS 开发人员使用 CocoaPods 或 SwiftPackages(有时还有 Carthage)。 For Android applications, developers usually define dependencies in dependencies block of their Gradle-build script (which essentially takes them from remote repos).对于 Android 应用程序,开发人员通常在 Gradle-build 脚本的dependencies项块中定义依赖项(本质上是从远程存储库获取它们)。

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 .就我而言,在 C++ 世界中,依赖项预计将通过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.我不会说 CMake 本身定义了管理依赖项的传统方式(这样做的方法远不止一种),但每个人都期望的部分是,当您的 CMake 脚本成功完成时,它已经设置了所有依赖项。 Here are a few options you have when managing dependencies via CMake:在通过 CMake 管理依赖项时,您可以使用以下几个选项:

  • find_package - the most widespread way. find_package - 最普遍的方式。 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).因此,此命令最终应该包含在您的 cmake 脚本中配置的包含目录和库目标(您链接自己的项目)。 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.老实说,我不是这个命令的忠实拥护者,因为它实际上取决于主机的配置方式,并且您应该编写额外的Find<Library_name>.cmake文件,您可以在其中提供有关 CMake 如何找到给定的图书馆。
  • 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.将您的依赖项作为 git 子模块添加到项目的子目录中,并在CMakeLists.txt中对这些依赖项使用add_subdirectory - 我的首选方式。 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).最后但并非最不重要的一点是,子模块保留使用的存储库的 state,因此克隆您的代码的每个人都将获得与您使用的依赖项完全相同的版本,同时他们有可能对其进行更新(将更新的版本拉为需要)。
  • Package managers (eg 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. Package 管理器(例如Conan - 我认为您已经猜到了,C++ 中的依赖管理对新手不是很友好,并且仍然有一些新的选项正在开发中来解决这个问题。 Conan is one of such options, and it's compatible with CMake (but it's not very widespread yet).柯南就是这样的选择之一,它与 CMake 兼容(但还不是很普遍)。

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.由于 CMake 的灵活性,您还可以添加任何中间步骤来轻松安装依赖项,包括但不限于自动 git 子模块拉取和更新,或安装一些缺少的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在程序退出之前确保所有c ++ boost线程完成? - How can I make sure all of my c++ boost threads finish before the program exits? 我如何使这个C ++程序使用负数? - How would i make this C++ Program work with negative Numbers? 在使用boost的program_options时,如何确保声明在C ++中具有存储类或类型说明符? - How do I make sure that a declaration has a storage class or type specifier in C++ when using boost's program_options? 验证用户输入以确保其为二进制(C ++) - Validating a user's input to make sure it's in binary (C++) C ++不确定如何让我的程序输出空间的位置 - C++ Not sure how to have my program output the location of a space 我怎样才能让我的 C++ 程序计数? - How can I make my c++ program to countinue? 如何确保我的默认 C/C++ 编译器是 GCC - How do I make sure that my default C/C++ compiler is GCC 我将如何提示用户让他们选择在 C++ 中重新运行程序 - How would I prompt the user to give them the option to rerun program in C++ 如何在 C++ 程序中使用 C 标头? - How do I use C Headers in a C++ Program? 我写了一个 C++ 程序来模拟 Enigma 机器。我没有得到输出 - I wrote a C++ program that would simulate Enigma machine.I am not getting the output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM