Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
This is mine socket.cpp
int main()
{
int hh=2;
 
    //int chkvalue=calculatechecksum();
    int chkvalue=calc_check(hh);
}
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);
}
This is mine header file(constant.h) which is included in socket.cpp
 
int calc_check(int);
Posted 12-Sep-12 4:06am
Comments
Wes Aday at 12-Sep-12 10:10am
   
Looks like name mangling to me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

constant.h:
#ifdef __cplusplus
extern "C" {
#endif
 
int calc_check(int);
 
#ifdef __cplusplus
}
#endif
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.
  Permalink  
Comments
Tarun Batra at 12-Sep-12 10:29am
   
please have a look at this http://www.codeproject.com/Questions/457875/code-i-correct-why-am-i-getting-plusplus-needs-l-v and please answer it
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 382
1 Damith Weerasinghe 370
2 thatraja 350
3 Sergey Alexandrovich Kryukov 259
4 Ajith K Gatty 200
0 Sergey Alexandrovich Kryukov 10,639
1 OriginalGriff 6,658
2 Peter Leow 4,735
3 Abhinav S 3,813
4 Maciej Los 3,565


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid