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

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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器