-1

Hi I am trying to run an application using dll. I am using Dev C++, but i always get a linker error. The code of dll.h is

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
#define DLLIMPORT __declspec (dllexport)
#else 
#define DLLIMPORT __declspec (dllimport)
#endif 


class DLLIMPORT sum
{
  public:
    sum();
    void input();
    void add();
    void display();
    virtual ~sum(void);

  private:
          int x,y,res;

};


#endif 

and that of dllmain.cpp is
#include<iostream>
#include "dll.h"
#include <windows.h>
sum::sum()
{
 x=y=res=0;
}


sum::~sum()
{
}
void sum::input()
{
     std::cout<<"Enter two numbers ";
     std::cin>>x>>y;
}
void sum::add()
{
     res=x+y;
}
void sum::display()
{
     std::cout<<"The application in running under a dll file"<<std::endl;
     std::cout<<"The sum is "<<res<<std::endl;
}              

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

The host application (sample.cpp) code is

#include<iostream>
#include "dll.h"
using namespace std;
int main()
{
    sum s;
    s.input();
    s.add();
    s.display();
    system("pause");
    return 0;
}    

But i get a linker error saying:

  [Linker error] undefined reference to `_imp___ZN3sumC1Ev@4' 
  [Linker error] undefined reference to `_imp___ZN3sum5inputEv@4' 
  [Linker error] undefined reference to `_imp___ZN3sum3addEv@4' 
  [Linker error] undefined reference to `_imp___ZN3sum7displayEv@4' 
  [Linker error] undefined reference to `_imp___ZN3sum7displayEv@4' 
  [Linker error] undefined reference to `_imp___ZN3sum7displayEv@4' 
  [Linker error] undefined reference to `_imp___ZN3sum7displayEv@4' 

I dont know what to do. I just started dll programming, but am stick with this. Can anybody help me out this?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
ioesandeep
  • 23
  • 1
  • 5
  • 1
    http://stackoverflow.com/a/12574400/673730 – Luchian Grigore Nov 04 '12 at 10:55
  • 3
    Formating this for you was more work than if you hadn't put your random `` or `
    `. Please read the faq and [the markdown help](http://stackoverflow.com/editing-help) before asking again.
    – Denys Séguret Nov 04 '12 at 10:56
  • 1
    Macros and identifiers containing an underscore followed by a capital letter are reserved by the implementation, change your header guards! – rubenvb Nov 04 '12 at 10:59
  • @rubenvb: Right, or just use a convenient `pragma once`. – Mr.C64 Nov 04 '12 at 11:04
  • @Mr.C64 which is equally nonstandard. No. – rubenvb Nov 04 '12 at 11:44
  • @rubenvb: `pragma once` is supported by MSVC, and I think also by GCC and Clang. I use it with MSVC. While `_RESERVED` (like `_Reserved`) is an abomination since underscore followed by capital letter is reserved, `pragma once` certainly isn't. – Mr.C64 Nov 04 '12 at 17:52
  • @Mr.C64 both are implementation defined behavior. Both non-standard, whichever way you put it. You don't even gain anything from pragma once, header guard handling is extremely optimized in modern compilers. But this is a boring discussion, so lets stop `;-)` – rubenvb Nov 04 '12 at 20:54
  • @rubenvb: `pragma once` is less error prone and more convenient to me; in any case, there are several discussions on this topic, e.g. [here](http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards). – Mr.C64 Nov 04 '12 at 21:34

2 Answers2

0

Your DLLIMPORT symbol is expand to __declspec(dllexport) if BUILDING_DLL is #define'd.

So, in your DLL implementationn file (dllmain.cpp), you should #define BUILDING_DLL before #include "dll.h".

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
0

You defined DLLIMPORT like this:

#if BUILDING_DLL
#define DLLIMPORT __declspec (dllexport)
#else
#define DLLIMPORT __declspec (dllimport)
#endif

But you've forgotten to define BUILDING_DLL in your dll source files, before the inclusion of dll.h:

#define BUILDING_DLL
#include "dll.h"

Note: you should use:

#ifdef BUILDING_DLL

instead of

#if BUILDING_DLL

because the first one is more self-documenting.

Synxis
  • 9,236
  • 2
  • 42
  • 64