Executable UNIX cc

When I compile a file .cusing a command cc, it creates an executable file a.out. I noticed that it creates a file a.outinside my current directory. Is there a way to create a file a.outin the same directory as the file .c, wherever I am on the system?

for example, if my current path ~/desktopand I type the command:

cc path/to/my/file/example.c

It creates a file a.outin a directory ~/desktop. I would like it to create a file a.outinpath/to/my/file/a.out

+3
source share
4 answers

You will need to use the -o switch each time you call cc:

cc -o path/to/my/file/a.out path/to/my/file/example.c

or you can do a wrapper script like this:

mycc

#!/bin/bash

dirname=`dirname "$1"`
#enquoted both input and output filenames to make it work with files that include spaces in their names.
cmd="cc -o \"$dirname/a.out\" \"$1\""

eval $cmd

./mycc path/to/my/file/example.c

, ,

cc -o "path/to/my/file/a.out" path/to/my/file/example.c

, mycc $PATH, :

mycc path/to/my/file/example.c
+5

"-o" . :

cc path/to/my/file/example.c -o path/to/my/file/a.out
+2

, -o.

cc path/to/my/file/example.c -o path/to/my/file/a.out
+1

, , , -o siwtch :

cc -o /a/dir/output b/dir/input.c

, , (), , bash : ( , , , )

i = "a/path/to/a/file.c" cc -o ${i%.c} $i

, i, , .c-suffix.

+1

Source: https://habr.com/ru/post/1762643/


All Articles