Eclipse CDT:如何以编程方式(永久)保存项目范围的环境变量

发布于 2024-11-10 15:19:47 字数 623 浏览 3 评论 0 原文

我能够以编程方式创建项目范围的环境变量到项目属性 -> C/C++ 构建 ->环境页面。但是,当我重新启动工作区时,环境变量消失了。

我用来添加新的项目范围环境变量的代码位于:

全局变量:

private ICConfigurationDescription cfgd = null;
private final MultiCfgContributedEnvironment ce = new MultiCfgContributedEnvironment();

内部方法:

ICConfigurationDescription[] cfgs;
    cfgs = new ICConfigurationDescription[] {cfgd};
    for (ICConfigurationDescription cfg : cfgs) { 
        ce.addVariable("PKG_CONFIG_LIBDIR", dir, 
        EnvironmentVariable.ENVVAR_APPEND, SEPARATOR, cfg);
    }

我正在寻找一种在工作区重新启动后将环境变量保留在“环境页面”上的方法。

I am able to create project-wide environment variables programmatically to Project properties -> C/C++ Build -> Environment page. However when I restart the workspace the environment variables are vanished.

The code I use to add new project-wide environment variables is here:

Global variables:

private ICConfigurationDescription cfgd = null;
private final MultiCfgContributedEnvironment ce = new MultiCfgContributedEnvironment();

Inside method:

ICConfigurationDescription[] cfgs;
    cfgs = new ICConfigurationDescription[] {cfgd};
    for (ICConfigurationDescription cfg : cfgs) { 
        ce.addVariable("PKG_CONFIG_LIBDIR", dir, 
        EnvironmentVariable.ENVVAR_APPEND, SEPARATOR, cfg);
    }

I am looking for a way to keep the environment variables on the 'Environment page' after the workspace is restarted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2024-11-17 15:19:47

另一种不使用内部类/API 的解决方案是:

IContributedEnvironment environment = CCorePlugin.getDefault().getBuildEnvironmentManager().getContributedEnvironment();
ICProjectDescription projectDescription = CoreModel.getDefault().getProjectDescription(project, true);
ICConfigurationDescription config = projectDescription.getActiveConfiguration(); // or any other configuration...

// Add variable to project configuration
environment.addVariable("PKG_CONFIG_LIBDIR", dir, IEnvironmentVariable.ENVVAR_APPEND, null, config);

// Update project (description)
CoreModel.getDefault().setProjectDescription(project, projectDescription);

请注意,使用 null 而不是 config 将变量添加到全局/工作空间环境中。

另请注意 IEnvironmentVariable 中定义的不同“操作”。

Another solution, without using the internal classes/APIs, is:

IContributedEnvironment environment = CCorePlugin.getDefault().getBuildEnvironmentManager().getContributedEnvironment();
ICProjectDescription projectDescription = CoreModel.getDefault().getProjectDescription(project, true);
ICConfigurationDescription config = projectDescription.getActiveConfiguration(); // or any other configuration...

// Add variable to project configuration
environment.addVariable("PKG_CONFIG_LIBDIR", dir, IEnvironmentVariable.ENVVAR_APPEND, null, config);

// Update project (description)
CoreModel.getDefault().setProjectDescription(project, projectDescription);

Note that using null instead of config adds the variable to the global/workspace environment.

Also note the different "operation", as defined in IEnvironmentVariable.

好多鱼好多余 2024-11-17 15:19:47

我解决了我自己的问题。如果其他人也遇到同样的问题,我会在此处包含代码。

解决方案是使用存储 env 的 StorableEnvironment。变量到 XML。

UserDefinedEnvironmentSupplier fUserSupplier = EnvironmentVariableManager.fUserSupplier;
StorableEnvironment vars = fUserSupplier.getWorkspaceEnvironmentCopy();
vars.createVariable("PKG_CONFIG_LIBDIR", dir);
fUserSupplier.setWorkspaceEnvironment(vars);

请注意,它们

org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
org.eclipse.cdt.internal.core.envvar.UserDefinedEnvironmentSupplier;

是内部 API 类,因此不建议使用它们,因为它们的实现可能会更改并影响代码的功能。

I solved my own question. I include the code here if someone else has the same problem.

The solution is to use StorableEnvironment which stores env. vars to XML.

UserDefinedEnvironmentSupplier fUserSupplier = EnvironmentVariableManager.fUserSupplier;
StorableEnvironment vars = fUserSupplier.getWorkspaceEnvironmentCopy();
vars.createVariable("PKG_CONFIG_LIBDIR", dir);
fUserSupplier.setWorkspaceEnvironment(vars);

Be aware though that

org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
org.eclipse.cdt.internal.core.envvar.UserDefinedEnvironmentSupplier;

Are internal API classes and therefore their use is not recommended because their implementation could change and affect the functionality of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文