问题
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 !
回答1:
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:
- Stack Overflow: How do I programmatically change the Eclipse CDT tool settings for a file?
*and here is that same author's background thread on solving it:
- Eclipse Forum: How do I programmatically change the Eclipse CDT tool settings for a file?
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.
- Stack Overflow: Eclipse CDT New Project Template - How to add a library
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!
来源:https://stackoverflow.com/questions/22376138/eclipse-cdt-get-gcc-options-from-code