c&c++函数调用的一个区别

        我们知道,在c语言中,函数的名字是全局可见的。举个例子来说,如果我们在fun.c中引用fun1.c中的一个函数,例如fun1(),哪么,在fun.c中无需声明fun1()便可以直接引用它。代码如下:

fun.h文件

#ifndef _FUN_H_
#define  _FUN_H_

#include
< stdio.h >
#endif

fun.c文件

#inlucde " fun.h "
void  fun()
{
      printf(
"hello");
          fun1();
}

fun1.h

#ifndef _FUN1_H_
#define  _FUN1_H_

#include
< stdio.h >
#endif

fun1.c

#inlucde " fun1.h "
void  fun1()
{
      printf(
" world /n");
}

main.c

int  main( int  argc, char   ** argv)
{
    fun();
    
return 0;
}

编译 gcc  -c *.c   连接并生成可执行文件gcc *.o -o  funTest  运行./funTest 输出为“hello world”

如果将fun.c fun1.c及main.c改为fun.cpp,fun1.cpp以及main.cpp的话,在编译的时候就会出现

main.cpp:errro:"fun" was not decleared in this scope.

fun.cpp:error "fun1" was not decleared in this scope.

说明编译器在编译的时候找不到函数fun()和fun1()。因此,在main。cpp加入fun()声明void fun(),在fun。cpp中加入fun1的声明void fun1();这样便可以编译成功。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值