2

By default, when running gcc test.c, gcc will output the executable as a.exe. Is there a way to globally override this so that gcc will create test.exe?

This assumes that there's only one input file - otherwise gcc can choose to use a.exe, or perhaps to use the first file's name.

  • That's usually done with makefile rules. – Thomas Dickey May 14 '16 at 0:10
  • @ThomasDickey I want it to be system wide, not project specific. – Mark May 14 '16 at 0:11
  • 1
    Other than using make (with or without a Makefile) as mentioned by @ThomasDickey, the obvious answer is to use gcc's -o option: gcc test.c -o test.exe. Using make is a much better choice, though. – cas May 14 '16 at 4:17
6

That's usually done with makefile rules, e.g., given test.c, you would be able to do this (even without having a makefile):

make test

Likewise, there are predefined rules for g++, given test2.cc, you could do the same thing:

make test2

But in either case, you probably need libraries. Doing that is what makefiles are good for (in addition to providing ways of compiling multiple objects).

gcc by itself doesn't have an option to simulate makefile rules. If you want a tool which does that, you could make a shell script which does the special case mentioned, e.g.,

#!/bin/sh
for name in "$@"
do
    case "$name" in
    *.c)
        gcc -o $(basename "$name") "$name"
        ;;
    *.cc)
        g++ -o $(basename "$name") "$name"
        ;;
    *)
        echo "ignoring $name, since it is not one of my files!"
        ;;
    esac
done

But (see the beginning of the answer), the script merely makes explicit the feature that the predefined rules for make provide you. On the other hand, you could modify the script to add necessary libraries, and do that in one place (rather than a set of makefiles, which seems to be the issue).

By the way: whether gcc automatically provides a ".exe" suffix (when you do not) depends upon the platform and the compiler configuration. The suffix is not useful on Unix-like platforms and is generally optional on related things such as Cygwin.

  • I appreciate the answer but as I mentioned in my comment, makefiles aren't a solution for what I'm looking for - I want a system wide solutions and yours is project specific (i.e. I have to define a separate makefile for each project). – Mark May 14 '16 at 0:22
  • 2
    @Mark no you don't. There are default rules for make. If you have a file widget.c then try make widget. You can even specify the default set of libraries to be included in the link stage by specifying them in the environment variable LDLIBS (e.g. export LDLIBS='-lm -lterminfo') – roaima May 14 '16 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.