请告诉LNK2019错误解决方案 [英] Please tell the LNK2019 error solution

查看:76
本文介绍了请告诉LNK2019错误解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的错误是

I am getting an error that is

Socket.obj : error LNK2019: unresolved external symbol "int __cdecl calc_check(int)" (?calc_check@@YAHH@Z) referenced in function _main


中引用了
这是我的socket.cpp



This is mine socket.cpp

int main()
{
int hh=2;

    //int chkvalue=calculatechecksum();
    int chkvalue=calc_check(hh);
}


这是我的checksum.c


This is mine checksum.c

//===================================================== file = checksum.c =====

//----- Include files ---------------------------------------------------------
#include <stdio.h>                  // Needed for printf()
#include <stdlib.h>                 // Needed for rand()
#include <windows.h>
#include<conio.h>
#include <stdint.h>
#include"Constant.h"

//----- Type defines ----------------------------------------------------------
typedef unsigned char      byte;    // Byte is a char
typedef unsigned short int word16;  // 16-bit word is a short int
typedef unsigned int       word32;  // 32-bit word is an int

//----- Defines ---------------------------------------------------------------
#define BUFFER_LEN        6      // Length of buffer
//extern char data[6];
//char data[6]="CM00";
 extern char data[6]="CM00";
   word16      check;  
//----- Prototypes ------------------------------------------------------------
word16 checksum(char *addr, word32 count);
//int calc_check(int);
//===== Main program ==========================================================
int calc_check(int w)
{
 //byte        buff[BUFFER_LEN]; // Buffer of packet bytes

          // 16-bit checksum value
  word32      i;                // Loop counter

  // Load buffer with BUFFER_LEN random bytes
  for (i=0; i<BUFFER_LEN; i++)
  {
    //buff[i] = (byte) rand();
	data[i]=(byte) rand();
  }

  // Compute the 16-bit checksum
  //check = checksum(buff, BUFFER_LEN);
  check = checksum(data, BUFFER_LEN);

  // Output the checksum
  printf("checksum = %04X \n", check);
  return check;
  
}

//=============================================================================
//=  Compute Internet Checksum for count bytes beginning at location addr     =
//=============================================================================
word16 checksum(char *addr, word32 count)
{
  register word32 sum = 0;

  // Main summing loop
  while(count > 1)
  {
    //sum = sum + *((word16 *) addr)++;
	  sum += *((word16 *) addr)++;
    count = count - 2;
  }

  // Add left-over byte, if any
  if (count > 0)
    sum = sum + *((byte *) addr);

  // Fold 32-bit sum to 16 bits
  while (sum>>16)
    sum = (sum & 0xFFFF) + (sum >> 16);

  return(~sum);
}


这是我的头文件(constant.h),包含在socket.cpp中


This is mine header file(constant.h) which is included in socket.cpp

int calc_check(int);

推荐答案

constant.h:
#ifdef __cplusplus
extern "C" {
#endif

int calc_check(int);

#ifdef __cplusplus
}
#endif


问题在于,为.cpp文件声明函数时,必须将其声明为extern "C",因为.c文件会生成目标文件,而函数名称不会被弄乱(例如?calc_check @@ YAHH @ Z).如果在函数声明中没有告诉extern "C" .cpp文件,则C ++源代码将查找错误的名称,而不会找到未修饰的.c符号.使用上面的代码,您的头文件在同时包含.c和.cpp文件时仍然有效,并告诉您的C ++代码查找未破坏的内容.


The problem is that you have to declare your function as extern "C" when you declaring it for .cpp files because .c files generate object files where the name of functions isn''t mangled (like ?calc_check@@YAHH@Z). If you don''t tell extern "C" for .cpp files in the function declaration then the C++ sources will look for mangled names and wont find the unmangled .c symbols. With the above code your header file remains valid when included from both .c and .cpp files and tells your C++ code to look for unmangled stuff.


在C文件中,很可能是在C语言中编译的C函数名称编码约定的模式.但是您的主要功能在CPP文件中.因此,您必须在标头中使用extern"C"声明,或将名称checksum.c更改为checksum.cpp
In C file most probably is compiled in C mode with C function name encoding convention. But your main function is in a CPP file. So, you have to declare with extern "C" in the header, or change the name checksum.c to checksum.cpp


这篇关于请告诉LNK2019错误解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆