简体   繁体   English

在编译时为运行时设置环境变量

[英]Setting environment variables for run-time in compile-time

I have a C++ Vulkan program that needs multiples libraries to be available at run-time. 我有一个C ++ Vulkan程序,该程序需要多个库才能在运行时可用。 Also, Vulkan has a feature called "Validation Layers" which is configured with a config file. 另外,Vulkan具有称为“验证层”的功能,该功能通过配置文件进行配置。

In run-time my program needs to know where those libraries are and where that config file is. 在运行时,我的程序需要知道这些库在哪里以及该配置文件在哪里。 I'm guessing there's no way of doing it programmatically, but if there is let me know. 我猜测无法以编程方式进行,但是如果有,请告诉我。 To workaround this I set environment variables, namely LD_LIBRARY_PATH (for it to find the libraries) and VK_LAYER_PATH (for it to find Vulkan's Validation Layer config file). 要解决此问题,我设置了环境变量,即LD_LIBRARY_PATH(用于查找库)和VK_LAYER_PATH(用于查找Vulkan的Validation Layer配置文件)。

This works, but I want a better way to do this, because this doesn't allow me to simply double-click the file and run it. 这可行,但是我想要一种更好的方法,因为这不允许我简单地双击文件并运行它。 I must first set the env vars, which is bad if I'm deploying the program. 我必须首先设置env vars,如果我正在部署程序,这是很糟糕的。

My question is: is there a compiler/linker option to do this? 我的问题是:是否有编译器/链接器选项可以执行此操作?

This is the workaround I'm using in my makefile: 这是我在makefile中使用的解决方法:

run:
    LD_LIBRARY_PATH=./path/to/lib1/:./path/to/lib2 VK_LAYER_PATH=./path/to/vulkan/config ./program_name

I am using Linux, g++ and make. 我正在使用Linux,g ++和make。

If you know where the libraries you need to link against will be installed you could set an rpath. 如果您知道需要链接的库将安装在何处,则可以设置rpath。 This will add the search path to the ELF header. 这会将搜索路径添加到ELF标头中。 When the dynamic linker runs it will search these locations in addition to the default locations. 当动态链接程序运行时,它将在默认位置之外搜索这些位置。

Add to your compilation line -Wl,-rpath ./path/to/lib1/ to drop lib1 from the LD_LIBRARY_PRELOAD list. 将添加到您的编译行-Wl,-rpath ./path/to/lib1/ ,-rpath -Wl,-rpath ./path/to/lib1/以从LD_LIBRARY_PRELOAD列表中删除lib1。 The -Wl is needed so the the compiler passes the flag onto the linker where it is actually recognized. -Wl是必需的,因此编译器将标志传递到链接器上,在该链接器中可以实际识别它。

This blog seems to have a good description of all the different options 这个博客似乎对所有不同的选项都有很好的描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM