Using this:
g++ -c -Wall -l libuthreads.a test02-new.cc -o test02-new
I'm compiling my code.
While trying to execude the code with : test02-new
I get:
test02-new: Permission denied.
Do you know whats the reason for it?
I used this command before.
If you use the -c flag, GCC outputs an object file. You cannot execute these.
This should work:
$ g++ -Wall libuthreads.a test02-new.cc -o test02-new
$ ./test02-new
Related
As in the title, when I try to compile an object file using g++ by running this shell script:
#!/bin/bash
name=textsweeper
srcdir=src
buildir=build
cc=g++
cppflags=-Wall -std=c++11 -ggdb -O
libs=
rm -f $buildir/$name $buildir/main.o
$cc $cppflags $srcdir/main.cpp -c -o $buildir/main.o
$cc $buildir/main.o $libs -o $buildir/$name
I get the following error:
$ bash compile
compile: line 6: -std=c++11: command not found
And other errors about things being only available only with stdc++11.
I've tried yahooing the error, but I've only got answers to about errors about actual command not arguments.
Variable assignment is space sensitive. Change:
cppflags=-Wall -std=c++11 -ggdb -O
to
cppflags="-Wall -std=c++11 -ggdb -O"
Otherwise, you're trying to run the command -std=c++11 -ggdb -O with the environment including a setting of cppflags=-Wall. bash allows temporary environment settings to be done this way, which is why its important to quote any variable assignments that contain spaces.
I know that you can add the c++ linker with -lstdc++ and I do this, yet I am still getting an error. fatal error: iostream: No such file or directory. Hence, gcc doesn't seem to know where to look for the headers.
What is the best way to proceed here, given that g++ is not an option?
Thanks for the help!
Yes, gcc treats a file with extension .cpp as C++ source:
$ cat test.cpp
#include <iostream>
int c;
$ gcc -c test.cpp
$
You can also explicitly specify the language with -x language:
$ mv test.cpp test.c
$ gcc -c -x c++ test.c
$
But why do you want to do this? You should have g++ available and working. If not, that sounds like an incomplete or botched installation.
I have Jgrasp set up and running fine with c++. I want to use c++11. I get the following compile error:
gift1.cpp:52:24: warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11
Which I attempt to solve by clicking Settings, compiler settings, workspace. I copied my environment settings "g++ - generic" and renamed it "user: g++ - generic." I then clicked edit. I changed the original compile code:
g++ %<CY> %<D> -g %<D> -c -o %<RELE_DEF_BIN_DIR>%<base>.o %<> %<REL_FILE>
to
g++ %<CY> %<D> -g %<D> -c -o -std=c++11 %<RELE_DEF_BIN_DIR>%<base>.o %<> %<REL_FILE>
But it still gives the same error. (I added "-std=c++11".)
Edit: I have changed the string to
g++ -std=c++11 %<CY> %<D> -g %<D> -c -o %<RELE_DEF_BIN_DIR>%<base>.o %<> %<REL_FILE>
but have the same problem. I agree with the comment that this just doesn't appear to be modifying my build commands at all. When I compile, it outputs:
jGRASP exec: g++ -g -o gift1.exe gift1.cpp
Trivially
g++ sample.c
generates a.out
Can g++ be configured to output to a different default name for output file ?
You need to use the -o option of g++
g++ -o output_file_name source.cpp
Use the g++ -o switch: g++ sample.cc -o myoutfile
See a man page for g++
-o file
Place output in file file.
If you want to change the default output name to test for example, all you need to do is go to .bashrc, and put in:
alias g++='g++ -o test'
But you need to reopen a new terminal for it to work.
Man pages are your friends:
$ man g++ (and just do a search for "out" and you're done ;)
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.