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

问题描述

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;
}

解决方案

如果不是调用已经编译好的c函数,我觉得没有必要使用extern c
参考:什么情况下需要使用externc
http://my.oschina.net/xlplbo/blog/330593

解决方案二:

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

解决方案三:

你的代码在vs2013中编译通过
如果全部改成.c,函数是用c的方式编译,在c的main函数中以c的方式调用应该没什么问题
全部改成.cpp,在main中以cpp的方式调用c函数,不同编译器可能就会出错,这时候不能用#include"student.h",试试用extern "C" int add()可能就会解决问题

解决方案四:

无论是用控制台还是空项目,我都可以编译通过,除了注意那个strcpy的安全性检查error
我觉得控制台和空项目是一样的,只不过自动添加了一个stdafx.h和helloworld,其他没有本质区别
你可以自己打开空项目和控制台程序的所有配置文件仔细比较下,我粗略看了一下没有什么区别

时间: 2024-08-04 04:05:05

c cpp-error LNK2019: 无法解析的外部符号 _add,该符号在函数 _main 中被引用的相关文章

lnk1120-vs2010运行程序报错:error LNK2019: 无法解析的外部符号

问题描述 vs2010运行程序报错:error LNK2019: 无法解析的外部符号 如题,我在vs2010环境下做C++练习题时出现该错误.程序代码如下: //array.h#ifndef ARRAY_H#define ARRAY_Htemplate<typename T>class Array{public: Array(int n);//数组首地址不用指定,待会分配 Array(Array &a); ~Array(); T getAt(int i);//返回第i个数组元素 voi

lnk2019-求救大神们,error LNK2019: 无法解析的外部符号 的问题

问题描述 求救大神们,error LNK2019: 无法解析的外部符号 的问题 我从网上下了个demo,然后新建工程导入了源代码,.h和.lib文件也都安放到位了,运行后报错显示 error LNK2019: 无法解析的外部符号 "public: __thiscall gsound::SoundPropagator::~SoundPropagator(void)" (??1SoundPropagator@gsound@@QAE@XZ),该符号在函数 _main 中被引用 还有很多都是类

c++-VS2013 C++ error LNK2019: 无法解析的外部符号

问题描述 VS2013 C++ error LNK2019: 无法解析的外部符号 #include #include #include using namespace std; #include template class CMatrix { T * * Mat; int nRow; int nCol; public: CMatrix(); CMatrix(int row, int col); CMatrix(int row, int col, int k); void Show(); voi

graph-matlab进行mex时出现 error LNK2019: 无法解析的外部符号……

问题描述 matlab进行mex时出现 error LNK2019: 无法解析的外部符号-- **具体问题是:我想把现有的.cpp文件转成.mexw32的文件,代码没有出现错误,因为前辈曾转出过64位的.** 而且配置应该没问题,因为mex代码中其他.cpp文件是可以做出来的,唯独这个出现以下错误: mex mex_maxflow.cpp 正在创建库 C:DOCUME~1ADMINI~1LOCALS~1TEMPMEX_4W~1templib.x 和对象 C:DOCUME~1ADMINI~1LOC

linkerror-1&amp;amp;gt;Generic.obj : error LNK2019: 无法解析的外部符号

问题描述 1>Generic.obj : error LNK2019: 无法解析的外部符号 //Generic.cpp #include // 每一个 Windows 程序都需要包含此档 #include "resource.h" // 內含各个 resource IDs #include "Generic.h" // 本程序之含入档 HINSTANCE _hInst; // Instance handle HWND _hWnd; char _szAppNam

VS2010 error LNK2019: 无法解析的外部符号

问题描述 VS2010 error LNK2019: 无法解析的外部符号 程序如下: #include #include #include #include #include #include #include #include #include #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "cuda_runtime.h" #include #i

error LNK2019: 无法解析的外部符号 _deflate

我的环境为: Win764 + VS2005 + zlib1.2.8 zlib1.2.8我使用VS2010来编译. ------------------------------------------------------------------------------------------------- 我导入zlib库的代码如下: #include "zlib128/zlib.h"// #pragma comment(lib, "zlib128/zlibwapi.li

c++-C++ error LNK2019: 无法解析的外部符号

问题描述 C++ error LNK2019: 无法解析的外部符号 在看某项目的源码时 发现其使用了第三方的头文件,之后我就去官网下载了. 下载来是压缩文件 里面有一些.c .h之类的文件,我把这些文件都包含在源码中. 然后运行就出现了: error LNK2019: 无法解析的外部符号 _jpeg_std_error,该符号在函数 "unsigned int __cdecl rescaleJPEG(unsigned char *,unsigned int,unsigned char * *,i

python-Python中出现error LNK2019无法解析的外部符号 _PyCallable_Check

问题描述 Python中出现error LNK2019无法解析的外部符号 _PyCallable_Check #include #include #include using namespace std; int main() { Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.patth.app