使用extern keywod在c中导入函数

car*_*sel 0 c import function extern

我在将外部函数导入主 c 文件时遇到问题。

这是我的最小代码:

/* main.c */

#include<stdio.h>
extern int func() 

int main(){
    extern int func();
}

/*external file with one function that I want to     
import*/

#include<stdio.h>

int func(){
    printf("Hello World Again\n");
}
Run Code Online (Sandbox Code Playgroud)

我像这样编译和运行 - gcc main.c然后./a.out但什么也没发生。任何的想法 ?

sim*_*onc 5

你必须编译包含该文件func还

gcc -Wall main.c external_file.c
Run Code Online (Sandbox Code Playgroud)

(请注意,-Wall编译器命令中的 不是绝对必要的,但非常好的做法)

正如其他人所指出的,您还需要修复要调用的代码,func而不仅仅是重新声明它。