C++ Fatal Error LNK1120: 1 unresolved externals
Asked Answered
F

13

23

What is causing this error? I google'd it and first few solutions I found were that something was wrong with the library and the main function but both seem to be fine in my problem, I even retyped both! What could be causing this?

This might be helpful:

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain@16 referenced in function __tmainCRTStartup

#include <iostream>
using namespace std;
int main()
{
    const double A = 15.0, 
                 B = 12.0, 
                 C = 9.0;
    double aTotal, bTotal, cTotal, total;
    int numSold;

    cout << "Enter The Number of Class A Tickets Sold: ";
    cin >> numSold;
    aTotal = numSold * A;

    cout << "Enter The Number of Class B Tickets Sold: ";
    cin >> numSold;
    bTotal = numSold * B;

    cout << "Enter The Number of Class C Tickets Sold: ";
    cin >> numSold;
    cTotal = numSold * C;

    total = aTotal + bTotal + cTotal;

    cout << "Income Generated" << endl;
    cout << "From Class A Seats $" << aTotal << endl;
    cout << "From Class B Seats $" << bTotal << endl;
    cout << "From Class C Seats $" << cTotal << endl;
    cout << "-----------------------" << endl;
    cout << "Total Income: " << total << endl;

    return 0;
}
Felipafelipe answered 14/9, 2011 at 2:58 Comment(3)
Post the full error. Which is the unresolved symbol?Medicament
And the unresolved symbol is? Please provide the entire error text.Sexism
You should also be getting LNK2001 errors listed what is unresolved. What are those?Hilaria
S
34

From msdn

When you created the project, you made the wrong choice of application type. When asked whether your project was a console application or a windows application or a DLL or a static library, you made the wrong chose windows application (wrong choice).

Go back, start over again, go to File -> New -> Project -> Win32 Console Application -> name your app -> click next -> click application settings.

For the application type, make sure Console Application is selected (this step is the vital step).

The main for a windows application is called WinMain, for a DLL is called DllMain, for a .NET application is called Main(cli::array ^), and a static library doesn't have a main. Only in a console app is main called main

Sturmabteilung answered 14/9, 2011 at 3:6 Comment(2)
HA! Your right I clicked the one below it ~ Looks exactly the same though how can you tell?Felipafelipe
Okay that made things click for me lol, but it wasn't because I didn't switch to a Windows Application it was because when you do Project Properties -> Linker -> System -> SubSystem and then choose Windows. It will only change for the current compile type which was Debug for me. So when I went to try out Release Mode it didn't compile, but now realized have to change to All Configuration or Manually edit each one. Also that's how you do it without making a new project for anyone else not sure if this was available in 2011 though. but this lnk error was driving me crazy lol.Indusium
H
14

I incurred this error once.

It turns out I had named my program ProgramMame.ccp instead of ProgramName.cpp

easy to do ...

Hope this may help

Hammerskjold answered 25/10, 2012 at 15:58 Comment(0)
A
9

My problem was int Main() instead of int main()

good luck

Ardyth answered 21/11, 2012 at 13:19 Comment(0)
S
2

Well it seems that you are missing a reference to some library. I had the similar error solved it by adding a reference to the #pragma comment(lib, "windowscodecs.lib")

Shalom answered 25/4, 2015 at 10:18 Comment(0)
G
2

My case: I defined a prototype of the class de-constructor, but forgot to define the body.

class SomeClass {
    ~SomeClass(); //error
};

class SomeClass {
    ~SomeClass(){}; //no error
}
Garwin answered 9/7, 2022 at 4:23 Comment(0)
L
1

In my case, the argument type was different in the header file and .cpp file. In the header file the type was std::wstring and in the .cpp file it was LPCWSTR.

Likker answered 23/9, 2020 at 11:18 Comment(0)
M
1

I had this error when I wrote a prototype member function void foo() const; in the bar.hpp but forgot to define its body in the class namespace. I initially wrote foo() const { /* body */ } in the bar.cpp file instead of bar::foo() const { /* body */ }

Malefaction answered 4/10, 2023 at 17:3 Comment(0)
L
0

You must reference it. To do this, open the shortcut menu for the project in Solution Explorer, and then choose References. In the Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button.

Lilylilyan answered 5/7, 2016 at 10:6 Comment(0)
O
0

I have faced this particular error when I didn't defined the main() function. Check if the main() function exists or check the name of the function letter by letter as Timothy described above or check if the file where the main function is located is included to your project.

Oarlock answered 21/9, 2016 at 9:2 Comment(1)
I am trying some asm with cpp in VS. Adding an empty source.cpp to my source.asm helped some of the way. But this LNK2019 and LNK1120 persisted. Then I added int main() to the empty cpp and that ended the failmessages, but now it just runs the cpp file and leaves the asm untouched. ?Quicksilver
P
0

In my particular case, this error error was happening because the file which I've added wasn't referenced at .vcproj file.

Penthouse answered 19/11, 2019 at 16:32 Comment(0)
B
0

In my case I got this error when I had declared a function under 'public' access specifier. Issue got resolved when I declared that function as private.

Burlesque answered 11/3, 2021 at 18:12 Comment(0)
S
0

I have encountered the same error. For me it turned out to be because I tried to implement an inline function in the .cpp file, instead of putting it in the header file, where the definition is. Therefore when I tried to include the header file and use the function, I got this error.

Soult answered 25/11, 2021 at 22:56 Comment(0)
B
0

In my case I had forgotten to add the main() function altogether.

Baseball answered 29/7, 2022 at 5:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.