简体   繁体   中英

#include other C programs

I need to include file_1.c into main.c. In file_1.c, I currently have multiple functions. If I want to call these functions in main.c, what do I need to do? I have #include"file_1.c" in my main program.

Use standard approach by making header file

#include"file_1.h"

you will have to compile this "file_1.c" together with main.c and make one executable because function calls are need in run time.

Try this :

create a header file file_1.h

#ifndef _FILE_H
#define _FILE_H

void foo(int );
#endif

give all the declaraion of function and struct definitions (if any) or any global variables

then in file_1.c will contain actual defintion of function

//file_1.c

    #include "file_1.h"
    #include <stdio.h>
    void foo(int x)
    {
      printf("%d\t",x);
    }

//main.c
    #include "file_1.h"

    int main()
    {
    int x=10;
    foo(x);
    return 0;
    }

include header file file_1.h in both ( main.c and file_1.c ) the c files

In gcc

gcc -Wall main.c file_1.c -o myexe.out

Why do you think you need to do this?

Normally you would add the declaration of functions in file_1.c into file_1.h and include that in main.c.

When you link the program, you just need to include both main.c and file_1.c (which then includes the definitions of the functions) on the command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM