user8372510
user8372510

Reputation:

Why I got "clang: error: cannot specify -o when generating multiple output files"?

I am a newbie in C. I have two simple source code files f1.c and f2.c.

f1.c looks like:

#include <stdio.h>
#include "f.h"

void f1(void) {
    // some code ...
}

function f2() in f2.c relies on f1() in f1.c.

#include <stdio.h>
#include "f.h"

void f2(void) {
    f1();
}

f1.c and f2.c share a same header f.h,

void f1(void);
void f2(void);

There are no main() access, I just want to compile these two file into a .o file without linker (using -c option),

gcc -c f1.c f2.c -o f2.o

then I got,

clang: error: cannot specify -o when generating multiple output files

but when I mentioned only f2.c, it works well,

gcc -c f2.c -o f2.o

So what's the problem? Thanks!

Upvotes: 4

Views: 3914

Answers (1)

Christian Gibbons
Christian Gibbons

Reputation: 4370

You should look into the compilation process for C. The first stage is compiling the .c source code into .o object files. The .c files do not need to see the other .c files; they are accepting as fact what you've told them about the existence of external functions. It's not until the linker comes in that it really needs to see the function because the implementation details don't matter to your .c file, just the interface, which you've presumably given it in the header.

What you can do, if you like, is drop the -o flag specifying the output file you want to create. Just compile with gcc -c f1.c f2.c and it will know to create f1.o and f2.o which will be able to link against each other when the time comes that you do want to go through with the linking process.

I am curious, however, what your intentions may be for wanting to compile these without linking. I only ask as you refer to yourself as a newbie, so I am wondering if maybe there is an end goal you have in mind and perhaps aren't asking the right question.

Upvotes: 1

Related Questions