我在使用 gcc 编译 C 中的单独模块时遇到问题:
基本上,我有我的 header ,common.h:
#ifndef COMMON_M
#include<stdio.h>
#define COMMON_M
void print_array(int *a,int n);
void swap(int *v,int i,int j);
#endif
我的functions.c,其中包含上述函数的实现:
#include "common.h"
void print_array(int *a,int n) {
int i=0;
for(;i<n;++i)
printf( (i==n-1) ? "%d\n" : "%d ", a[i]);
}
void swap(int *v,int i,int j) {
int t=v[i];
v[i]=v[j];
v[j]=t;
}
和我的主要:
#include "common.h"
void main() {
int a[10]={6,2,0,12,4,3,7,-4,12,7};
print_array(a,10);
qsort_c(a,0,10);
print_array(a,10);
}
如果我尝试将 funcionts.c 编译为模块( gcc -c functions.c ),我会收到此错误:
错误:重新定义“交换” 错误:重新定义“print_array”
有人能帮我理解为什么吗? 我在 common.h 中声明一个函数并在functions.c 中定义它!我不明白为什么这在理论上不起作用。
提前致谢,
最佳答案
从上面的代码中,我看到的是:您包含了 common.h
两次。一个在 myfunction.c
中,另一个在您的文件中,其中包含 main()。
关于c - 如果在头文件中声明函数,C 中的单独编译会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686705/