Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ error LNK2001: unresolved external symbol function _main [duplicate]

Tags:

c++

main

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I'm learning C++ and I have a compiling problem in my project. I have read tons of post with this error on the title but I cant find where the problem is.

I have a method call in my Main function that is responsible for the error. Whenever I comment the line the project compiles perfect.

The code is the following:

Main.cpp

#pragma once 
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <WinSock.h>
#include <Windows.h>
#include <string.h>
#include "NetUtils.h"
#include "Utils.h"
#include "FileUtils.h"
#include "SendMail.h"
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{   

    SendMail *mail = new SendMail("[email protected]","Envio de C++","Cuerpo del mail");    
    char* msg="";   
    mail->SendNow();
    ...

This method mail->SendNow is the one I comment to solve the problem, so I guess I have some kind of header declaration problem inside of SendMail.cpp or SendMail.h

Now the rest of the classes and headers:

SendMail.h

#pragma once
#ifndef SENDMAIL_H
#define SENDMAIL_H


class SendMail

{
public:
    SendMail(char* c_to, char* c_subject, char* c_body);
    void Check(int iStatus, char *szFunction);
    void SendNow();
    char * to;
    char * subject;
    char * body;    
};


#endif

SendMail.cpp

#define WIN32_LEAN_AND_MEAN

#pragma once
#include "SendMail.h"
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <winsock2.h>



#pragma comment(lib, "ws2_32.lib")
using namespace std;

// Insist on at least Winsock v1.1
const int VERSION_MAJOR = 1;
const int VERSION_MINOR = 1;

#define CRLF "\r\n"                 // carriage-return/line feed pair



SendMail::SendMail(char* c_to, char* c_subject, char* c_body)
{
    to = c_to;
    subject= c_subject;
    body = c_body;

}

// Basic error checking for send() and recv() functions
void Check(int iStatus, char *szFunction)
{
  if((iStatus != SOCKET_ERROR) && (iStatus))
    return;

  cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
}

void SendNow()
{
    // WSADATA     WSData;  

    ///* Attempt to intialize WinSock (1.1 or later)*/
    //  if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
    //  {
    //  cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
    //  ErrMsg="Cannot find Winsock v";
    //  return;     
    //  }
}

AS you can see the method Send is commented out so I cant figure out what the problem is.

The compiler output is:

Error   6   error LNK1120: 1 unresolved externals   C:\Users\clanderasm\Documents\Visual Studio 2010\Projects\LandeTestConsole\Debug\LandeCplusConsole.exe  LandeCplusConsole
Error   5   error LNK2019: unresolved external symbol "public: void __thiscall SendMail::SendNow(void)" (?SendNow@SendMail@@QAEXXZ) referenced in function _main    C:\Users\clanderasm\Documents\Visual Studio 2010\Projects\LandeTestConsole\LandeCplusConsole\LandeCplusConsole.obj  LandeCplusConsole
like image 553
Carlos Landeras Avatar asked Nov 08 '12 14:11

Carlos Landeras


People also ask

How do I fix unresolved external symbol on lnk2001?

To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .

What is unresolved external symbol error?

The compiler can identify when a symbol isn't declared, but it can't tell when the symbol isn't defined. It's because the definition may be in a different source file or library. If a symbol is referred to but never defined, the linker generates an unresolved external symbol error.

What kind of an error is an unresolved external reference?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.

What is __ cxxframehandler4?

__CxxFrameHandler4 indicates that the library is building with the VS 2019 x64 toolset (19.20 - 19.28), but is being linked against an older version of the Visual C++ Runtime.


1 Answers

Basically what that error means is that you have a function that you promise to implement in your header, but when it got to the part where it actually needed the function, it didn't find it.

If you comment out the function call, the promise that you will implement that function is still there. However, nobody is actually using the function, so it doesn't matter that you don't come through on your promise.

Once you know that it means, it's pretty easy to find what is wrong:

You are defining the function as:

void SendNow()

This is a global function and not a class function, as such you have not implemented the class function you promised to implement.

You can fix this by turning it into:

void SendMail::SendNow()

Do note that you have the same problem in Check() even though that's not causing an error yet.

like image 82
Jasper Avatar answered Sep 18 '22 22:09

Jasper