How to change default output file of g++ in ubuntu? - c++

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 ;)

Related

Cannot link the header file using gcc linker in windows

hello.c will use the header file plugin.h. If I want to use gcc to compile, can I know how should I write the -LXXX.
gcc -o hello hello.c -LXXX
Currently my project structure look like this
project directory is (C:\project)
project
examples/ hello.c
include / plugins/ plugin.h
You don't link the header file, you include it. You link object files and static/shared libraries, which are already compiled files.
But to answer your question, you include your plugin with the -I option.
g++ -O2 examples/hello.c -I include/plugins -o hello
Or if you have a library to link say in lib:
g++ -O2 examples/hello.c -I include/plugins -L lib/plugins -lplugin -o hello
Or if you want to do in two steps (notice the -c)
g++ -O2 -c examples/hello.c -I include/plugins -o hello.o
g++ hello.o -L lib/plugins -lplugin -o hello

Make g++ create the -o file same as program name

I want to write an alias or new command that will call g++ and create an executable with the same name as the cpp file
eg
g++ hello.cpp creates ./hello instead of ./a
I use g++ hello.cpp -o hello
Is there some kind of alias g++=" g++ $x.cpp -o $x" that i can create?

How do I link multiple files without the use of a makefile?

For example, I'm given carModels.cpp, carModels.h, carType.in, manufacturers.h, manufacturers.o, and lastly my own file tester.cpp. How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)
Compile each source file to its own object file:
g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o
Now link all object files together:
g++ carModels.o tester.o manufacturers.o -o outputname
Consider adding more options like -O3, -std=c++11, -Wall, etc. as needed.
you can do this in two steps, first compile to *.o files,
gcc -c your.cpp other.cpp .....
then link them
gcc -o you_out_put_name the_object_files.o ...
In a single line, that would be just g++ -o tester *.cpp *.o. GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.

Permission denied while compiling my own code

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

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.