我想在一个文件中调用另外一个文件中的函数,如下面是主函数:
#include "stdio.h"
#include "temp.h"
void main()
{
int k,j=10;
k=aa(j);
}
下面为temp.h:
#include "stdio.h"
extern int aa(int i);
下面为temp.c:
#include "temp.h"
int aa(int i){
i=10;
printf("aaaaaa");
return i;
}
为什么编译时总报错:
error LNK2001: unresolved external symbol "int __cdecl aa(int)" (?aa@@YAHH@Z)

解决方案 »

  1.   

    搞不懂你为什么要在temp.h中函数申明前放extern,
    你可以在main()函数中#incluee "temp.cpp"
      

  2.   

    声明用extern "C" extern int aa(int i);
      

  3.   

    你应该在main()函数前面加一个
    extern int aa(int i)
    告诉编译器,这个函数是别的文件中的函数
      

  4.   

    wuchaohq(烈火雄心) :
    在main()前加extern int aa(int i)
    报同样的错。
      

  5.   

    声明用extern "C" extern int aa(int i);
      

  6.   

    我记错了
    应该 在main()前加 yjs_soft所说的
      extern "C" extern int aa(int i);
      

  7.   

    在cpp中调用c中的函数,必须加extern "C" 
    你可以在temp.h中这样声明
    #ifdef __cplusplus
    extern "C" {
    #endifextern int aa(int i);#ifdef __cplusplus
    }
    #endif
      

  8.   

    yjs_soft() :
    请问一下在Turbo C下怎么写呢?
      

  9.   

    afc(afc):
    请问一下在Turbo C下怎么写呢?
      

  10.   

    turbo c++下用extern "C" 也没有问题,不过注意extern "C"只能出现在cpp文件中
      

  11.   

    extern "c"
    {
    #include "temp.h"
    }
      

  12.   

    afc(afc) :
    不行,加了extern "C" 编译报定义不正常结束,编译器不认得 “C"
      

  13.   

    afc(afc) :
    我的文件是以.c命名。
      

  14.   

    在TC下,你需要
    (1)单独把 temp.c编译成 temp.obj
    (2)把主文件编译成 XX.obj
    (3)手工连接 成 XX.exe或者
    你可以设置TC环境里的 主文件
      

  15.   

    你的程序如果将temp.h和temp.c和起来,程序执行起来并没有错,为什么非要分开写呀!
      

  16.   

    可以根据你的编译错误,main所在的文件是cpp呀,你在这里加入extern "C"
      

  17.   

    iicup(双杯献酒) :谢谢你。
    为什么在Turbo C++下加extern "C" 编译报定义不正常结束,编译器不认得 “C"
      

  18.   

    你是在main函数前声明的extern "C" extern int aa(int i);么?
      

  19.   

    在#include "stdio.h"
    #include "temp.h"
    void main()
    的第三行加入
    #include "temp.c"
    程序也不会出错的!
      

  20.   

    是的,加在main函数声明的前面,但是编译还是报错。
      

  21.   

    200120012001(宇航) :
    但一般写程序时不会把.c文件include 进来的。
      

  22.   

    在main函数中加extern "C" 编译不过。
      

  23.   

    extern "C" int aa(int i);
      

  24.   

    extern "C" extern int aa(int i);
      

  25.   

    在vc++ 下,除了main()外,其他自定义文件应以***.h命名这是我的经验