Generate executable file at runtime - C++ / Visual C++ - c++

Maybe you can help me with a question I have, related to C++ language:
Is it possible or, is there a way to generate an executable file at runtime of a C++ Windows application?
To illustrate you why I'm asking this, I will detail the scenario that I'm thinking right now:
I have a C++ Windows Application that is more like an editor, where the user inputs some data and make some configurations, so when everything is done, the user clicks the "Run" button and this editor based on the user input data and configurations, creates some C++ or C# code files, compiles them and generates an executable file that the user can simply use without having to enter any line of code by himself.
Is it possible? Please can you give me a clue of how to do my search or where to find some help?

Of course it is possible, the compiler itself is likely a program that fulfils this requirement.
If you want to do it without just invoking a compiler though (and thus generate source code to feed it), you need to learn quite a bit about the machine language and executable file format of your platform.

You could simply make a call from your application to an external compiler, make the compiler build your executable and if the compilation is successful you could run the resulting executable. You can even capture the output of the compiler in your application.

Related

How to make building / compilation more comfortable

My current workflow when developing Apps or programs with Java or C/C++ is as follows:
I don't use any IDE like IntelliJ, Visual Studio, ...
Using linux or OS X, I use vim as code editor. When I build with a makefile or (when in Java) gradle, I :!make and wait for the compiler and linker to create the executable, which will be run automatically.
In case of compilation errors, the output of the compiler can get very long and the lines exceed the columns of the console. So everything gets messy, and sometimes takes too much time to find out, what the first error ist (often causing all following compile errors).
My question is, what is your workflow as a C++ developer? For example is there a way, to generate a nicely formatted local html file, that you can view / update in your browser window. Or other ideas?
Yes, I know. I could use Xcode or any other IDE. But I just don't want.
Compiling in vim with :!make instead of :make doesn't make any sense -- it's even one of the early features of vim. The former will expect us to have good eyes. The latter will display compilation errors into the quickfix window, which we can navigate. In other words, no need to use an auxiliary log file: we can navigate compilation errors even in (a coupled of) editors that run into a console.
I did expand on a related topic in https://stackoverflow.com/a/35702919/15934.
Regarding compilation, there are a few plugins that permits to compile in background. I've added this facility in build-tool-wrapper lately (it requires vim 7.4-1980 -- and it's still in a development branch at this time). This plugin also permits me to easily filter errors in the standard library with the venerable STLfilt, and to manage several build configurations (each in a separate directory).

What do I need to do to compile a C++ program with Embarcado XE7?

I'm trying my first program in this particular environment and it seems unnecessarily complicated.
I've written a header file xxxx.h and a C++ file xxxx.cpp but there doesn't appear to be an option to compile this (where I would expect it to be in the toolbar, the run and/or compile options are greyed out and unavailable).
What am I doing wrong? Maybe I haven't saved these files in the right place or I don't know where they should have been written/created.
I know nothing about this compiler/editor so there may be loads of things that are obvious to an experienced programmer which I'm just not aware of. Do the .h and .cpp files need to be part of a project which ultimately gets compiled? If so, how do I make these files part of that project?
I feel like I couldn't even write a "Hello World" program in this environment with my current knowledge. Please help.
The IDE(Environment) might require you to make it a project type. If you can't find help or instructions in the program itself, please try the following:
select file, then new. In the drop down box, select project and just read through whatever comes up and click OK. Now, you might already have a .cpp file opened by default. If you do, you are lucky, just type your code in, and you will be able to compile it. Otherwise you have to make a new .cpp file within the project using the project window.
Please bear in mind that I'm saying this with experience only form similar IDEs, not your particular one. If you are a beginner, then probably Borland C++ IDE will be better for you. Not the embarcadero one, the older versions used in the 1998-99 period. Hope this helps. Thanks.

Fastest way to write & compile a C/C++ program in Windows

I'm usually using Visual Studio, but several things bother me when I just quickly want to test some code:
it has a rather long startup time
it always needs a project to execute/debug files
program output gets printed to the console, but the window simply closes when I don't insert a getchar() or a breakpoint in the program and thus I'm not seeing it.
I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question, running it and seeing its output.
I don't think that console programs or g++ under CygWin are a good solution, because there it takes ages to cd into the right dir to save the file, I'm not used to editors such as Vim, and typing in the compiler commandline myself has always annoyed me etc.
So I guess what I'm looking for is a very lightweight free C/C++ IDE which is preconfigured to work with a free compiler (bonus points if it is even shipped with it.)
What can you recommend which adresses at least two items from the list above?
Is there maybe even a program which can execute/interpret C or C++ in an interactive commandline (like Python)?
I'm looking for a program which is suitable for a really, really quick
programming in Windows. Such as, copying some code from an SO question
and executing it and seeing it's output.
For quick-and-dirty experimental coding, I really like codepad.org. Not having to create a file is especially nice as it saves me from coming up with a suitable name and disk location. Be aware that it uses g++ 4.1.2 behind the scenes so some of the latest C++11 features aren't supported.
"really, really quick (and dirty, throw away?) programming "?
Compiler : VC++ command line - you already have it.
Editor: Notepad or somesuch
Compilation process: A .BAT file you write once
and supply a parameter with the name of the single source file.
Location: Set up some desktop shortcuts to a known directory for your
test code.
Use TCC : Tiny C Compiler
start a command prompt
cd wherever
notepad main.c
write code in notepad. save
back in the command prompt type tcc -run main.c
notice errors, go back to 4
Note that with -run parameter you're invoking tcc like an interpreter
Which compiler you use doesn’t really matter. I prefer G++ but cl.exe (from Visual Studio) works equally well.
In order to use the compiler quickly from the command line, either
include it into your PATH variable by setting it in the system settings, or
create a simple .cmd script which launches a console with the right paths included.
Visual Studio incidentally comes bundled with such a .cmd script which is linked in the Start Menu entry of Visual Studio. Personally, though, I prefer adjusting the PATH variable.
Then you can simply invoke the compiler from any directory in the command line. If you are too lazy to write the whole command line, create a script to do it for you. Or use Cygwin and (C)Make.
Two additional remarks:
Starting the project using the build configuration (Cntr+F5 (?)) leaves the console open after the program has run, without you having to include getch() calls or similar.
I highly recommend you learn an editor such as Emacs or Vim, unless you plan never to use any other platform than Windows, and even then. These editors are just tremendously powerful, and in some ways light-years beyond what the Visual Studio code editor offers.
But if you really don’t have the time, use a decent text editor such as Notepad++ instead.
Open Watcom is easy to install and use, it's fast and it's the closest compiler to MSVC++, although it's noticeably behind in features (especially in C++).
I don't use its IDE at all as I got used to doing most of the stuff in the console, but it's there and the debugger is there too.
Compiling one-filers is easy.
Compiling C code:
wcl386.exe /we /wx /q sourcefile.c
Compiling C++ code:
wcl386.exe /xs /we /wx /q sourcefile.cpp
On my machine, I have a "empty" project called "Test". When I want to test some random code on the internet, I simply put it into main.cpp in that project, and compile.
If you think MSVC takes too long to load, it should be possible to write a batch script that attempts to compile the project and puts the build log in a file. Then you can simply alter the existing main.cpp with notepad, double click the batch file, then pop open the build log or run the executable.
[Edit] I made a batch file to compile the entire solution. Turns out that requires loading visual studio. However, the batch file can compile/run a single cpp file easy enough.
My favorite IDE: http://www.codeblocks.org/
Here is a direct link to the download that includes the MinGW compiler: http://download2.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe
You're not gonna find any (good) C/C++ interpreters.
Once I used PSPad setting its "compiler" option for C++ files to a reasonable default (cl.exe in the correct directory, speed optimization, all warnings). Then it's just Ctrl+F9.
All of the above compiler recommendations are good. For an editor, I really like Notepad2
I know you didn't ask...
First off, your expectations are not reasonable. no program can guess what you want, over a range of input from the simplest to the most complex. If cd'ing into cygwin is too hard, and starting up visual studio is too time-consuming, you're pretty much toast. Sorry.
That said, you can edit code with notepad (which you can invoke from the command line as notepad foo.cpp). Notepad uses your mouse and the arrow keys on your pc so it's pretty intuitive.
you can use visual studio tools from the command line, without having to fiddle with project files.
Visual studio comes with a tool called nmake, the most basic usage of which is similar to linux make. If you have very simple input, nmake's default rules may be good enough to produce an executable. If not, you may be able to construct a makefile that will take any single simple file, say foo.cpp, and compile and link it to an executable called foo.exe. You'll still have to learn to use nmake, which some people think is easy, and others think is fiendishly difficult. Try nmake foo.cpp and see if the result is what you want.

Designing IDE in Qt; is there an already made compiler package I could use?

I am designing a simple IDE just as a side project.
I don't want to design a compiler for something that is just a side project that I will only be working on ever once in awhile.
So, is there some sort of pre-made, open source package I could use and link with my IDE?
Right now all I care about compiling is c and c++ but I want to add support for other languages (Java, C#, Perl, etc...) at some point, if I'm still working on the project.
Thanks. Again, just to clarify, I am looking for a c/c++ compiler(compilers for other languages would also help) to link with my Qt made IDE project to compile the code written in my application.
You just need to call command line GCC and capture output (stdout) to display in your application window.
It is simply a matter of invoking the desired compiler as an external process. You will need to pass the correct command line arguments, and presumably capture the output of the compiler and display it to the user in the GUI.
Since you are using Qt, I would suggest looking at QProcess. QProcess provides a simple and platform-neutral way of invoking a process and communicating with it.
Apart from that, all you need is a way of generating the correct command line arguments for each compiler you wish to integrate with your IDE.

Compile batch file into an EXE file

I want to compile a batch file into an EXE file using C++. I can get through parsing the batch file and writing a new .cpp file. But I don't know how to compile the new .cpp file into an EXE file for the end user.
OK, here's the thing, I am creating an application in DevC++ that will read in a batch file. Then, one by one parsing it using:
system(getline(myfile,line));
After setting everything up, I save the newly created file as "main.cpp".
The problem is, I want to compile it into an EXE file, from my program, for the end user.
So basically, can I compile a C++ file from a C++ EXE?
Yes, you can provided that the end user has a C++ compiler installed and you're emitting valid C++.
Depending on the compiler you're using, your C++ executable would have to spawn a process that runs
cl main.cpp
or a similar invocation of the compiler after finishing the translation.
If your user doesn't have a compiler installed, then you're pretty much out of luck - trying to build a C++ compiler yourself is a rather non-trivial exercise.
The short answer is no. Unless you are willing to write an entire C++ compiler, you will need to invoke an external C++ compiler to compile that .cpp file.
On the plus side, if you are simply looking to convert .BAT files into .EXE files, there are several existing solutions, such as quickbfc.
Can I ask why do you need to parse bat file?
I mean if you are taking input or something from that file then can you try to use a database or something for that?
Also for the user end you can write web application to display output.
There`s C++ Server Pages equivalent to JSP, PHP which can use C++ classes.
Am I helping here or this is not what you want? may be if you can describe you application use somebody can help you better.