多个项目中包含相同的头文件:C vs C ++

问题描述 投票:0回答:2

这里有一个带有两个源文件和一个头文件的示例项目,如下:

main.c:

#include<stdio.h>                                                               
#include "personal.h"                                                           
int main(){                                                                     
    i = 5;                                                                        
    printf("Value is %d\n",i);                                                    
    return 0;                                                                     
 }

sub.c:

#include "personal.h"                                                           
// do nothing

最后是personal.h:

#pragma once                                                                    
int i; 

。c文件中的每个文件都包含personal.h,该文件被“保护”。我用gcc编译,一切正常:

>gcc sub.c main.c -o out
>./out 
Value is 5

但是对于g ++,会发生这种情况:

>g++ sub.c main.c -o out
/tmp/cctYwVnO.o:(.bss+0x0): multiple definition of `i'
/tmp/ccPElZ27.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status

C ++和C之间在文件链接方式,预处理程序活动等方面有根本不同吗?我对其他编译器(如clang)尝试了相同的操作,并且发生了相同的情况。我也许在这里想念一些愚蠢的东西。

c++ c compilation include preprocessor
2个回答
2
投票

在C中,

int i;

是一个临时定义。 C标准允许在多个编译单元中具有暂定定义,但没有强制要求。 Unix C编译器的自定义行为是允许它,但是gcc有一个选项(-fno-common)阻止它并在链接时生成错误。

IIRC,C ++没有这样的津贴。


1
投票

sub.c将包括personal.h并将在全局范围内创建变量i。同样,main.c也将包括personal.h并在全局范围内创建变量i。最终,当您链接时,在全局范围中存在i的两个定义,因此也有错误。

© www.soinside.com 2019 - 2024. All rights reserved.