g++ permission denied(Mac) - c++

I tried to run a c++ file with the mac terminal, but it gave me the error as shown below:
Does anyone have any idea why this is happening?

If you do this gcc test.cpp you will get executable called a.out, and to run it you type:
./a.out
to get executable called test you have to run g++ test.cpp -o test now you can run this:
./test

Related

./a.out on visual studio code on windows wont work - gives a 'look for an app in the Microsoft Store' prompt

I am trying to run a simple hello world code of CPP. Nothing fancy. Just a couple of header files with a basic hello world class.
I have the following sequence of commands.
g++ -c HelloWorld.cpp -o HelloWorld.o
g++ -c main.cpp -o main.o
g++ HelloWorld.o main.o -o a.out
All goes well. Then, I do the next logical step to run the program.
./a.out
Instead of giving me the output in the terminal, I get the following prompt. I apologize for uploading a mobile phone taken photo. I try to screen grab, the prompt goes away.
So, how do I get the output in the terminal as expected?
Note 1 : I am already able to get this working on Mac OS. So, I know the code works fine, so does visual studio code. This must be some windows issue.
[with help from comments from #MarkTolonen above]
g++ -c HelloWorld.cpp -o HelloWorld.exe
g++ -c main.cpp -o main.exe
g++ HelloWorld.exe main.exe -o a.exe
./a.exe
The above worked for me.

How do I execute my C++ program using the Mac terminal?

I'm a begginer in programming and new to everything.
I am trying to use the Mac terminal to run my C++ program. My file name is Learning and my executable file is main.cpp.
I've tried:
$ g++ -o Learning main.cpp
$ ./main
But when I command the first line it returns:
clang: error: no such file or directory: 'main.cpp'
clang: error: no input files
This gives me the error message so I tried another way:
$ g++ main.cpp
$ ./a.out main.cpp
But it still gives me the same error message. I'm lost.
Does anyone have any anwsers?
P.S. Thanks for taking the time to read this and potentially help me.

Trying to compile .cpp with g++ for 64 bit windows

First I tried just downloading what you get after searching mingw64 windows. That didn't work. While searching for a solution I came across this, where the answer includes what seems to be a legit version of mingw64.
This being probably the third or fourth mingw64 I've downloaded, I was happy to see a g++64.exe which I assumed would take care of everything. It doesn't, after compiling with g++64 -o hello.exe -c hello.cpp and running hello I get an error saying This version of [...]\hello.exe is not compatible[...].
What am I doing wrong? I've tried -m64. Is there some additional setting I need to change? Should I post what I get for g++64 -v?
Your command is wrong, you're not creating a .exe file, but an object file that you need to link to produce an executable. Do it like this:
g++64 -o hello.exe hello.cpp
The -c argument tells the compiler to just compile but not link your code. You can do the above in 2 steps, compile and link:
g++64 -c -o hello.o hello.cpp
g++64 -o hello.exe hello.o

Debugger error: not in executable format: File format not recognized

I've written a program, saved it on the desktop under the name 'Swap.cpp' and when I run gdb (the first time), I get the error:
"/Users/myname/Desktop/Swap": not in executable format: File format
not recognized.
I have no idea what I'm doing wrong. Any help will be appreciated.
Sorry I should've given more information:
I am using Mac OS.
I've already compiled the program and have the Swap.o file that I can see on my desktop.And here are the commands that I enter while trying to run the debugger from bash:
$ clang++ -g Swap.cpp -o Swap
$ ./Swap
this runs Swap and then I try to access the debugger using:
$ gdb Swap
that then gives me the aforesaid message. I tried doing what Rakholiya Jenish suggested but to no avail.
To run gdb on windows:
path_to_gdb.exe program_to_debug
If the compilation was not proper, either compile it with your IDE (if you are using) or with g++ using cmd.exe as:
g++ -g Swap.cpp -o output -lm
I figured out how to use lldb instead of gdb. lldb works just fine. Here is what I did:
$ clang++ -g -o Swap Swap.cpp
$ lldb Swap
Thank you all for your help.

C++ programs, compiling with g++

I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.
I have source file in test.cpp.
To compile this, I did
(1)
g++ -c test.cpp
g++ -o test test.o
./test
Everything works fine.
But when I did compling and linking in same stage, like this
(2)
g++ test.cpp -o test
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work
In my last case, test is generated but is no more executable; but in my guess it should work fine.
So, what is wrong or do I need to change some settings/configuration ??
I am using g++ 4.3.3
Thanks.
When you say:
g++ -c test.cpp -o test
The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.
Basically, don't do that.
You are forcing compiler to produce an object file and name it like an executable.
Essentially your last line tells: compile this to an object file, but name it test, instead of test.obj.
-c flag means Compile Only
Try
g++ -o test test.cpp
Specifying -o in the g++ command line tells the compiler what name to give the output file. When you tried to do it all in one line, you just told the compiler to compile test.cpp as an object file named test, and no linking was done.
Have a look at the fabulous online manual for GCC for more details.
from the gcc manual:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
You must link the compiled object files to get the executable file.
More info about compiling and linking and stuff is here.
Read man g++. The switch -c is to compile only but not to link.
g++ -c test.cpp -o test
does what
g++ -c test.cpp
does but the object file will be test istead of the default name test.o. An object file cannot be executed.