简体   繁体   English

当涉及路径时,如何编写与系统无关的代码?

[英]How do I write system-independent code when there are paths involved?

Say I am creating a project that uses a certain library and I have to provide the path for that library while linking. 假设我正在创建一个使用某个库的项目,我必须在链接时提供该库的路径。 In the command line or makefile I might have: 在命令行或makefile中我可能有:

g++ ... -L/path/to/mylibrary

I'm also going to send this project to someone else who wants to use it. 我也将把这个项目发送给想要使用它的其他人。 The path on their system might not necessarily be the same as mine. 他们系统上的路径可能不一定与我的相同。 They could be using a different file path all together. 他们可能一起使用不同的文件路径。

How do I make sure that the path to the library works for both my computer and the recipient of my project? 如何确保库的路径适用于我的计算机和项目的收件人?

This is the role of a build system or build configuration tool. 这是构建系统或构建配置工具的角色。 There are many of those around. 周围有许多人。 The main one is probably CMake as it has a very extensive feature set, cross-platform, and widely adopted. 主要的可能是CMake,因为它具有非常广泛的功能集,跨平台,并被广泛采用。 There are others like Boost.Jam , autoconf , and others. 还有其他像Boost.Jamautoconf等。

The way that these tools will be used is that they have automated scripts for looking into the file-system and finding the headers or libraries that you need, ie, the dependencies required to compile your code. 这些工具的使用方式是它们具有自动脚本,用于查看文件系统并查找所需的头文件或库,即编译代码所需的依赖项。 They can also be used to do all sorts of other fancy things, like checking what features the OS supports and reconfiguring the build as a consequence of that. 它们还可以用于执行各种其他奇特的操作,例如检查操作系统支持的功能以及重新配置构建的结果。 But the point is, you don't hard-code any file-paths into the build configuration, everything is either relative to your source folder or it is found automatically by the build script. 但问题是,您没有将任何文件路径硬编码到构建配置中,所有内容都与源文件夹相关,或者由构建脚本自动找到。

Here is an example CMake file for a project that uses Boost: 以下是使用Boost的项目的示例CMake文件:

cmake_minimum_required (VERSION 2.8)
project (ExampleWithBoost)

find_package(Boost 1.46 COMPONENTS thread program_options filesystem REQUIRED)

# Add the boost directory to the include paths:
include_directories(SYSTEM ${Boost_INCLUDE_DIR})
# Add the boost library directory to the link paths:
link_directories(${Boost_LIBRARY_DIRS})

# Add an executable target (for compilation):
add_executable(example_with_boost example_with_boost.cpp)
# Add boost libraries to the linking on the target:
target_link_libraries(example_with_boost ${Boost_LIBRARIES})

The find_package cmake function is simply a special script (specialized for Boost, and installed with CMake) that finds the latest version of boost (with some minimal version) installed on the system, and it does so based on the file-name patterns that the library uses. find_package cmake函数只是一个特殊的脚本(专门用于Boost,并与CMake一起安装),它可以在系统上安装最新版本的boost(带有一些最小版本),并且它基于文件名模式来实现。图书馆使用。 You can also write your own equivalents of find_package , or even your own package finders, using the functions that CMake provides for searching the file system for certain file-name patterns (eg, regular expressions). 您还可以使用CMake为搜索文件系统提供的某些文件名模式(例如,正则表达式)提供的函数编写自己的find_package等效find_package ,甚至是您自己的包查找程序。

As you see, the build configuration file above only refer directly to your source files, like "example_with_boost.cpp", and it's only relative to the source folder. 如您所见,上面的构建配置文件仅直接引用您的源文件,例如“example_with_boost.cpp”,并且它仅与源文件夹相关。 If you do things right, the configuration scripts will work on virtually any system and any OS that CMake supports (and that the libraries you depend on support). 如果您做得对,配置脚本几乎可以在任何系统和CMake支持的任何操作系统(以及您所依赖的库支持)上运行。 This is how most major cross-platform projects work, and when you understand how to work with these systems, it's very powerful and very easy to use (in general, far easier to use and trouble-free than build configurations that you do by point-and-click within IDE menus like in Visual Studio). 这就是大多数主要跨平台项目的工作方式,当您了解如何使用这些系统时,它非常强大且易于使用(通常,比使用点构建的配置更容易使用和无故障 - 并在IDE菜单中单击,如在Visual Studio中)。

You can use premake that generates cross platform makefiles: Visual Studio, Gcc and others 您可以使用生成跨平台makefile的premake:Visual Studio,Gcc等

http://industriousone.com/what-premake http://industriousone.com/what-premake

CMake is another alternative CMake是另一种选择

http://www.cmake.org/ http://www.cmake.org/

I'm not sure if there's a single universal way of doing this, but people often provide different config files and let the main Makefile detect which one to include: linux.make , darwin.make , cygwin.make etc. 我不确定是否有一种通用的方法可以做到这一点,但人们经常提供不同的配置文件,让主​​Makefile检测哪一个包括: linux.makedarwin.makecygwin.make等。

there are also various tools like CMake that allow to automate this, but all in all it's just scripting that hides the system-dependency from the developer. 还有像CMake这样的各种工具允许自动执行此操作,但总而言之,它只是隐藏了开发人员对系统的依赖性的脚本。

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

相关问题 动态C ++编译器,用于与系统无关的程序分发 - On-the-fly C++ compiler for system-independent program distribution 如何编写与编译器无关的通用代码? - How to write generic compiler-independent code? 当涉及多种编码语言时,如何将我的应用程序连接到蓝牙? - How do I connect my app to bluetooth when multiple coding languages are involved? 如果 copts 不允许系统路径,如何引用外部依赖项使用的系统库? - How do I reference a system library used by an external dependency if copts doesn't allow system paths? 我应该如何进行深度独立混合? - How should I do depth independent blending? 当我为 linux 和 windows 编写代码时,如何正确构建我的文件 - How do is structure my files right, when I write code for linux and windows 如何独立于主渲染循环逐步执行算法(出现提示时)? - How do I step through an algorithm (when prompted) independent of the main render loop? 删除多个(虚拟)继承中涉及的对象时,为什么会出现无效的块崩溃? - Why do I get an invalid block crash when deleting objects involved in multiple (virtual) inheritance? 您如何“刷新” write()系统调用? - How do you “flush” the write() system call? C++:在比较 boost::filesystem 中的路径时如何忽略第一个目录路径? - C++: How do I ignore the first directory path when comparing paths in boost::filesystem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM