设置
如果我有这样的程序
一个头文件,它声明了我的主库函数,primary()
并定义了一个简短的简单帮助函数,helper()
.
/* primary_header.h */
#ifndef _PRIMARY_HEADER_H
#define _PRIMARY_HEADER_H
#include <stdio.h>
/* Forward declare the primary workhorse function */
void primary();
/* Also define a helper function */
void helper()
{
printf("I'm a helper function and I helped!\n");
}
#endif /* _PRIMARY_HEADER_H */
定义它的主要功能的实现文件。
/* primary_impl.c */
#include "primary_header.h"
#include <stdio.h>
/* Define the primary workhorse function */
void primary()
{
/* do the main work */
printf("I'm the primary function, I'm doin' work.\n");
/* also get some help from the helper function */
helper();
}
main()
通过调用测试代码的文件primary()
/* main.c */
#include "primary_header.h"
int main()
{
/* just call the primary function */
primary();
}
问题
使用
gcc main.c primary_impl.c
不链接,因为该primary_header.h
文件被包含两次,因此该函数存在非法的双重定义helper()
。什么是构造这个项目的源代码的正确方法,这样就不会发生双重定义?