Solved how can I use cc -o on two files?

I have two files from a book I'm trying to compile. One is the main file usehotel.c. The second is the function definitions for a header file, hotel.c. I've tried the -o option different ways and I can't get it to work. I don't want the file names to change to a.out after compilation.

I've tried compiling them separately with -c -o but when I cc hotel usehotel after it works but when I ./usehotel I get permission denied.
 
Last edited by a moderator:
Shouldn't that be cc -o usehotel hotel.c usehotel.c? To compile separately, you'd use
Code:
cc -c hotel.c
cc -c usehotel.c
cc -o usehotel hotel.o usehotel.o
 
Shouldn't that be cc -o usehotel hotel.c usehotel.c? To compile separately, you'd use
Code:
cc -c hotel.c
cc -c usehotel.c
cc -o usehotel hotel.o usehotel.o
Thanks. Are the object files not displayed by ls?
 
Last edited by a moderator:
Object files should be displayed normally by ls. But if you refer to the first way (compiling one or more source files to one executable), cc doesn't save the object files.
 
You're welcome.
To elaborate a bit, in the most simple form, cc will process one or more source files and link to a single executable binary as output. Traditionally, the executable is named a.out, but this is often changed using -o <output-file>.
There are several switches for controlling the compilation process in the sense that it stops prior to linking. The most commonly used one is -c, instructing the compiler to not attempt to link, leaving the object file (named according to the source with extension .o). This is useful when building larger programs, where one doesn't want to compile all sources after a change. Instead only affected sources are recompiled, and are then linked with other object files in a separate step. Generally make or a similar facility is used to do this automatically.
Somewhat similar are -S and -E to request assembly language or preprocessor output, respectively. The latter can occasionally be helpful for tracking down problems with macros. In that case, output goes to the console by default (-o may be applied, though).
 
Back
Top