C program compilation from a java program

笑着哭i 提交于 2019-12-12 14:42:48

问题


I am trying to compile a c program from a java program on Linux platform. My snippet is.

          ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/gcc",
          "-c","/hipad/UserProject/example.c");

          Process proc = processBuilder.start();

There is no error during compilation of java program but I am not able to get .o file. I tried to find out solutions but no one is working. Any suggestion.....


回答1:


The default working directory of a child process is what ever directory the Java process has as a working directory, which usually is where it was launched from. And by default gcc writes output files to current working directory. That's where you should find example.o.

There are two simple ways to solve this. You can give gcc -o option and full path and name of desired output file, or you can set working directory of child process, like this:

ProcessBuilder processBuilder =
    new ProcessBuilder("/usr/bin/gcc", "-c","example.c"); // source in working dir
processBuilder.directory(new File ("/hipad/UserProject")); // or whatever
Process proc = processBuilder.start();

See ProcessBuilder javadoc for more info.



来源:https://stackoverflow.com/questions/20463523/c-program-compilation-from-a-java-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!