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

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?

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

Got "-std=c++11: command not found" error when trying to compile a object file using g++

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.

xtables_find_match in g++ does not work but it is fine in gcc

Iptables is a c-implemented lib.
My project is based on C++, so I create a new header file h1.h, which will be included in my c++ file through 'extern "c"{#include h1.h}'.
Many iptables-lib functions work fine in my c++ project, except xtables_find_match() in do_command().
==============================
gcc -c -std=c99 -o file1.o file1.c
g++ -c -std=c++0x -o file2.o file2.cpp
g++ -o myapp file1.o file2.o
Functions like xtables_init_all and init_extensions should be called first.
otherwise load_extension will exit silently.
xtables_find_match will call load_extension.

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

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

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