Castle_Dust
Castle_Dust

Reputation: 63

how does gcc -c option work?

#include"header.h"

int main(){

    function();
    return 0;
}

above is simplified form of my code. I implemented function() in header.h file, and put it in the same directory with this code.c file.

I heard that "gcc -c code.c" is "compile but no linking" option, but this code need linking with header.h file. So I guess -c option will flag an error, while it didn't. Though, without -c option it flags an error. Can anyone explain how this -c options works?

Upvotes: 1

Views: 794

Answers (2)

user3013807
user3013807

Reputation: 57

gcc -c compiles source files without linking.

header files have nothing to do with linking process, they are only used in compilation process to tell compiler the various declaration and function prototypes.

However it is bad practice to implement function in header file, both compilation strategy should work in this case. i.e. gcc with and without c flag

Upvotes: 1

DoxyLover
DoxyLover

Reputation: 3484

Header files have nothing to do with linking. Linking is combining multiple object files and libraries into an executable.

Header files are processed by the compiler, as part of generating an object file. Therefore, gcc -c will process header files.

Upvotes: 6

Related Questions