g++和gcc编译工具编译文件区别

"本文探讨了如何在C++代码中通过extern "C"声明使函数适用于C编译,并介绍了process和processcplus函数在不同编译条件下的汇编实现。重点讲解了标签的使用和编译时的链接过程。"
摘要由CSDN通过智能技术生成

三个测试文件加一个Makefile文件
mian.c

#include <iostream>
#include "test.h" 

int main(int argc, char *argv[])
{
        int fd; 
        process(fd);
        processcplus(fd);                                                       
}


test.c

#include <stdio.h>
#include "test.h"
#ifdef __cplusplus
extern "C" {
#endif

void ctest();

#ifdef __cplusplus
}
#endif


void ctest()
{
}

void cplustest()
{
}



int processcplus(int fd)
{
    ctest();
    cplustest();
}                                                                               


int process(int fd)
{
    ctest();
    cplustest();
}

test.h

#include <stdio.h>

extern int processcplus(int fd);                                                
#ifdef __cplusplus
extern "C" {
#endif

extern int process(int fd);

#ifdef __cplusplus
}
#endif


Makefile

CC=g++
SRC_DIR = .
target=test
source:= $(wildcard $(SRC_DIR)/*.c)
$(warning $(source))
all: $(target)
$(target): $(source)
        $(CC) -o $@  $^
        $(CC) -S $^

.PHONY:clean
clean:
        rm -f test *.s 

直接编译上述工程,会生成一个可执行文件和两个汇编文件,可执行文件由这两个汇编文件链接生成.
  以下截取汇编文件的片段简单说明编译规则.

#ifdef __cplusplus
extern “C” {
#endif
#ifdef __cplusplus
}
#endif

main:
.LFB1021:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16 
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $32, %rsp
        movl    %edi, -20(%rbp)                                                 
        movq    %rsi, -32(%rbp)
        movl    -4(%rbp), %eax
        movl    %eax, %edi
        call    process
        movl    -4(%rbp), %eax
        movl    %eax, %edi
        call    _Z12processcplusi
        movl    $0, %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFB0:                                                                          
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        nop
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   ctest, .-ctest
        .globl  _Z9cplustestv
        .type   _Z9cplustestv, @function
_Z9cplustestv:
.LFB1:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        nop
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE1:
        .size   _Z9cplustestv, .-_Z9cplustestv
        .globl  _Z12processcplusi
        .type   _Z12processcplusi, @function
_Z12processcplusi:
.LFB2:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16


以上可以得出:
在头文件中加入了申明按c编译的,则process汇编成了process,没有加入标签的processcplus汇编成了_Z12processcplusi.

在当前文件的前面对相应函数进行申明的:
ctest汇编成了ctest
cplustest汇编成了_Z9cplustestv.

所以在c++代码中对于需要使用为c代码编译的函数使用上述标签进行申明即可使用.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值