Create an executable file from given files

Hello there! I will jump in straight to the problem. Take a look at the following scenario: I have a couple of files (.h and .cpp) and a main method in one of these files. All these files when built will give you an exe file with "hello world!" printing in the console window. The problem here is that I need to build the files through visual studio or some sort of compiler. I want to create a new program that will take in these couple of files and when I run this new program, it will create an executable file from those files that it took. Is there a library do this for me? PLEASE PLEASE HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

thanks
In Visual Studio, you'll find an exe file in the folder called "debug" or "release" contained in the project folder. Is that what you're looking for?
Um, I'm not quite sure what you're asking. You don't need a "library" to put these files together - Visual Studio itself is the thing that will compile these files and link them together to make the executable (*).

Just create an empty project in VS, and add the files that you want to compile and link to make your executable. Then build the project.

As zapshe says, the executable will be built into a directory called either "Debug" or "Release", depending on what type of build you're doing.

(*) Strictly speaking, it's the compiler that builds and links the executable. VS runs the compiler, and tells it what files to build. But you don't need to worry about that.
Last edited on
I know everything you're saying but you are not getting my point. I know visual studio provides the creation of exe files. Here is what I want to do: I have a program. It asks me for some files: I give it some files (these files are .h and .cpp and one of them include a main method). Then I click "export" and it generates a .exe file. When I run the exe file it runs the code on the files that I gave it (.h and .cpp). Thanks for the reply. Please don't get angry with me!
So... you want to write a program that will invoke the compiler to compile some code in some existing .cpp and .h files you have. Is that right?

EDIT: And why do you think anyone's getting angry with you?
Last edited on
So it sounds like you're trying to make your own wrapper for a compiler. Some might call this a simple form of an IDE :P

The easiest way would be to have your program call the same .exe files (compiler + linker) that Visual Studio calls internally.
https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2019
See "Command-line tools" section.

https://docs.microsoft.com/en-us/cpp/build/walkthrough-compiling-a-native-cpp-program-on-the-command-line?view=vs-2019
Last edited on
Um, unfortunately it seems like this is getting a little too complicated. This may look like off topic but trust me it isn't. Can anyone just tell me how the Unity engine exports a game. When you make a game with unity, you can export it as an independent playable game. How does unity go about doing that? thanks.
Um, unfortunately it seems like this is getting a little too complicated.

It actually isn't complicated at all. It should be quite easy to figure out, from the tutorial Ganado linked you to, the command-line command you'd need to run the compiler.

Then writing a C++ program to execute that command, using the system() function, is trivial:

https://www.geeksforgeeks.org/system-call-in-c/


Unity works with C# and .NET, so "all" the Unity SDK has to do is build a .NET executable that includes the game's resources inside the binary. To do this, it can either use Microsoft's compiler (I doubt it, but I've never used Unity, so I can't say it definitely doesn't do that), or the Mono Project's compiler.

One way or another, a program that takes source code and produces an executable must necessarily either be, or indirectly execute a compiler. There's no way around this.
Last edited on
You can choose between using the Roslyn (MS) compiler or Mono, I think.
https://docs.unity3d.com/Manual/CSharpCompiler.html

I don't think Unity is open-source, and I'm not too familiar with Unity, much less its internals. But the basic idea is that when you publish a build as a standalone application, internally the Unity IDE will be calling some sort of compiler, which generates an EXE file, and any assets will be copied over and integrated as well.

Looks like the assets are all wrapped together and compressed using a compression method
https://docs.unity3d.com/Manual/BuildSettings.html (bottom of page)
Thanks so much for replying!!! So Unity DOES call a compiler? I want to know how to go about implementing this functionality? How does it call the compiler?
Ultimately, by building a command line, and running it, whethr directly on an actual command line, or by calling some kind of createProcess function.

Either way, a command line is built. If it were calling the g++ compiler, it would effectively be typing (for example):
g++ somefile.cpp someotherfile.cpp -o output.exe
on the command line and pressing enter.

If you want to do this youself, first you need to learn how to use a compiler so you know what command line to build.
Great!! Can you point me in the right direction? either some link or some sort example source code? THANKS!
assuming you distribute the compiler with your program or tools, and easy way to do it in windows is a batch file.
for example I have this one in my 'quick trash programs' folder:

g++ %1 %2 %3 %4 %5 %6 -std=c++17 -Wall -Wextra -pedantic-errors -O3 -s

you may want to add a -oname (to generate name.exe) to the above. that could be put in as %1 and reorder the parameters:

g++ %2 %3 %4 %5 %6 %7 -std=c++17 -Wall -Wextra -pedantic-errors -O3 -s %1

and then you might call it in your c++ code (there are some safer alternatives to 'system' but to get started its fine).

system("compile.bat -ofirst first.cpp second.cpp third.cpp");

and then to run the program
system("first.exe");

you may want to redirect the output of g++ to a file and parse that before attempting to run first.exe; for your own sake, and throw an error or something if the compile failed.

you can redirect output of any program to a file via > file
so g++ ...stuff... >gout.txt for example

system is dumb as a stick. IT ALWAYS SUCCEEDS. Let me say that again: IT ALWAYS SUCCEEDS! What that means is that you try to compile, and system looks like it succeeded even if g++ failed. You try to run first.exe, and it looks like it succeeded, even if there is no such program. And so on. At some point you are going to have assumed you did something that did not actually happen, and bad things will happen. Some of the alternatives to system may be able to tap a return code to see if it was a success, or you can do your own babysitting, but you must check every step to ensure that what you think happened, actually did happen. System is also a security risk, but we have covered that to death... IMHO if they are far enough to hack via that, they already own your system anyway.

Last edited on
You are not reinventing make, are you?
https://en.wikipedia.org/wiki/Make_(software)

However, the make requires instructions. Some other tool might provide them. For example:

https://www.gnu.org/software/automake/manual/html_node/GNU-Build-System.html
cmake .
make


or

http://derekmolloy.ie/hello-world-introductions-to-cmake/
./configure
make


or IDE of your choice, etc.


Alas, cmake/configure/IDE/Unity requires instructions before it can give 'make' instructions ...

... and there might be tools for that too.


Do you really need a new tool or could you learn to use existing tools?
Great!! Can you point me in the right direction? either some link or some sort example source code? THANKS!

You've already been given links to documentation for the system() function, which is what you'd use in your C / C++ code to call the compiler.

What compiler are you using? You mentioned Visual Studio, which uses the CL compiler, and you've already been given a link to a tutorial for using that from the command line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;


// Defaults
string COMPILER = "g++";
string OPTIONS  = "-O3 -Wall -pedantic -Wextra";
string SOURCE   = "temp.cpp";
string EXENAME  = "go.exe";


//======================================================================

int main()
{
   string compiler = COMPILER;
   string options  = OPTIONS;
   string source   = SOURCE;
   string exename  = EXENAME;
   int choice;
   string cmd;

   while ( true )
   {

      cout << "\n\nOption number (current values):\n"
           << "0 to exit this program\n"
           << "1 for compiler ("        << compiler << ")\n"
           << "2 for options ("         << options  << ")\n"
           << "3 for source files ("    << source   << ")\n"
           << "4 for executable name (" << exename  << ")\n"
           << "5 to build executable\n"
           << "6 to run executable\n"
           << "7 to write settings to file\n"
           << "8 to read settings from file\n"
           << "====> ";
      cin >> choice;   cin.ignore( 1000, '\n');

      switch ( choice )
      {
         case 0:
            return 0;
         case 1:
         {
            cout << "Enter compiler (g++ or gfortran): ";   getline( cin, compiler );
            if ( compiler == "" ) compiler = COMPILER;
            break;
         }
         case 2:
         {
            cout << "Enter options (R.T.M.): ";   getline( cin, options );
            if ( options == "" ) options = OPTIONS;
            break;
         }
         case 3:
         {
            cout << "Enter source files: ";   getline( cin, source );
            if ( source == "" ) source = SOURCE;
            break;
         }
         case 4:
         {
            cout << "Enter executable file: ";   getline( cin, exename );
            if ( exename == "" ) exename = EXENAME;
            break;
         }
         case 5:
         {
            cmd = compiler + " -o " + exename + " " + options + " " + source;
            cout << "Issuing command: " << cmd << '\n';
            system( cmd.c_str() );
            break;
         }
         case 6:
         {
            cmd = "start " + exename;
            cout << "Issuing command: " << cmd << '\n';
            system( cmd.c_str() );
            break;
         }
         case 7:
         {
            ofstream out( "makeit.txt" );
            out << compiler << '\n' 
                << options  << '\n' 
                << source   << '\n' 
                << exename;
            out.close();
            break;
         }
         case 8:
         {
            ifstream in( "makeit.txt" );
            if ( getline( in, compiler ) &&
                 getline( in, options  ) &&
                 getline( in, source   ) &&
                 getline( in, exename  ) )
            {
               cout << "File read successfully\n";
            }
            else
            {
               cout << "Can't read file; reverting to defaults\n";
               string compiler = COMPILER;
               string options  = OPTIONS;
               string source   = SOURCE;
               string exename  = EXENAME;
            }
            in.close();
            break;
         }
      }
   }
}
Last edited on
Registered users can post here. Sign in or register to post.