Compiling multiple C files with gcc

后端 未结 4 1111
予麋鹿
予麋鹿 2020-12-06 00:08

I have two files, main.o and modules.o, and I\'m trying to compile them so that main.o can call functions in modules.o. I

4条回答
  •  执念已碎
    2020-12-06 00:59

    You should define the functions that you want to call from modules.c into main.c into a header file, let us say modules.h, and include that header file in main.c. Once you have the header file, please compile both of the files together: gcc main.c modules.c -o output


    Two additional notes. First, modules.o is an object file and it should not be included in a C source file. Second, we cannot have a C file have a .o extension. You should actually get an error when compiling a .o file. Something like:

    $ cat t.o
    int main() {
        int x = 1;
        return 0;
    }
    $
    $ gcc t.o
    ld: warning: in t.o, file is not of required architecture
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    $
    

提交回复
热议问题