-1

I'm writing a program that looks roughly like this:

  • Some main code in src/
  • Some code in Demo1/

I compile Demo1's stuff into .o , then .so, and use that .so in the creation of the main executable. (Demo1's code is used in src/).
Demo1 uses an already complied library lib1 that sits in a separate folder with both its binaries and the source.

That's how I've been building Demo1:

gcc -c -fpic Demo1/src/*.c -Ilib1/include -o Demo1/build/demo1.o  
gcc -shared Demo1/build/demo1.o -Llib1/lib -llib1 -o Demo1/dist/libDemo1.so

Now I've added one new .c file and one new .h to Demo1 and then this happened when I ran the build script (that contains the two command from above, among others):

gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files

What does it mean ? I don't think that an addition of a source file could have caused this..
I've been using -c with -o on the same line in the past of course, and besides that - the last part of the sentence is not the most clear to me grammatically... (does it mean I can't specify -o with {-E when -E has multiple files})?

I'm running gcc 4.8.4 on an Ubuntu 14.04 machine.
Thanks in advance.

  • 1
    What specifically about the man-page of gcc did you not understand? Did you look up the options you use? What they mean? – too honest for this site Aug 24 '16 at 14:21
  • 1
    If you previously had only one source file and now you have two, then yes absolutely that would cause that. What do you think it's trying to do? – inetknght Aug 24 '16 at 14:22

2 Answers2

2

The -o option specifies the (single) output file. Blithely ignoring the options that are not immediately relevant, when you have:

gcc -c file1.c file2.c -o output.o

does the output from file1.c or file2.c go into output.o?

GCC is telling you that you can't do that. Either you have to use:

gcc -c file1.c -o file1.o
gcc -c file2.c -o file2.o

or you have to use:

gcc -c file1.c file2.c

That's what the error message says.

Note that you have source code in a sub-directory. The default output from the compiler will be in the current directory. That is:

gcc -c Demo1/file1.c Demo1/file2.c

will create output files ./file1.o and ./file2.o. AFAIK, there isn't a way to specify the output directory (-o directory-name gives an error like error: unable to open output file 'directory-name': 'Is a directory', for example). Therefore, the separate compilation with the explicit output file is probably the best way for you to work. It is also what build systems such as make would do.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

It is complaining that you try to compile multiple .c files into a single .o file. When you omit the link step, each source file will result in a separate output file.

The default name for the output file is that .c is replaced by .o when the -c (compile only) flag is specified. This will put the intermediate files in the current directory.

Let the compiler name the output files in the compile step and include all object files in the link step:

cd Demo1/build
gcc -c -fpic ../src/*.c -Ilib1/include 
gcc -shared *.o -Llib1/lib -llib1 -o ../dist/libDemo1.so

Another way is to split the compile step into one line for each source file.

As you can see, this isn't very user friendly and will get messy as you create more files.

If I were you I would create a makefile (or learn some newer build system if I wanted spice up my CV). That way you can specify the compilation the way you want it using rules and then build using a single command.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Note that the object files will not be in `Demo1/src`; they'll be in the current directory. If you want the object files in `Demo1/src`, you'll have to compile each file separately with the output file specified (using `-o Demo1/src/whatever.o`). – Jonathan Leffler Aug 24 '16 at 14:49
  • @JonathanLeffler Thanks for reminding me. Been a while since I used multiple directories without the aid of makefiles. And when I see my edited answer it is easy to understand why. – Klas Lindbäck Aug 24 '16 at 15:05