Matt Phillips

CW Win32 x86 - Newbie can't get off the ground making a dll, please help!

Discussion created by Matt Phillips on Sep 28, 2007
Latest reply on Oct 2, 2007 by Matt Phillips
Hello,

I've been at it for about a week now and gone through every manual and internet resource I could find, but I still don't know what I'm doing wrong.  (Windows XP on a Dell Inspiron 640m, CW 5.0.0.)  I can't get rid of this error:

Error   : Undefined symbol: '_mysum' referenced from '_main' in hello.c:19
hello.c line 19  

'mysum' is a function defined in a DLL I created from the CW template, and is called from hello.c.  This is happening in the simplest possible program I can write, so it's possible to describe what I'm doing literally screen by screen:

Making the DLL:
1. New project => Win 32 C++ Stationery, project name ‘testutils’
2.
Choose Win32 DLL: DLL using default libraries
3. Project testutils.mcp is now created, with the file
'x86_Win32 DLL.cpp' in the source directory. 
4. I add the definition for 'mysum' to x86_Win32 DLL.cpp', and put the definition in the header.  This compiles s without problem, creating “testutils Release.dll” and “testutils Release.lib”.  I've put the code for these functions at the end of the message.

Making hello.c:
1. New project =>
Win 32 C Stationery, project name ‘test_cw’
2. Choose Win32 Console App: App using default libraries
3. Project test_cw.mcp is now created, with the file 'hello.c' in the source directory. 
4
. a) Add call to ‘mysum’ in function main, b) add #include "x86_Win32 DLL.h", c) add directory testutils to access path.

Doing all of this results in the error I showed above, and *only* this error.  I've tried a number of variations of this, like putting mysum in a separate file, but I always get the above linker error OR a variation thereof which has something like (@X@ANNN@Y) following the reference to _mysum--sorry I didn't save this but I presume that when you use CW's DLL template, your functions are supposed to go in the cpp file it creates?  Anyway I would be immensely grateful if someone could shed light on this, I really couldn't find a how-to anywhere in the documentation or elsewhere.

Best,
Matt

/*******************************hello.c**************************************/
#include "x86_Win32 DLL.h"
#include <stdio.h>

int main(void)
{
    double d;
   
    d = mysum(2,3);
   
    fprintf(stdout, "Hello World, 2 + 3 = %f!\n", d);
    return 0;
}
/*********************************************************************/


/***********************************
x86_Win32 DLL.h**********************/
#ifndef __X86_WIN32_DLL_H__
#define __X86_WIN32_DLL_H__

#ifndef IMPEXP
#define IMPEXP    __declspec(dllimport)
#endif

IMPEXP int RectFrame(HDC hdc, int x1, int y1, int x2, int y2, int t);
IMPEXP int EllipseFrame(HDC hdc, int x1, int y1, int x2, int y2, int t);

#endif /*#ifndef __X86_WIN32_DLL_H__*/

double mysum(double a, double b);
/*********************************************************************/


/****************************************x86_Win32 DLL.cpp*****************************/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

/*    Declare our exports, defining IMPEXP only inside the DLL */
#define IMPEXP    __declspec(dllexport)
#include "x86_Win32 DLL.h"

/*    This DLL user entry point function is only required when your application
    has special initialization or termination requirements.  Otherwise
    the MSL runtime provides the appropriate startup code.  */
BOOL WINAPI DllMain ( HINSTANCE hInst, DWORD wDataSeg, LPVOID lpvReserved );

BOOL WINAPI DllMain( HINSTANCE hInst, DWORD fdwReason, LPVOID lpReserved )
{
    char buf[512];
    char exename[256];
    char dllname[256];
   
    GetModuleFileName(0L, exename, sizeof(exename));
    GetModuleFileName(hInst, dllname, sizeof(dllname));
       
    switch(fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            snprintf(buf, sizeof(buf), "Process\n'%s'\nattaching to\n'%s'...", exename, dllname);
            MessageBox ( GetFocus(), buf, "Notice", MB_OK|MB_SYSTEMMODAL );
            return 1;

        case DLL_PROCESS_DETACH:
            snprintf(buf, sizeof(buf), "Process\n'%s'\ndetaching from\n'%s'...", exename, dllname);
            MessageBox ( GetFocus(), buf, "Notice", MB_OK|MB_SYSTEMMODAL );
            return 1;

        default:
            return 1;
       }
       return 0;
}

IMPEXP int RectFrame(HDC hdc, int x1, int y1, int x2, int y2, int t)
{
    Rectangle(hdc, x1, y1, x2, y2);
    Rectangle(hdc, x1+t, y1+t, x2-t, y2-t);
    return 1;
}

IMPEXP int EllipseFrame(HDC hdc, int x1, int y1, int x2, int y2, int t)
{
    Ellipse(hdc, x1, y1, x2, y2);
    Ellipse(hdc, x1+t, y1+t, x2-t, y2-t);
    return 1;
}

double mysum(double a, double b) {
    return(a+b);
    }
/*********************************************************************/

--
Alban Edit: Please always include CodeWarrior version in Message Subject line.




Message Edited by Alban on 2007-09-28 08:18 PM

Outcomes