Link to home
Start Free TrialLog in
Avatar of BarryTang
BarryTang

asked on

Error in building exe in VC++6 ?

I have a VC++6program which call an dll : vctest0.dll in my source program "vctest0a.cpp", which is as follows :

#include "stdafx.h"
#include <c:\vctest0\vctest0.h>

__declspec( dllexport ) int MyFunction();

int main()
{
     MyFunction();
     return 0;
}

It is no error/warning in compiling this program
But it returns error as follows when build the exe

Linking...
vctest0a.obj : error LNK2001: unresolved external symbol "int __cdecl MyFunction(void)" (?MyFunction@@YAHXZ)
Debug/vctest0a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
vctest0a.exe - 2 error(s), 0 warning(s)

( vctest0a.cpp is my source code file )

Actually I am not familiar to use DLL,
can anyone help me for this issue,
Thank you.

Avatar of Exceter
Exceter
Flag of United States of America image

That is a linker error.
That is a linker error.
Avatar of BarryTang
BarryTang

ASKER

How to solve the linker error ?
Could you cost the source to your h files.
Do you have this module which is exporting MyFunction() in the import list for the exe?  
Project Settings / Link / input / object,library modules
The setting of Project Settings / Link / input / object,library modules

kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "c:\vctest0\release\vctest0.lib"


The following is the source :

// vctest0.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "vctest0.h"
#include "stdio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//     Note!
//
//          If this DLL is dynamically linked against the MFC
//          DLLs, any functions exported from this DLL which
//          call into MFC must have the AFX_MANAGE_STATE macro
//          added at the very beginning of the function.
//
//          For example:
//
//          extern "C" BOOL PASCAL EXPORT ExportedFunction()
//          {
//               AFX_MANAGE_STATE(AfxGetStaticModuleState());
//               // normal function body here
//          }
//
//          It is very important that this macro appear in each
//          function, prior to any calls into MFC.  This means that
//          it must appear as the first statement within the
//          function, even before any object variable declarations
//          as their constructors may generate calls into the MFC
//          DLL.
//
//          Please see MFC Technical Notes 33 and 58 for additional
//          details.
//

/////////////////////////////////////////////////////////////////////////////
// CVctest0App

BEGIN_MESSAGE_MAP(CVctest0App, CWinApp)
     //{{AFX_MSG_MAP(CVctest0App)
          // NOTE - the ClassWizard will add and remove mapping macros here.
          //    DO NOT EDIT what you see in these blocks of generated code!
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVctest0App construction

CVctest0App::CVctest0App()
{
     // TODO: add construction code here,
     // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CVctest0App object

CVctest0App theApp;

void _stdcall MyFunction () {
      AfxMessageBox("Hello, This is a test");
      return;
}

This is the h files

// vctest0.h : main header file for the VCTEST0 DLL
//

#if !defined(AFX_VCTEST0_H__2AC7561D_6FB8_4E5F_84FF_5C435DE1929A__INCLUDED_)
#define AFX_VCTEST0_H__2AC7561D_6FB8_4E5F_84FF_5C435DE1929A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
     #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"          // main symbols

/////////////////////////////////////////////////////////////////////////////
// CVctest0App
// See vctest0.cpp for the implementation of this class
//

class CVctest0App : public CWinApp
{
public:
     CVctest0App();

// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CVctest0App)
     //}}AFX_VIRTUAL

     //{{AFX_MSG(CVctest0App)
          // NOTE - the ClassWizard will add and remove member functions here.
          //    DO NOT EDIT what you see in these blocks of generated code !
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_VCTEST0_H__2AC7561D_6FB8_4E5F_84FF_5C435DE1929A__INCLUDED_)

ASKER CERTIFIED SOLUTION
Avatar of Mafalda
Mafalda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
change this __declspec( dllexport ) int MyFunction();

to __declspec( dllimport ) int MyFunction();
Everything looks correct to me except function declarations

in dll it should be declared as:
__declspec( dllexport ) int MyFunction();

but in exe:
__declspec( dllimport ) int MyFunction();

This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "Mafalda"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **