Cannot run .exe file after compiler a c++ file - c++

I use VSC to make a simple hello work c++. Then I use "g++ c.cpp -o c" to compile, but I cannot run the c.exe file by command "c.exe". I remember I could run c++ before in the same way. Do I miss anything? Could you help me please?

It seems like c.exe is there.
Since you are using PowerShell, you need to run local files with a .\ prefix, e.g. .\c.exe

Related

Generate preprocesed file ( .i ) in code blocks

I am following The Cherno C++ series and in this video he is talking about generating .i files. I am an Ubuntu [20.04] user and am making my projects on Code::Blocks. Does any one know how to generate .i files in Code::Blocks?
actually you can't directly genrate a preprocessor file in Code::Blocks
as far as my knowledge go. Because I am also following the same series and I also had this problem { i am also using C::b }, so what you can do is in ubuntu terminal go to that file location by using cd command and then use
gcc -E FileName.c -o FileName.i
command this will genrate the preprocessor file.

How to compile multiple files in sublime text 3

I'm using Sublime Text 3 to write some school projects and I have to write a program with some custom headers which have 1 function. The problem is that the build system (what sublime text writes in cmd to build) only puts the main.cpp file and not the headers and .cpp of the headers in the cmd, so it's not compiling properly.
I'm looking for a way to compile with headers just by writing the main.cpp file in the cmd ("g++ main.cpp -o main.exe") or to modify the build system so it adds all the headers in the cmd line ("g++ headersourcefile.cpp header.h main.cpp -o main.exe") (this one compiles ok)
The build system is written in JSON (which I have no idea about :))
---Edit---
My problem is that Sublime Text is only compiling the file that I'm writing on, and not the #includes that I made, f.e. #include "organiser.h". The build system that I'm using is a custom one that I wrote based on some of them found on the Internet (that's why I thought I've done something wrong). Now I'm trying the "make" build system and I have no idea how it works(it says: "no target specified and no makefile found. Stop.).
Thanks for you attention :)
Thanks to Andrew! You can make his build run with the code:
{
"cmd" : ["g++ *.cpp -o $file_base_name &./$file_base_name"],
"working_dir": "$file_path",
"shell":true
}
As sugested I got myself into makefiles and got one of them working, it's building the project and doing what I wanted. Needed to reinstall the mingw tho, to get one with msys2 so make could work correctly.
Thanks for the help
This is answered here: https://stackoverflow.com/a/21956602/1599699
{
"cmd" : ["g++ *.cpp -o executablename"],
"shell":true
}
or, as I mentioned in the comments:
"Use "cmd": ["g++ *.cpp -o test && gnome-terminal -- './test'"] (or equivalent terminal program like konsole or xterm instead of gnome-terminal) to then run it. I was using gnome-terminal -x sh -c './test', but then it said to just use -- to terminate the options and put the command afterwards."

Why does "./a.out" only run the last compiled executable program and not the previous ones?

I have taken my first two courses in java and now have to take Data Structures in C++. I'm trying to open up the different files I've compiled.
They're just two "Hello, world!" programs with slightly different text.
When I type:
g++ HelloWorld.cpp
The file "a.out" is created which I run by typing ./a.out into the command prompt.
Now that I compiled a second executable program, HelloWorldII.cpp, the a.out file only runs that program.
When I try to run ./HelloWorld.cpp I got permission denied, so I typed in:
sudo chmod 744 /Users/username/HelloWorld
to make me the owner of the folder which didn't work so I typed in:
sudo chown -R "$garyjones:" users/username/HelloWorld
to give the owner(me) permission to edit/open, after which when I attempted to run ./HelloWorld.cpp, terminal began to attempt executing it but instead showed me syntax error even though compiling them with g++ HelloWorld.cpp and running them with their a.out files worked fine.
If I have multiple executable files in a folder, how can I open the one I want?
When you compile and link code, the executable is by default named a.out - you are supposed to add a parameter to the linking to name it.
If you don’t do it, they are all going to be named a.out, and of course overwrite each other - there can be only one file with that name.
It is strange for you attempting to execute a pure ascii text file. You don't understand the execution mode's meaning. You are not familiar with g++. It seems like you don't know the process of how an executable file generated by compiler from source file.
And here is my advice.
Google the basic usage of g++ or some other compilers. Choose your favourite.
Here is the temporary solution.
g++ <source file> -o <executable filename>
Example:
## if you don't specific a name for the binary file,
## it will generate a.out and delete the exiting a.out firstly
g++ HelloWorld.cpp -o HelloWorld
## and then you can execute it
./HelloWorld
Figure out what the exact meaning of file permission.
The a.out filename has traditional reason and I recommend you to explore it. It's funny.

Creating a bash script to compile a c++

I have a program I've written in c++ which outputs some simulation results to a .csv excel file.
According to some instructions I need to create a simple bash script that would run the .cpp file given the command "$ run_program" ($ is not a part of the command).
I've looked on Stackoverflow and other sites however I have not found a concrete answer to help me. I would also greatly appreciate it if those who answer can take some time to explain what the parameters mean.
Thank you.
How I should make a bash script to run a C++ program?
This is one of the links I've looked at, however I could not make heads or tails out of this.
i dont know the command you are using to compile your c++ program but this might help you.
Create a file with ".sh" extension and open it with your favorite text editor.
Paste this code (change compiling line with line you are using to compile your progam)
#!/bin/bash
#Run this in terminal
#+ Command to compile c++ program. here i used common one
g++ filename.cpp -o anyname
exit 0
Now you need to run this script, To do this open a terminal
chmod u+x scriptname.sh
Then run the script by ./scriptname.sh
Hopefully this will compile your program.
It sounds like a Makefile is what you are looking for here. Definitely worth getting a handle on if you deal with programming.

How can I compile and run C/C++ code in a Unix console or Mac terminal?

How can I compile/run C or C++ code in a Unix console or a Mac terminal?
If it is a simple single-source program,
make foo
where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the extension.
Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATH to find executables, and most often that does not include the current directory (.).
So to run the built executable foo:
./foo
gcc main.cpp -o main.out
./main.out
This is the command that works on all Unix machines... I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.
g++ -o lab21 iterative.cpp
-o is the letter O, not zero
lab21 will be your executable file
iterative.cpp is your C++ file
After you run that command, type the following in the terminal to run your program:
./lab21
Two steps for me:
First:
make foo
Then:
./foo
All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.
You can display this path in the terminal with this command:
echo $PATH
On Mac OS X (by default) this will display the following colon separated search path:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
So any executable in the listed directories can by run just by typing in their name. For example:
cat mytextfile.txt
This runs /bin/cat and displays mytextfile.txt to the terminal.
To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:
/Users/oliver/MyProgram
If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgram was in the directory /Users/oliver/MyProject I and I was in my home directory I can qualify the executable name like this, and have it execute:
MyProject/MyProgram
Or say I was in the directory /Users/oliver/MyProject2 and I wanted to execute /Users/oliver/MyProject/MyProgram I can use a relative path like this, to execute it:
../MyProject/MyProgram
Similarly if I am in the same directory as MyProgram I need to use a "current directory" relative path. The current directory you are in is the period character followed by a slash. For example:
./MyProgram
To determine which directory you are currently in use the pwd command.
If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a "bin" directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.
This can be does easily by either creating or editing the existing .bash_profile file in your home directory and adding the lines:
#!/bin/sh
export PATH=$PATH:~/bin
Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn't already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in ~/bin to be used in preference to system executables your should reorder the export statement as follows:
export PATH=~/bin:$PATH
Do all of this in "Terminal".
To use the G++ compiler, you need to do this:
Navigate to the directory in which you stored the *.cpp file.
cd ~/programs/myprograms/
(the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)
Compile it
g++ input.cpp -o output.bin (output.bin can be anything with any extension, really. Extension .bin is just common on Unix.)
There should be nothing returned if it was successful, and that is okay. Generally you get returns on failures.
However, if you type ls, you will see the list of files in the same directory. For example, you would see the other folders, input.cpp and output.bin
From inside the directory, now execute it with ./outbut.bin
A compact way to go about doing that could be:
make foo && ./$_
It is nice to have a one-liner so you can just rerun your executable again easily.
Assuming the current directory is not in the path, the syntax is ./[name of the program].
For example ./a.out
To compile C or C++ programs, there is a common command:
make filename
./filename
make will build your source file into an executable file with the same name. But if you want to use the standard way, You could use the gcc compiler to build C programs and g++ for C++.
For C:
gcc filename.c
./a.out
For C++:
g++ filename.cpp
./a.out
Add the following to get the best warnings, and you will not regret it. If you can, compile using WISE (warning is error).
- Wall -pedantic -Weffc++ -Werror
Step 1 - create a cpp file using the command
touch test.cpp
Step 2 - Run this command
g++ test.cpp
Step 3 - Run your cpp file
./a.out
I am on a new MacBook Pro with the Apple M1 Pro chip. I have my Xcode installed - both IDE and command line tools. This is how it worked for me:
g++ one.cpp -o one
./one
Use a makefile. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.
Read the make manual. It seems quite long at first glance, but most sections you can just skim over. All in all, it took me a few hours and made me much more productive.
I found this link with directions:
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
Basically you do:
gcc hello.c
./a.out (or with the output file of the first command)
In order to compile and run C++ source code from a Mac terminal, one needs to do the following:
If the path of .cpp file is somePath/fileName.cpp, first go the directory with path somePath
To compile fileName.cpp, type c++ fileName.cpp -o fileName
To run the program, type ./fileName
Just enter in the directory in which your .c/.cpp file is.
For compiling and running C code.
gcc filename.c
./a.out filename.c
For compiling and running C++ code.
g++ filename.cpp
./a.out filename.cpp
You need to go into the folder where you have saved your file.
To compile the code: gcc fileName
You can also use the g++ fileName
This will compile your code and create a binary.
Now look for the binary in the same folder and run it.
For running C++ files, run the below command, assuming the file name is "main.cpp".
Compile to make an object file from C++ file.
g++ -c main.cpp -o main.o
Since #include <conio.h> is not supported on macOS, we should use its alternative which is supported on Mac. That is #include <curses.h>. Now the object file needs to be converted to an executable file. To use file curses.h, we have to use library -lcurses.
g++ -o main main.o -lcurses
Now run the executable.
./main
Running a .C file using the terminal is a two-step process.
The first step is to type gcc in the terminal and drop the .C file to the terminal, and then press Enter:
gcc /Desktop/test.c
In the second step, run the following command:
~/a.out