I'M wanting to write my own command/script for compiling my c++ files. I use g++ -g -Wall filename.cpp -o filename

I'M trying to figure out how I can trip of the last four characters (.cpp) from a string. Any ideas?

Dani AI

Generated

Building on the earlier suggestions from , and , here’s a small, practical single-file wrapper that avoids hard‑coding names and handles a few real-world edge cases (paths, spaces, filenames that start with a dot, forwarding extra g++ flags). It strips only the final extension (so foo.bar.cpp -> foo.bar) and protects against filenames that look like options.

#!/usr/bin/env bash
set -euo pipefail

if [[ $# -lt 1 ]]; then
  printf 'Usage: %s file.cpp [g++-options]\n' "${0##*/}" >&2
  exit 2
fi

src="$1"; shift
file_only="${src##*/}"

if [[ "$file_only" == .* && "${file_only#*.}" != *.* ]]; then
  base="$file_only"
else
  base="${file_only%.*}"
fi

out="./$base"
g++ -g -Wall -- "$src" -o "$out" "$@"

Notes and troubleshooting

  • Quote variables and pass -- before positional filenames so filenames starting with - aren’t treated as options.
  • The script removes the last “.something”; if you only want to remove .cpp (and nothing else), check the suffix first and only strip when it matches.
  • Hidden files (names that start with . and have no other dot) are left unchanged by the script to avoid producing an empty output name.
  • Make the script executable (chmod +x compile) and put it in your PATH for convenience. For multi-file projects or dependency tracking, the Makefile approach recommended earlier is still the better long-term solution.

Recommended Answers

All 4 Replies

Take a look at the make command, which is made specifically for this kind of thing. Just create a Makefile in the same directory as the source file(s). You can even try this without a Makefile and make MyPrg will compile the file based on the defaults.

#Makefile
MyPrg:
        g++ -O3 -Wall MyPrg.cpp -o MyPrg
        strip MyPrg

debug:  MyPrg.cpp
        g++ -g -Wall MyPrg.cpp -o MyPrg
        touch debug

clean:
        -rm debug MyPrg

To use this Makefile to generate MyPrg, run the make command.
~/src/cpp/MyPrg> make
This will use the first rule to make MyPrg from the given source file. If MyPrg.cpp changes, then calling make will recompile MyPrg.
~/src/cpp/MyPrg> make debug
This will use the debug: rule to create MyPrg to make that version of the program.
~/src/cpp/MyPrg> make clean
This will remove the files generated using this Makefile.

I was trying to avoid hard coding MyPrg.cpp and MyPrg. I was going to use ${1} for command line arguments. So I would simply type "compile MyPrg.cpp" and my script would run g++ -g -Wall ${1} -o ${1 - last 4 characters}

hi,

v="myFile.cpp"
((${#v}>4)) && echo "${v:: -4}"

or

v="myFile.cpp"
echo "${v%.cpp}"
commented: I like the second version better. This is a good solution for this question. +3

Also, check out the basename command

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.