Eclipse CDT从代码中获取GCC选项 [英] Eclipse CDT get GCC options from code

查看:292
本文介绍了Eclipse CDT从代码中获取GCC选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse插件,其中包括必须检查在当前C项目中设置哪些编译器选项。
基本上,我想访问属性 - > C / C ++构建 - >设置 - > GCC C编译器 - >所有选项字段。



我已经搜索如何访问它,但我没有找到一种方法来做到这一点。
我试图通过以下代码中的首选项进行访问:

  IEclipsePreferences root = Platform.getPreferencesService() .getRootNode(); 

我可以以这种方式访问​​我的插件的首选项,但不能使用C项目。 p>

有人知道有办法吗?我不需要更改编译器选项,只是为了知道哪些标志被设置。



更新:我找到了一个解决方案。 / p>

  IResourceInfo info = getResourceInfo(translationUnit,description); 
ITool工具[] = info.getTools();
for(ITool t:tools){
if(t.getName()。compareToIgnoreCase(GCC C Compiler)== 0){
try {
//最后字段我正在寻找
String commandLine = t.getToolCommandFlagsString(getProject()。getFullPath(),null);

} catch(BuildException e){
e.printStackTrace();
}
}
}

然后我可以解析字符串,不理想,但它的作品。
我从这篇文章得到了getResourceInfo()函数:如何以编程方式更改文件的Eclipse CDT工具设置?



所以,谢谢justinmreina的答案!

解决方案

你穿过一条黑暗和孤独的路,我的朋友:)。



自定义工具链/工具上的设置选项



这里是一个例子,其中有人试图以编程方式设置GNU工具/工具链的选项:





*这里是同一作者的背景线程解决它:





他在这里做的是让你得到你的决议。我建议先浏览org.eclipse.cdt.managedbuild.gnu.ui的plugin.xml。关注工具链,工具及其选项。



查找GNU C工具链的选项/工具



此外,这里是一段有用的文章,我回到在GNU C项目中找到dang选项。不是确切的OP的问题,但答案与您的问题相关。





结论



我非常怀疑你会发现一个答案< 10行代码,甚至为编译器设置'-v'标志...如果你找到一个简单的结果,我建议在这里作为后续发布。



祝你好运!



编辑:现在我已经啃了一会儿,因为我最近绊倒/失败了。以下是如何从代码中设置选项。

  //假设
//#1 project is [0]在工作空间
//#2编译器是工作空间中的[2]

//获取项目
IProject proj = ResourcesPlugin.getWorkspace()。getRoot()。getProject(hello_world );

// get< storageModule moduleId =org.eclipse.cdt.core.settings>
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(proj);

// get< storageModule moduleId =cdtBuildSystem>
IManagedProject sub_info = info.getManagedProject();

// get< configuration name =Debug>
IConfiguration config = sub_info.getConfigurations()[0];

// get< toolChain>
IToolChain toolchain = config.getToolChain();

// get< tool name =GCC C Compiler>
ITool工具= toolchain.getTools()[2];

// get< option>
IOption选项= tool.getOptionBySuperClassId(gnu.c.compiler.option.misc.other);

// ---- append new flag ---- //
String new_opt_value = option.getValue()+-mySuperFlag;

// -----存储它// //
ManagedBuildManager.setOption(config,tool,option,new_opt_value);
ManagedBuildManager.saveBuildInfo(proj,true);

注意
- 一旦您开始查看此操作, Eclipse资源,方法变得(有点...)清除
- 每个对象调用访问一个字段只是访问.cproject资源的XML模式中的另一个部分



希望这有帮助!


I am working on an Eclipse Plugin that among other things must check which compiler options are set in the current C project. Basically, I want to access the Properties -> C/C++ Build -> Settings -> GCC C Compiler -> All options field.

I have searched how to access it, but I haven't found a way to do it. I tried to acces it via the preferences like in the following code :

    IEclipsePreferences root = Platform.getPreferencesService().getRootNode();

I can access the preferences of my plugin this way, but not those of the C project.

Does anyone know a way to do it ? I don't need to change the compiler options, just to know which flags are set.

UPDATE: I have found a solution.

    IResourceInfo info = getResourceInfo(translationUnit, description);
    ITool tools[] = info.getTools();
        for (ITool t : tools) {
            if (t.getName().compareToIgnoreCase("GCC C Compiler") == 0) {
                try {
                    //Finally the field I was looking for
                    String commandLine = t.getToolCommandFlagsString(getProject().getFullPath(), null);

                } catch (BuildException e) {
                    e.printStackTrace();
                }
            }
        }

I can then parse the string, not ideal but it works. I got the getResourceInfo() function from this post : How do I programmatically change the Eclipse CDT tool settings for a file?

So, thanks justinmreina for the answer !

解决方案

your traversing down a dark and lonely road, my friend :). but a fun one, nonetheless.

Setting Options on a Custom Toolchain/Tool

Here is an example where someone attempted to programmatically set the options of the GNU tools/toolchains:

*and here is that same author's background thread on solving it:

What he did here will get you to your resolution. I'd suggest browsing the plugin.xml of org.eclipse.cdt.managedbuild.gnu.ui first though. Focus on the toolchains, tools and their options.

Finding the Options of the GNU C Toolchain(s)/Tool(s)

Also, here is a useful post I put up awhile back on 'finding the dang options in a GNU C Project'. Not the exact OP's question, but the answer is relevant to your question.

Conclusion

I strongly doubt you'll find an answer <10 lines of code, even to set the '-v' flag for the Compiler... If you do find a simple result, I'd suggest posting it here as a followup.

Best of luck!

EDIT: I've been gnawing at this for awhile now, because I recently stumbled/failed over it. Here is how to set the options from code.

//assumptions
//#1 project  is [0] in workspace
//#2 compiler is [2] in workspace

//get project
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject("hello_world");

//get <storageModule moduleId="org.eclipse.cdt.core.settings">
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(proj);

//get <storageModule moduleId="cdtBuildSystem">
IManagedProject sub_info = info.getManagedProject();

//get <configuration name="Debug">
IConfiguration config = sub_info.getConfigurations()[0];

//get <toolChain>
IToolChain toolchain = config.getToolChain();

//get <tool name="GCC C Compiler">
ITool tool = toolchain.getTools()[2];

//get <option>
IOption option = tool.getOptionBySuperClassId("gnu.c.compiler.option.misc.other");

//----append new flag----//
String new_opt_value = option.getValue() + " -mySuperFlag";

//-----store it----//
ManagedBuildManager.setOption(config, tool, option, new_opt_value);
ManagedBuildManager.saveBuildInfo(proj, true);

Notes - once you start viewing this operation as an 'Eclipse Resource', the methodology becomes (somewhat...) clear - each object call down to access a field is just accessing another section within the XML schema of the .cproject resource

Hope this helps!

这篇关于Eclipse CDT从代码中获取GCC选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆