0

I developed a code which create a .cpp file from a .isc file. This .isc file contains tons of lines with logic circuit information. My code read every line of this .isc file and write a code in a .cpp file that will simulate the logic of each .isc line, and this .cpp is saved in the same folder of the code which creates it. What I want to do is compile and run the executable of this .cpp file I created with a command line straight from my main code. I've been doing some researches and I found that a makefile could do that for me. About makefile I found some information here:

Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?

C++ makefile on Linux with Multiple *.cpp files

Based on that, After creating and writing the converted code in the .cpp file, I created a makefile (with dynamically name), here is it:

ofstream make_file("Makefile", ios::out | ios::trunc); //read and open the file
if (make_file == NULL ){ cout << "Error creating makefile!"; return 1; }

make_file << "# Makefile" << endl;
make_file << "# This makefile will run the new cpp file created\n" << endl;
make_file << "CC = g++\n" << endl;
make_file << "# FLAGS:" << endl;
make_file << "CFLAGS = -g -B -Wall\n" << endl;
make_file << "Executable target:" << endl;
make_file << "TARGET = " << netlist << "\n" << endl;
make_file << "all: $(TARGET)\n" << endl;
make_file << "$(TARGET): $(TARGET).c" << endl;
make_file << "\t$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).cpp\n" << endl;
make_file << "clean:" << endl;
make_file << "\t$(RM) $(TARGET)" << endl;
make_file.close();

So, my objective is to make this makefile compile the .cpp file and run its executable, assuming this is possible. If it is and I created the makefile in a correct way, how do I execute, or "make" it?

Edit: I'm using codeblocks

Cœur
  • 37,241
  • 25
  • 195
  • 267
Leonardo Alves
  • 37
  • 1
  • 10
  • possible duplicate of [How do I execute external program within C code in linux with arguments?](http://stackoverflow.com/questions/5237482/how-do-i-execute-external-program-within-c-code-in-linux-with-arguments) – m.s. Jun 28 '15 at 20:49
  • @m.s. I'm think its not a duplicate because my objectives with this post is to know if I created the makefile in a correct way and how to execute it since it is a .cpp and not .o – Leonardo Alves Jun 28 '15 at 20:51
  • It is unclear to me what you want to do. compile some cpp file using make? how to invoke any external program is answered in the linked question. you cannot "run a cpp file", you need to compile it into an executable and run that. or compile it into a shared library, which you load dynamically. but you give way to few details so I can only guess. – m.s. Jun 28 '15 at 20:54
  • So your question is "how do I execute a makefile?"? – Oliver Charlesworth Jun 28 '15 at 20:56
  • @m.s., Oliver This is what I want to do, compile the .cpp created and run the executable of it. I thought the makefile I wrote do this and I just need to execute the makefile after, and do this straight from my C++ code. – Leonardo Alves Jun 28 '15 at 21:02
  • do you want to do this all from within your c++ program? if yes, then just call `system("make")` as shown in the linked answer. but why would you want to do this? I'd just use a script for that ... – m.s. Jun 28 '15 at 21:04
  • @m.s. What I really need its to execute what I have on the .cpp file. But, the only way to get the code for this .cpp file is constructing it from a .isc file. So, what my program do is convert the .isc file to what I need and put in this .cpp file. And to execute it I have to keep creating new codeblock projects. I want to avoid that by executing it right after creating it, automatically. – Leonardo Alves Jun 28 '15 at 21:09
  • I have no idea what a ".isc" file is. You should give a complete and detailed example of your use case in the question above. what is the input, what is your desired output, which steps do you need to automate, etc. explain why you can't use a standard shell script. – m.s. Jun 28 '15 at 21:12
  • @m.s. I edit my post with more explanation. – Leonardo Alves Jun 28 '15 at 21:24
  • I still don't see why you don't use a script which 1) runs your program to create `foo.cpp` from `foo.isc` and 2) runs `g++ -o foo foo.cpp` and finally 3) executes `./foo` – m.s. Jun 28 '15 at 21:27
  • @m.s. How can I do this direct from the code? Just add in the end of the code `g++ -o dynamic_name dynamic_name.cpp` and `./dynamic_name` , is this possible? – Leonardo Alves Jun 28 '15 at 21:32
  • 1
    Even better, also integrate the logic for creating the C++ file out of the ISC file into the makefile. The you can re-build the entire executable directly from the ISC file by just one `make` invocation. Make will figure out what files need to be re-generated due to updated dependencies. This is really the kind of thing Make was made for. – 5gon12eder Jun 28 '15 at 21:33
  • don't do it from within the cpp code. do what @5gon12eder wrote – m.s. Jun 28 '15 at 21:34
  • @5gon12eder I don't get it. I have the `.isc` file (this file is like a .txt file), I have my main program that read the `.isc` (ifstream) and create a new C++ code (ofstram) in a `.cpp` file (This cpp has it own main function). My makefile is created in the same main program which created the `.cpp`. So how can I just invoke `make` from the main program? – Leonardo Alves Jun 28 '15 at 21:49
  • 1
    I think everyone is telling you that instead of having your main program execute Make, you ought to have Make execute your main program. But if you really want your main program to be in command, then there is little reason to use Make. – Beta Jun 28 '15 at 22:16
  • @Beta I get it now. I'm not so familiar with Make, I thought that would be the only way. But it's possible to make my main program execute the make command to the makefile? even being possible to do it by scripting. – Leonardo Alves Jun 28 '15 at 22:28
  • Yes, as others have said before, just call `system("make")`. – Beta Jun 28 '15 at 22:57

0 Answers0