Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Help- Adding Library Programaticaly

Dear All,
I have updated my code. I want to add library when user clicks on button. Please help me. I am stucked here since very long.
Below is the code which I used it from PTP & ealrier post.

Thank you very much.

public class TestHandler extends AbstractHandler implements IHandler {

        public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject proj = root.getProject("project1");   //Can I get the currrent Project Name instead of hardcoding it?
        IWorkbenchWindow window =
                    window.getShell(), "MenuEclipseArticle Plug-in",
                    "Hello, Eclipse world");
                /*IProject proj = projects[0];*/
                //System.out.println( proj );
                //IProject projects = getProject();
         String newIncludePath = "/home/sagar/workspace";
         String newLib = "yes";
         String newLibSearchPath = "/home/sagar/workspace";
                System.out.println( proj );
                IManagedBuildInfo info = null;
                try {
                     info = ManagedBuildManager.getBuildInfo(proj);  // how do I get the current executing project here ?
                   } catch (Exception e) {}
                   System.out.println( info );    //code doesnot reach here
               IManagedProject mProj = info.getManagedProject();
               IConfiguration[] configs = mProj.getConfigurations();
               for (int i = 0; i < configs.length; i++) {
                      IConfiguration cf = configs[i];
                      addIncludePath(cf, newIncludePath);
                                   ManagedBuildManager.saveBuildInfo(proj, true);

  return null;


    public IProject getProject(){
                CProjectWizard wiz=null;
                final java.lang.String     PAGE_ID =    "New Probekit File (Probe)";
                MBSCustomPageData pData = MBSCustomPageManager.getPageData(PAGE_ID);
                 try {
                    IWizardPage wp = pData.getWizardPage();// in the constructor, this isn't available
                    IWizard w = wp.getWizard();
                    if(w instanceof CProjectWizard){
                        wiz = (CProjectWizard)w;
                        System.out.println("Can't get CProjectWizard from MBS page data. Quitting. No MPI info added to project.");
                        return null;
                catch (Exception e) {
                    Throwable reason = e.getCause();
                    System.out.println(e.getMessage()+" reason: "+reason);
                IProject proj=wiz.getProject(true);
                return proj;

           private void addIncludePath(IConfiguration cf, String newIncludePath)
                 String ext = "c";
                 ITool cfTool = cf.getToolFromInputExtension(ext);
                 String optID = "gnu.c.compiler.option.include.paths ";
                 IOption option = cfTool.getOptionById(optID);
                 String[] includePaths = null;
                 try {
                       includePaths = option.getIncludePaths();
                 } catch (BuildException e) {}
                 //add include path
                 int len = includePaths.length;
                 String newIncludePaths[] = new String[len + 1];
                 System.arraycopy(includePaths, 0, newIncludePaths, 0, len);
                 newIncludePaths[len] = newIncludePath;
                 ManagedBuildManager.setOption(cf, cfTool, option,

           private void addLinkerOpt(IConfiguration cf, String libName, String
                    libPath) {
                                String ext = "o";
                                ITool cfTool = cf.getToolFromInputExtension(ext);
                                // add to -l (libraries)
                                String optLibsID = "";
                                IOption option = cfTool.getOptionById(optLibsID);
                                addOptionValue(cf, cfTool, option, libName);
                                //    add to       -L (library search path)
                                String optPathsID="";
                                addOptionValue(cf, cfTool, option, libPath);
           private void setBuildCommand(IConfiguration cf, String buildCmd) {
                            ITool compiler = cf.getToolFromInputExtension("c");
                            ITool linker= cf.getToolFromInputExtension("o");

               private void addOptionValue(IConfiguration cf, ITool tool, IOption option, String value) {
                       try {
                           int type = option.getValueType();
                           String[] valueList = null;
                           switch (type) {
                           case IOption.INCLUDE_PATH:
                               valueList = option.getIncludePaths();
                               valueList = add(valueList,value);
                           case IOption.LIBRARIES:
                               valueList = option.getLibraries();
                               valueList=addNotPath(valueList, value);
                           case IOption.LIBRARY_PATHS:// this is type for library search path cdt 4.0
                               System.out.println("MPIProjectWizard runnable postprocessing (MPIProjectRunnable), can't get type of option for " + option.getName());
                           // update the option in the managed builder options
                           ManagedBuildManager.setOption(cf, tool, option, valueList);
                       } catch (BuildException e) {
                           System.out.println("MPIProjectRunnable.addOptionValue(), "+e.getMessage());

               private String[] addNotPath(String[] strList, String newStr) {
                           int len = strList.length;
                           String newList[] = new String[len + 1];
                           System.arraycopy(strList, 0, newList, 0, len);
                           newList[len] = newStr;
                           return newList;
               private String[] add(String[] existingPaths, String newPath) {
                           String;  // semicolon for windows, colon for Mac/Linux
                           List<String> newPathList = new ArrayList<String>();
                           String path;
                           for (int i = 0; i < existingPaths.length; i++) {
                               path = existingPaths[i];
                           String[] newPathArray=newPath.split(pathSep);
                           for (int i = 0; i < newPathArray.length; i++) {
                               path = newPathArray[i];
                           String[] newArray=(String[])newPathList.toArray(new String[0]);
                           return newArray;

Back to the top