1
votes

I wrote my project using a simple editor and I compile them by using Microsoft vc++ compiler through command line interface, I am getting the following error:

/out:Main.exe Main.obj

Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Ac count::debit(int)" (?debit@Account@@QAEXH@Z) referenced in function _main

Main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Acc ount::getBalance(void)" (?getBalance@Account@@QAEHXZ) referenced in function _main

Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Account ::Account(int)" (??0Account@@QAE@H@Z) referenced in function _main Main.exe : fatal error LNK1120: 3 unresolved externals

Here is the code:

//File : Account.h
class Account{
    public:
        Account( int );
        void credit( int );
        void debit( int );
        int getBalance();
    private:
        int balance;
};
//File:Account.cpp

#include<iostream>
using std::cout;
using std::endl;

#include "Account.h"

Account::Account( int initialbalance ){
    balance = 0;

    if( initialbalance > 0 )
        balance = initialbalance;

    if ( initialbalance < 0 )
        cout<<"Initial Balance is empty\n"<<endl;
}

void Account::credit( int amount ){
        balance = balance + amount;
}

void Account::debit( int amount ){
        if( amount <= balance )
            balance = balance - amount;
        else
            cout<<"Debit amount exceed balance amount\n"<<endl;
}

int Account::getBalance(){
        return balance;
}   
//File : Main.cpp
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

#include "Account.h"

int main(){
    Account obj(50);

    cout<<"Account balance Rs. "<<obj.getBalance()<<"\n"<<endl;
    int withdraw;

    cout<<"Withdrawal amount for your account\n"<<endl;
    cin>>withdraw;

    cout<<"Withdrawing ....."<<endl;
    obj.debit( withdraw );

    cout<<"Final account balance : "<<obj.getBalance()<<endl;


    return 0;
}   

I have first compiled Account.cpp by using "cl /LD Account.cpp" , then when i try to compile "Main.cpp" I get these error , to be specific I want to know how to use a compiled .dll or .obj file in my client code that uses these compiled files when their source code is not available .

Thanks in advance.

4
I actually never compiler with the command line in VC++, but you would have to include the account.h/cpp into the compilation. from what I see you're only compiling the main.cpp, and that's not compiling and linking automatically account.cppMarius Bancila

4 Answers

4
votes

It looks you are creating Main.exe just from Main.obj; you should link also Account.obj.

1
votes

It appears you are not linking Account.obj into Main.exe. You named Main.obj, but not Account.obj.

1
votes

Apparently you have forgotten to include the Account.obj in your command line arguments: /out:Main.exe Main.obj Account.obj

0
votes

Assuming that you have compiled Account.cpp into Account.dll, the compiler should have generated an import library Account.lib as well. Then your link directive should be:

/out:Main.exe Main.obj Account.lib

In general, during compilation the concrete implementation of a class or function is not necessary - just the declarations, so the compiler whould know which "placeholders" to put within the intermidiate files such as .obj files. For example, you compiled successfully Main.cpp into Main.obj.

However, the linker needs the files that include the concrete implementation of used classes or functions such as .obj or .lib files in order to replace those "placeholders" with actual executable code. This is why you need also Accout.lib in order to link Main.exe.

Of course, in case of DLL its .lib file includes instead of "real" executable code an import address table (IAT), by which all DLL function calls are referenced (each referenced DLL function contains its own entry in the IAT). At run-time, the IAT is filled with appropriate addresses that point directly to a function in the separately-loaded DLL.

I hope this helps :-)