How to generate and compile C++ code while the program is running? - c++

So basically what I need isn't a specific code (of course that would be great), but just an idea and methods on how to achieve my goal.
1) I have to create a program in C++ , which generates a little example of C++ code, that is each time a bit different. (This causes no problems for me, I will use a template and randomize some variables in the code, which will make it unique every time.)
2) I will display the generated code and the user will have to type in, what he thinks the code prints out.
And here is where the problems start:
3) I have to take the generated code and compile it somehow to get a string with the text that the program would have printed out.
4) And then compare the string with what the user has typed in.
So the step 3) is where I stop and can't figure it out without help... I was thinking to write the generated code in a function of a .cpp file and then call that function, but I couldn't get it to work, so I started to think, I should ask an expert, maybe there are some other methods or ideas how to achieve this.

Write the .cpp file
invoke the compiler (or make or equivalent build system)
if compilation fails, presumably display the errors?
if compilation succeeds, run the resulting program and display the output
It's probably simplest to wrap 2/3/4 into a script, and invoke that with system or popen. The script can make sure the filenames are unique, fold stderr into stdout, etc. etc.
Your running program isn't really interacting with the compiled code, just reading the output, so keeping it as a seperate process is probably easiest. The script can add some markup to help you distinguish compiler output/errors from the program output.
I haven't written a batch file for years, but once you know how to run your compiler from the command line (ref), you can write a script to:
compile a .cpp file
execute the resulting .exe
redirect its output to a file
then, in C++ you just need to save the code to the .cpp file the script expects, execute the script like system("myScript.bat"), and then read the output file.
If you don't want to write a seperate batch script, you can just call system once to invoke the compiler, and again to execute the resulting .exe.

I think you are looking for a way to script c++. Have a look at http://www.softintegration.com/ or this stackoverflow question Any tutorial for embedding Clang as script interpreter into C++ Code?.

I have not used this but it is free and also does not compile but interpret. Very suitable for your problem, worth a shot...
http://root.cern.ch/drupal/content/cint

why dont you compile the generated code standalone using system() call?
system("g++ temp.cpp -o temp.exe); --something of this sort and then based on the return value
you can run temp.exe again like system("temp.exe"); Ofcourse you can print the output from temp.exe to a file and read that file to get the output of temp.exe to your current program..

Would something like Geordi work?

You can invoke the c++ compiler just like you'd invoke any external tool. E.g. system("g++ test.cpp") or use popen or whatever else is offered by your platform.
You could also look into integrating a library that implements a compiler into your program. For that you might want to look into clang and llvm.

Related

Display matlab's program's output calling for another c++ program

I'm not sure if my question is clear:
I have a C++ project that I run from Qtcreator, this program call another c++ script outside the project. This last one runs a shell script calling a bunch of matlab scripts.
I want to display output from the matlab scripts, disp doesn't work.
I tried to write the values I want to read in a .txt file. The file is created but stay empty.
I tried these lines to write in a file:
fileId= fopen('imagename.txt','a');
fprintf(fileId, 'test : %s',imageName);
fclose(fileId);
I also tried assignin with the values I want to show but they're not kept within matlab's workspace.
I can't change the architecture of the whole program because it's a big project made by someone else I have to continue.
Do you have another way to watch what is going on the matlab scripts?
It's difficult to launch them directly from Matlab as I don't have access to their inputs values. I can hardly change the c++ script calling them to display theses values because I have a 'reference to ofstream is ambiguous' issue when I try to build it, so I will have to debug something made by someone else and non commented.
The fact that the file you write to stays empty is weird and I would try to find the reason regardless of your question.
Anyway, since you call Matlab from a shell script I guess you use 'matlab -r' option with your script name, or something similar. In that case you can use the -f option which tells Matlab to write the command line output to a log file:
matlab -logfile output.log

fortran: type "executable #script" in a terminal to run program using script

I have a fortran program in which I usually use scripts of commands, for example, in a terminal I write:
$program
then I enter into the program (the terminal shows "$program>") and accepts either commands or a script that I call by typing manually "#script":
$program>#script
then the fortran program opens the file named "script", which contains a series of commands or tasks that are executed.
What I want to do now is to type directly in a terminal:
$program #script
to run the program with the commands contained in the file "script".
I want to do this to be able to create sh scripts to run the program many times without having to enter into the program each time to write manually the name of each script.
Does anybody know how could I do this in fortran. I guess that the way is to start the fortran program by saying that if something was typed in a terminal after the name of the program, then the fortran program should be able to read it and use it internally. No matter which type of variable is given after typing "program", the program should be able to read it directly from the terminal. Any idea will be greatly appreciated.
Thanks in advance.
You may be able to do this:
program <<EOF
#script
EOF
Details may depend slightly on which shell you're scripting in, and on the details of the program you're trying to run.
Also, this is a shell scripting question, not much related to Fortran.
Fortran 2003 defines some intrinsics that allow you to retrieve the command line arguments provided to your executable.
The functions you are interested in are get_command_argument, command_argument_count and get_command.
get_command retrieves the entire command line.
command_argument_count returns an integer with the number of arguments passed on the command line.
get_command_argument gets the nth argument passed on the command line.
Note that this is functionality you'll need to add to your Fortran program, and if you do not have the ability to recompile it, then you cannot make this work using this approach.
In the case modifying the Fortran is not possible, then you may use programs such as expect which let you automate input into programs that provide prompting.

How to make a gui for command-line program?

Recently, i've got interested in making a front-end for command-line program.
I guess there's two way to do it.
First one is just including source code and calling main proc with arguments
(Of course, there should be some changes in source code).
Second one, which is there's no source code and just program, is just executing program internally then reading the command line with APIs.
Though I well know about the first solution, i don't know what APIs is needed to do the second solution.
I'm talking about the APIs that get a command-line string or something like that.
See this question for information on how to run an external application; basically, you need to call CreateProcess function. I'm not sure what you mean by "reading the command line", I suppose you mean reading the output of an executed program? As for capturing an external application's output, there's already another question asking for that, you will probably find this answer most helpful.
here is a codeProject project that I have used and can handle command line arguments for you (in the setup you describe). If you are not happy with it, you can use the direct WinApi calls using CommandLineToArgvW.
Enjoy!

C++ interpreter / console / snippet compiler

I am looking for a program where I can enter a C++ code snippet
in one window, press a button, and get output in another window.
Compilation should somehow be hidden behind the button. On a
per-snippet basis would be fine, full interactive probably asking
too much. It should run under Linux/Unix. Main use case would be
learning/testing/short debugging, etc.
Related stuff I found:
-- the Reinteract project for python (which i'm told sage has features similar to)
-- the same thread for C# here: C# Console?
-- the CINT interpreter from the CERN ROOT project
(which may be close, but maybe there are more comfortable apps around)
-- some programs called Quickly Compile or Code Snippet, which are M$.
http://codepad.org/ works nicely for this purpose. By default, it will run what you paste when you hit submit and display the result (or any errors you might have).
Dinkumware has a page for this AND you can choose the compiler
http://dinkumware.com/exam/default.aspx
Do something like this ?
test a C# snippet code by just copying it to the clipboard and then type csc.exe:
http://reboltutorial.com/blog/redirect-shell-to-rebol-console/
Cling (interactive C++ interpreter, built on the top of LLVM and Clang libraries): https://root.cern.ch/drupal/content/cling
Just configure your code editor to compile and run your code snippets.
Most code editors have the capability of 'sending' the current buffer/file to an external tool. I configure one editor key binding to compile the current buffer, and another key binding to execute whatever was last compiled (actually to run whatever has the same base filename as the current buffer with an '.exe' extension). My experience is with Windows, but the same or similar can be done on Unix/Linux.
Then it becomes one keystroke to compile and another to run what I jut compiled. This could also easily be just a single keystroke to compile & run, but I have several key bindings set to compile using various different compilers. That way I can easily test snippets using the latest MSVC, MSVC 6, MinGW GCC, Comeau and Digital Mars compilers to check for differences.
I would do it like this:
Capture the 'snippit' as text
Create a.cpp with the following:
int main() {
snippitCode();
return 0;
}
void snippitCode() {
// INSERT SNIPPIT HERE
}
Use 'exec' to launch a compiler and pipe the output to an output file.
Use 'exec' to run the application and pipe the output to an output file.
In the 'output' window, you can run 'tail -f' on the output file to continuously update when new output arrives.

Executing external program via system() does not run properly

I try to call a program (ncbi blast, for those who need to know) from my code, via calling the command in a system() call.
If I execute the string directly in the shell, it works as intended, but if I try the same string via system(), the program returns much faster, without the intended results. The output file is created, but the file size is 0. The returned error code is also 0. I even tried appending "> output.log 2> error.log" but these files are not created.
I guess it has something to do with environment variables or the path...
The output file name is given via -o command line parameter, not output redirection.
I read something about the popen command being possibly better suited for my use-case, but I can not find it, which library is that from?
The most usual cause of such problems is incorrect environment variable setting in ones ~/.bashrc.
You should be able to see what ncbi is unhappy about by executing
$SHELL -c '<exact string you pass to system()>'
Another common way to debug this is with strace. Execute:
strace -fo /tmp/strace.out ./myProgram
and look in /tmp/strace.out for clues.
Is there any reason that you do not want to fork and exec? This is a common idiom for executing one process from another.
popen is in the standard C library
See the man page
Some quick questions:
How do you execute in in the shell?
How do you execute in the system command?
What is the value returned by system?
Exact copy and paste from you terminal is preferable then an English description.