fishermanZzhang 2015-06-06 09:39 采纳率: 33.3%
浏览 8937

error LNK2019: 无法解析的外部符号 _add,该符号在函数 _main 中被引用

如果把所有的.cpp文件改成.c,程序正常运行。我认为加上extern "C"应该可以正常运行了,但是不行,求解 原理。

student.h

 struct Student
{
    char name[30];

    int score;

    struct Student *next;
};
typedef struct Student Stu;
typedef struct Student * Pstu;

#ifdef __cplusplus
extern "C" {
#endif
     void add(Stu** head, char* name, int score);
     void showall(Pstu head);
     Pstu reverse(Pstu head);
     int getNum(Pstu head);
     Pstu getStu(Pstu head, int score);
     int change(Pstu head, int oldscore, int newscore);
     void freeall(Pstu head);
#ifdef __cplusplus
}
#endif

Student.cpp

 #include"student.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#ifdef __cplusplus
extern "C"{
#endif
    void add(Stu** head, char* name, int score)
    {
        Pstu p1 = *head;
        Pstu p2 = (Pstu)malloc(sizeof(Stu));
        strcpy(p2->name, name);
        p2->score = score;
        p2->next = p1;
        *head = p2;
    }
    void showall(Pstu head)
    {
        Pstu p = head;
        while (p)
        {
            printf("%s,%d", p->name, p->score);
            p = p->next;
        }
    }
#ifdef __cplusplus
}
#endif

main.cpp

 #include"student.h"
#include<stdio.h>
#include<stdlib.h>

int main()
{

    Pstu head = NULL;
    add(&head, "zz", 90);
    add(&head, "ymt", 100);
    //showall(head);
    return 0;
}
  • 写回答

4条回答

  • oyljerry 2015-06-06 11:55
    关注

    等于cpp调用c函数,调用约定有区别。

    评论

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL