5

I understand that java source codes can be compiled via a string using JavaCompiler. With a long String containing my java code, I can test if my code is compilable.

Source: http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

An Example: http://www.java2s.com/Code/Java/JDK-6/CompileaJavafilewithJavaCompiler.htm

My question is: I have a long string that contains a compilable C++ code. Am i able to do something similar using some form of java library or is it just impossible?

Thanks

Edit 1: As requested, the String can be user-generated (typed in a GUI - JTextArea) OR read from a .cpp file..

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3188291
  • 567
  • 1
  • 9
  • 22
  • 1
    In short: no, there is no built-in compiler in the C++ runtime. You can of course write code that results in some output from your program being compiled into another program by running the compiler over that output [and at least some compilers can do that using `stdout` from your program as the input to the compiler, so you don't actually need to produce a file as such] – Mats Petersson Jul 02 '15 at 23:07
  • Aww.. thanks for the reply... My real objective is when my program (using java) is to read in either C++ of java source codes, and test whether the input source code is compilable. Since i cant do that, i guess i have to scrap that feature off :(.. Thanks for your insight though. – user3188291 Jul 02 '15 at 23:15
  • You could redirect the string into `gcc` [via the command line...](http://stackoverflow.com/questions/1003644/is-it-possible-to-get-gcc-to-read-from-a-pipe) (by using a call to `system`) – scohe001 Jul 02 '15 at 23:25
  • 3
    You could do that by creating a temporary file and calling `system("g++ temfile.cpp");` or something similar. Or you could integrate with a compiler library, such as libclang, which allows you to build your own compiler within your own program - but that's a lot more work than calling system. – Mats Petersson Jul 02 '15 at 23:26
  • If you've got gcc, you probably also have `popen` which is a bit nicer way to find out if your source compiles. `system` has rather limited feedback. – MSalters Jul 03 '15 at 07:26
  • Where is the string coming from? You should edit your question to motivate it and explain that. – Basile Starynkevitch Jul 03 '15 at 07:41

3 Answers3

0

If you are using VisualStudio you can use a pre-build event to call the c++ compiler and compile a file. If you are having an error your Java project will not build. The idea here is that you are making an event happen before the build. You can make that event whatever you want, like for example, checking if a file compiles,

Here is a tutorial: https://dillieodigital.wordpress.com/2012/11/27/quick-tip-aborting-builds-in-visual-studio-based-on-file-contents/

In the part where he enters a script to run, that's where you would put your call to your favorite c++ compiler. He is not checking the same type of file but the principle is the same, he is checking a file.

If you are running Windows, which, you would be if you are using VisualStudio, this below will be helpful.

Compiling a Native C++ Program on the Command Line: https://msdn.microsoft.com/en-us/library/ms235639.aspx

So basically, you're making the VisualStudio project do a pre-build event which is a call on the command line to the c++ compiler to check your file before you build your Java project.

Hope that helps.

sitting-duck
  • 961
  • 1
  • 10
  • 22
  • 1
    If you are using Linux I'm pretty sure you can write a make file to compile both Java and C++ programs. If you are on MacOS I have no idea what to do. – sitting-duck Jul 02 '15 at 23:34
0

If you link to the LLVM library, there are facilities for this.

But beware that LLVM does not provide a stable API, so it is difficult to construct examples that continue to work. Even using the C API (which still requires updating the SONAME), I have had breakage with every single LLVM release.

o11c
  • 15,265
  • 4
  • 50
  • 75
0

My question is: I have a long string that contains a compilable C++ code.

The C++11 standard does not mention any function able to do that (compile C++ code in some string). And I know no library (except perhaps libclang, but I don't know if it able to compile a string) able to do that.

Actually, a C++ compiler practically needs to make a lot of optimizations (if you want the code to run not too slowly), so will spend some significant time (relative to computer speed, e.g. several tenth of seconds even for a small C++ source code) to compile your generated C++ code. And heavily templatized C++ code may take a lot of time (even an infinite amount in pathological cases, since C++ templates are accidentally Turing Complete) to be compiled.

So practically speaking, you gain no advantage to not writing a C++ source file. Some compilers (e.g. GCC on Linux with g++ -x c++ /dev/stdin) are able to compile C++ code from their standard input, so you could use (on POSIX systems) popen to feed them.

Just write your C++ code into some temporary C++ source file (perhaps in some tmpfs file system, if you want to avoid disk IO) or perhaps a pipe(7) or fifo(7)... and fork a compilation. On Linux and Posix systems, you could compile (e.g. with g++ -Wall -fPIC -O -shared /tmp/temporary1234.cc -o /tmp/temporary1234.so) that code to a "plugin" or shared object that your main program could later dlopen

If you are generating the C++ code, you could consider using (instead of generating C++ source then compiling it), some Just-In-Time compilation library like gccjit, LLVM, libjit, lightning, asmjit etc... Then you'll generate some AST-like internal representation (specific to the JIT library!) of the code.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547