Creating makefile dynamically and running make command automatically by the main program - c++

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

Related

How to compile .cpp to a valid .cgi?

Below is my test.cpp file.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Content-type:text/html" << endl << endl;
cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "<title>Hello World - First CGI Program</title>" << endl;
cout << "</head>" << endl;
cout << "<body>" << endl;
cout << "<h2>Hello World! This is my first CGI program</h2>" << endl;
cout << "</body>" << endl;
cout << "</html>" << endl;
return 0;
}
Below is how I compiled the test.cpp file to test.cgi file.
g++ -g test.cpp -o test.cgi
Below is how I change mode of the test.cgi file.
chmod 755 test.cgi
There is no error at all.
But when I visit the page in the browser "localhost:8080/test.cgi", I get the following error:
C:/xampp/cgi-bin/test.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[cgi:error] [pid 22568:tid 1864] (9)Bad file descriptor: [client ::1:60380] AH01222: don't know how to spawn child process: C:/xampp/cgi-bin/test.cgi
The default cgi.cgi of Apache server works as well. It seems like the test.cgi file is invalid. Because I cannot even view the content of the file in Visual Studio Code, while I can view the content of the cgi.cgi file as well, the default file of Apache server.
When I run the following command line in the terminal, the content of the test.cgi file is printed successfully.
./test.cgi
How to create a valid cgi file from a cpp file?

In Visual Studio 2019 can't read text file from release mode but works fine in debug (running within VS)

I am currently trying to read in a text file for use in a project but when running in release form (running the .exe file from a command window) the file doesn't read in. But yet in debug mode it works fine. I have tried having the files in the same directory as the .exe file but it doesn't seem to find it and I don't know why?
The code:
std::cout << "Acquiring data file..." << endl;
std::cout << "Reading data file..." << endl;
ifstream tempData;
tempData.open("temp_lincolnshire_short.txt");
if (tempData.is_open()) {
std::cout << tempData.rdbuf() << endl;
}
else {
cout << "Reading failed..." << endl;
}
tempData.close();
std::cout << "Data file closed." << endl;
What I ended up using was a relative path as identified on https://stackoverflow.com/a/35910234/3795116 which ended up being:
myFile.open("../Release/frequency.dat", ios::in);
*changing myFile to whatever your variable is.

C++ not run an output properly

This is a clip of my code
void start_hang(){
cout << "*********************\n";
cout << " \n";
cout << " \n";
cout << " \n";
cout << " \n";
cout << " \n";
cout << " \n";
cout << "*********************\n";
cout << "=====================\n";
}
But here is my output
*********************
*********************=====================
Here is my other attempt to make it work:
Not using namespace
Put std:: in front of every output
void start_hang(){
std::cout << "*********************\n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << "*********************\n";
std::cout << "=====================\n";
}
And still does not work.
Maybe I'm using class wrong?
Here is my full code:
https://pastebin.com/AmfZErqS
Here is my compiler:
g++ (tdm64-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The code is correct and there's no issue with it. The issue lies in the way you're compiling and executing.
On UNIX-like systems, the default name for the output binary from gcc is a.out (which is also an ancient executable format)
-o file
Place the primary output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.
https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
On Windows it's a.exe by default. You must specify the name using the -o option, or you'll have to run a.exe. So if you compile with g++ game.cpp then run game.exe then you're executing some old buggy game.exe compiled before
See Why does my GCC compiler not compile C code?
Im compiling with g++ game.cpp which resulting to the error hmm
however , after i do g++ -g game.cpp -o game.exe , it gives an output and and the program works as it supposed to be

How to execute an a.out file by using exec function family in Linux

The aim of this c++ program is about the understanding the cocurrent process mechanism in operating system. And the following code is for the child functions of one process. And the child process have theirs numbers, NO.5 and NO.6.
I'm trying to exectute an a.out file in the NO.6 process. I'm trying to do it this way.
void ChildFunction_For_ProcessNO.4(int i){
switch(i){
case(5):
cout << "This is process five, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
CreateThreads_Five();
cout << "Process five has ended.\n" << '\n';
break;
case(6):
cout << "This is process six, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
execl("./a.out", "a.out", NULL);
//and I also tried this way
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
char buf[100];
cout << "getcwd: " << getcwd(buf, sizeof(buf))) << endl;
cout << "Process six has ended.\n";
break;
}
and the getcwd's output goes like this
getcwd: /home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
The a.out file has already been put in the folder together with the c++ program.
the screenshot of the working directory
and the compile went well, but there is just no output which ought to be Hello world from the a.out file. How can I solve this problem. Thanks a lot!
The getcwd output shows that you have to put your file a.out into
/home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
Your attempt
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
is wrong this is not the full path. You would have to use
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "a.out", NULL);
BTW: You should specify the same value as argument 0 as you use for the program to be run, i.e.
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "/home/chengxuyuan/CLionProjects/Project_1/a.out", NULL);
or
execl("./a.out", "./a.out", NULL);
This is what the shell would do and what most programs expect.

How to read and write input file and output file

I am trying to run the following program:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double first=1.49, second=2.59, third=3.69, fourth=4.79;
inFile.open("prices.txt");
char response;
if(!inFile.fail())
{
cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
return -1;
}
}
outFile.open("prices.txt");
if (inFile.fail())
{
cout << "\n The file does not exist and can not be opened" << endl;
cout << "The file has been successfully opened for output." << endl;
outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
outFile.close();
exit(1);
cout << "The file has been successfully opened for output. " << endl;
outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
outFile.close();
return 0;
}
}
Yet this program will not write the values to the prices.txt file. If you run the program once it says the file does not exist. Running it a second time says the file is already there and if you want to overwrite it. The thing is searching my Mac I cannot find this file anywhere.
Any ideas what I am doing wrong with running it in Xcode? A friend runs the exact same code in Visual Studio 2008 and it works. Any help is appreciated.
You need to set the working directory for the executable since you are assuming that your data files are in the current working directory. In Xcode 3.x you set this in the first tab of Get Info for the executable. In Xcode 4.x it has been moved, but the principle is the same.
Alternatively you can change your data file paths (e.g. make them absolute) so that you do not make assumptions about the current working directory.
You may not have permission to write into the directory that you are trying to save the file too.
Also, there is an error in your program and I am sure if it is there for debugging reasons. You have
outFile.close();
exit(1);
But then shortly there after you try to write to the file, then close it again.