2
\$\begingroup\$

As I am starting to be tired of reverse-searching in history for the C++ compile command, which I'm using - details on my flags are written here, I defined the following .bash_aliases function:

function compile-cpp {
 if [ -z "1ドル" ]
 then
 {
 echo "Need a file to compile as argument."
 }
 else
 {
 # extract file name from path
 filename=$(basename "1ドル")
 # cut the extension
 filename="${filename%.*}"
 # compile
 g++ 1ドル -std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors -o "$filename"
 }
 fi
}

The goal was to take one cpp file as argument and compile it under the same name as the source's.

So, e.g. when I call:

compile-cpp delete.cpp

I expect it to output a file named as:

delete

In the same directory.

No problems, so far, detected by me, but as I am a C++ beginner, I can't know for sure, if this command will more or less always work.

Note: I don't wish to use a Makefile yet. The projects and various examples I work with are so little and there's so many of them, I believe it would be counterproductive.

I found the following question, probably trying to achieve somewhat similar thing:

Compile and run C++ code

But since I have different demands, looking at his code, I believe it is not its duplicate, in spite of having close to the same titles.

asked Nov 6, 2017 at 12:53
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You could just go:

export CXXFLAGS=-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors

Then at any point you want to compile:

make plop.cpp

This will build the executable plop. Use the CXXFLAGS and does NOT need a makefile.

If you want to keep your function, I would allow people to override the defaults using the environment:

function compile-cpp {
 if [ -z "1ドル" ]
 then
 {
 echo "Need a file to compile as argument."
 }
 else
 {
 # extract file name from path
 filename=$(basename "1ドル")
 # cut the extension
 filename="${filename%.*}"
 # compile
 if [ -z "${VERBOSE}" ]
 then
 {
 # Normally just want to see the file that is being compiled.
 echo ${CXX:-g++} 1ドル -o ${filename}
 }
 else
 {
 # But if things are going wrong then you want to see the full command LINE.
 echo ${CXX:-g++} 1ドル ${CXXFLAGS:-'-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors'} -o "$filename"
 }
 fi
 ${CXX:-g++} 1ドル ${CXXFLAGS:-'-std=c++14 -Wall -Wextra -Werror -Wpedantic -pedantic-errors'} -o "$filename"
 }
 fi
}

Here:

${CXX:-g++} means if the environment variable is empty or null use `g++`
 otherwise use the value of `${CXX}`.
Same for the `${CXXFLAGS}`

I would prefer using the make technique, as this provides you all the facilities of Make in addition to the compiler (so it checks file date stamps) and other stuff.

## Use the fortran compiler for shits and giggles
CXX=fortran compile-cpp plop.cpp
VERBOSE=1 compile-cpp plop.cpp
Toby Speight
87.1k14 gold badges104 silver badges322 bronze badges
answered Nov 6, 2017 at 19:25
\$\endgroup\$
1
  • \$\begingroup\$ A very complete answer! One minor improvement: I suggest redirecting the error message to stderr (>&2). I'm not sure about the verbose logging - stdout or stderr? Perhaps worth seeing what set -x does (and perhaps simply using set -x as the else branch - but it would need to be restored, so perhaps not worth it). \$\endgroup\$ Commented Nov 7, 2017 at 8:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.