Determining C executable name

CGccCompilationExecutableLinkage

C Problem Overview


When we are compiling a C program the output is stored in a.out. How can we redirect the compiled output to another file?

C Solutions


Solution 1 - C

Most C compilers provide an option for this, such as the -o option for gcc and some others:

gcc -o gentext gentext.c
cc  -o mainprog -Llib -lmymath firstbit.c secondbit.o
xlc -o coredump coredump.c

Solution 2 - C

-ofilename will make filename instead of a.out.

Solution 3 - C

According to the manual:

-o <file>  Place the output into <file>

Solution 4 - C

In Unix, where C originated from, C programs are usually compiled module-by-module, and then the compiled modules are linked into an executable. For a project that consists of modules foo.c and bar.c, the commands would be like this:

cc -c foo.c
cc -c bar.c
cc -o myprog foo.o bar.o

(With -c, the output filename becomes the source file with the suffix replaced with .o.)

This allows you to also re-compile only those modules that have changed, which can be a big time saver for big programs, but can also become pretty tricky. (This part is usually automated using make.)

For a single-module program there's not really any point in first compiling to a .o file, and then linking, so a single command suffices:

cc -o foo foo.c

For single-module programs, it is customary to call the resulting executable program the same as the C source file without the .c suffix. For multi-module programs, there is no hard custom on whether the output is named after the file with the main function or not, so you're free to invent whatever strikes your fancy.

Solution 5 - C

With the -o option.

gcc main.c -o myCoolExecutable.o

This is ok if your program consists of a single file. If you have more files I suggest using make: create a Makefile and then run the command make.

A Makefile is a file containing some rules for compilation. An example can be the following (# means the line is a comment):

CXX = gcc

#CXXFLAGS = -std=c++11
#INC_PATH = ...
#LIBS = ...

SOURCEDIR := yourSourceFolder
SOURCES := $(wildcard $(SOURCEDIR)/*.c)
OBJDIR=$(SOURCEDIR)/obj

OBJECTS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.o, $(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.d, $(SOURCES))

# ADD MORE WARNINGS!
WARNING := -Wall -Wextra

# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean

# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: yourExecutableName

clean:
	$(RM) $(OBJECTS) $(DEPENDS) yourExecutableName

# Linking the executable from the object files
# $^   # "src.c src.h" (all prerequisites)
yourExecutableName: $(OBJECTS)
	$(CXX) $(WARNING) $^ -o $@
	#$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)

-include $(DEPENDS)

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
	$(CXX) $(WARNING) -MMD -MP -c $< -o $@

Shortly CXX variable defines your compiler (gcc, g++), with CXXFLAGS you can define flags for your compilation (i.e. -std=c++11). Then you can include and define custom (INC_PATH and LIBS: not set in the example). With SOURCEDIR you can specify your source code directory (where *.c files are).Then SOURCES is basically telling that the source files for the compilation are all the files having extension *.c.

The Makefile contains a set of rules whose structure is the following:

output: inputs
    commandToExecute

The rule to generate your executable file is

yourExecutableName: $(OBJECTS)
	$(CXX) $(WARNING) $^ -o $@

which is equivalent to gcc -Wall -Wextra $(OBJECTS) -o yourExecutableName.

$(OBJECTS) are the object file resulting from the compilation. When the above rule is executed, if they are not found make will continue scanning the file to find a rule to generate them. In this case the rule to generate these files is:

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
	$(CXX) $(WARNING) -MMD -MP -c $< -o $@

If further information is needed let me know.

Solution 6 - C

If foo will be your executable and bar.c is your source file then the command is:

gcc -o foo bar.c

Solution 7 - C

Compile using:

cc -o <opfilename> <filename.c>

Execute using:

./<opfilename>

Solution 8 - C

The format of giving the Name of .exe file according to the User Choice in C Language

step 1 :- Run the gcc (or the compiler you have) in the below format on the Terminal

    gcc -o put_your_name_you_want_to_give (space) your_file_name_you_want_to_execute

NB:- If you are Running "Vs Code" Use the 'Tab' key for the Auto completion.

step 2 :- Write down the name of the program in format

           .\the_name_you_have_given.exe

you are done!

Solution 9 - C

Assuming you are in ubuntu

step-1: run gcc with these commands to compile filename.c

gcc filename.c -o filename.out

  • filename.out will be created, (it might or might not be shown where the other files are stored)

step-2: execute the filename.out by

./filename.out

step-3: wait for the output

thats it , you are done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarunView Question on Stackoverflow
Solution 1 - CpaxdiabloView Answer on Stackoverflow
Solution 2 - CDaniel A. WhiteView Answer on Stackoverflow
Solution 3 - CArkaitz JimenezView Answer on Stackoverflow
Solution 4 - Cuser25148View Answer on Stackoverflow
Solution 5 - CFrancesco BoiView Answer on Stackoverflow
Solution 6 - CAlbert DaviesView Answer on Stackoverflow
Solution 7 - CKJ SudarshanView Answer on Stackoverflow
Solution 8 - Cshubhajit22View Answer on Stackoverflow
Solution 9 - CRohan DevakiView Answer on Stackoverflow