Compile Errors != Linker Errors, BTW why do I get Linker errors?

Daniel Duffy

C++ author, trainer
I am getting a LINKER error. Any ideas what's up? I have been trying for days now
Code:
Error 3 error LNK1120: 1 unresolved externals c:\users\felix\documents\visual studio 2013\Projects\testmc\Debug\testmc.exe testmc
 
Error 2 error LNK2019: unresolved external symbol "public: __thiscall BoostNormal::BoostNormal(void)" (??0BoostNormal@@QAE@XZ) referenced in function _main c:\Users\felix\documents\visual studio 2013\Projects\testmc\testmc\TestMC.obj testmc
 
Warning 1 warning C4244: 'argument' : conversion from 'double' to 'long', possible loss of data c:\program files (x86)\microsoft visual studio 12.0\vc\include\utilitiesdjd\geometry\range.cpp 156 1 testmc
 

Aaronnejad

Member
C++ Student
Hi Dr. Duffy ,

I just looked at your first lecture again.

I seem to have gotten past some of the hurdles in the Visual C++ environment but I cant seem to get any of the programs to compile without errors. For example I have Hello.cpp open as well as Hello.hpp and testHello.cpp. I go to Hello.cpp and highlight the first few lines with the include headings and void hello_C with its printf comman and I get the following result.
BTW I follow the exact same steps that you take.


1>------ Build started: Project: RoniTest3, Configuration: Release Win32 ------
1>MSVCRT.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main
1>c:\users\aaron\documents\visual studio 2010\Projects\RoniTest3\Release\RoniTest3.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I always receive the result:
Build :0 succeeded, 1 Failed.

Please let me know what i could be doing wrong.

Thanks in advance.
 

Daniel Duffy

C++ author, trainer
Please let me know what i could be doing wrong.

If you give me a dollar! Just kidding.

Hint

unresolved external symbol _main

Add the .cpp file with main() in it to your project.

hth
 

Aaronnejad

Member
C++ Student
I changed void hello_c() to
void main()

is that what you wanted me to do? It still doesnt work.
 

APalley

Well-Known Member
I changed void hello_c() to
void main()

is that what you wanted me to do? It still doesnt work.
Have you added it to the project? Simply having it open will not suffice.

1) Create an empty project
2) Add an existing or empty code file
3) Ensure that you have a main ()
4) Compile
 
Last edited:

Daniel Duffy

C++ author, trainer
Using C++ 11 to compute PI by throwing darts

Code:
// TestPI.cpp
//
// Calculation of PI using random numbers; it's analogous
// to throwing darts.
//
// 2014-9-8 Code portd to C++ 11 from Boost.
//
// (C) Datasim Education BV 2010-2014
//

#include <random> // Convenience header file
#include <iostream>
#include <ctime>            // std::time

int main()
{
    std::default_random_engine rng;

    std::random_device rd;
    rng.seed(rd());
    std::uniform_real_distribution<double> uni(0.0,1.0);

    // Choose the desired accuracy
    std::cout << "How many darts to throw? "; long N; std::cin >> N;

    // Thow the dart; does it fall in the circle or outside
    // Start throwing darts and count where they land
    long hits = 0;
    double x, y, distance;
    for (long n = 1; n <= N; ++n)
    {
        x = uni(rng); y = uni(rng);
        distance = sqrt(x*x + y*y);

        if (distance <= 1.0)
        {
            hits++;
        }
    }

    std::cout << "#hits, PI is: " << hits << ", " << 4.0 * double(hits) / double (N);
       

    return 0;

}
 

Daniel Duffy

C++ author, trainer
<Quote>
What I don't understand earlier is what is the exact difference between header files and source files, also I don't understand how my code turn into .exe files. The core problem of this is I don't know how the computer works when it turns my code to a exe. That is how it works:
It is very important to understand the graph in the class material 01-01 -C Environment, page 7(see in the attachment). The source files do not know each other's behavior before the linkers link them together.So it is very dangerous for a source file A to include another source file B, except the situation that the functions in the source file are all generic functions, because before the A.cpp and B.cpp link together, even if they use #ifndef #define#endif, all the variables and functions in B.cpp would still be defined in A.cpp. After that, when the linker link them together, the functions and variables in B would come up with redefined problem.
Plus, the head files would not be pre-processed and compiled independently, it must depend on the source files. That is the difference between head files and source files.
<Unquote>
 

Daniel Duffy

C++ author, trainer
I (Yao Wang) am providing a easy step to solve these problems.
Step 1 : Search MSO.DLL ,VBE6EXT.OLB, and EXCEL.EXE to find out where they are located.
Step 2: Replace the code in the ExcelImports.cpp
Here is my code :
"C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\MSO.DLL"
"C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"
Be careful : Office 15 --- one is capital(OFFICE15) ---so make sure it is the exact place you find
Hope this save you some time.
 

Danielle Chen

Member
C++ Student
Maybe this will help

http://www.datasim.nl/Education/Videos/MyFirstProject/

This is the best piece of information, ever :) It will save you so much time. It did for me :)
Hi Daniel, @APalley
I'm still getting link errors after I followed this instruction and add additional include directories. Could you please tell me where went wrong? Thanks!
1>Shape.obj : error LNK2005: "public: class YC::CAD::Shape & __thiscall YC::CAD::Shape::operator=(class YC::CAD::Shape const &)" (??4Shape@CAD@YC@@QAEAAV012@ABV012@@Z) already defined in Point.obj
1>Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ) referenced in function __unwindfunclet$??0Circle@CAD@YC@@QAE@XZ$0
1>Line.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>Point.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>C:\Users\Danielle\Documents\Visual Studio 2010\Projects\Level 5\3.4-Exercise2\Debug\3.4-Exercise2.exe : fatal error LNK1120: 1 unresolved externals
 

APalley

Well-Known Member
Hi Daniel, @APalley
I'm still getting link errors after I followed this instruction and add additional include directories. Could you please tell me where went wrong? Thanks!
1>Shape.obj : error LNK2005: "public: class YC::CAD::Shape & __thiscall YC::CAD::Shape::operator=(class YC::CAD::Shape const &)" (??4Shape@CAD@YC@@QAEAAV012@ABV012@@Z) already defined in Point.obj
1>Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ) referenced in function __unwindfunclet$??0Circle@CAD@YC@@QAE@XZ$0
1>Line.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>Point.obj : error LNK2001: unresolved external symbol "public: __thiscall YC::CAD::Shape::~Shape(void)" (??1Shape@CAD@YC@@QAE@XZ)
1>C:\Users\Danielle\Documents\Visual Studio 2010\Projects\Level 5\3.4-Exercise2\Debug\3.4-Exercise2.exe : fatal error LNK1120: 1 unresolved externals
Does your Shape destructor have a body defined?
 

Danielle Chen

Member
C++ Student
Does your Shape destructor have a body defined?
Thank you APalley @APalley, I made a silly mistake to forget to write destructor lol..
After adding destructor, I still got one link error:

1>Shape.obj : error LNK2005: "public: class YC::CAD::Shape & __thiscall YC::CAD::Shape::operator=(class YC::CAD::Shape const &)" (??4Shape@CAD@YC@@QAEAAV012@ABV012@@Z) already defined in Point.obj
1>C:\Users\Danielle\Documents\Visual Studio 2010\Projects\Level 5\3.4-Exercise2\Debug\3.4-Exercise2.exe : fatal error LNK1169: one or more multiply defined symbols found
Below is my code for operator = in shape and point:

Code:
Shape& Shape::operator=(const Shape& source)
{//Assignment operator.
    cout<<"Shape assignment operator is called."<<endl;
    if (this == &source)
        return *this;
    this->m_id = source.m_id;  
    return *this;
}
Code:
Point& Point::operator = (const Point& source)          
{//Print out text in assignment operator.
  
    cout<<"Point assignment operator called."<<endl;
    if (this==&source)  
        return *this;
    Shape::operator=(source);  
    this->m_x = source.m_x;
    this->m_y = source.m_y;
    return *this;
}
Thanks!
 
Last edited:
Top