Wix
Wix

Reputation: 125

DLL compilation Error

I have a problem again. I was able to compile my DLL properly. But once I begin linking it to a ready-to-compile executable. Something happens.

Here's the CMD:

>gcc -c -o dllmain.o dllmain.c -D ADD_EXPORTS
>gcc -o dllmain.dll dllmain.o -s -shared -Wl,--subsystem,windows

This part turned out just fine and produced a seemingly good-looking dllmain.dll.

Here's where the problem starts:

>gcc -c -o dllmain.o dllmain.c
>gcc -o main.exe -s main.o -L. -ladd
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -ladd
collect2.exe: error: ld returned 1 exit status

Can someone tell me why it appears? (All filenames are triple-checked and are correct!)

Here's "dllmain.c"

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>

DLLIMPORT void HelloWorld()
{
    MessageBox(0,"Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}

DLLIMPORT CALL add(int a, int b)
{
   return a + b;
}

DLLIMPORT CALL subtract(int a, int b)
{
    return a - b;
}

DLLIMPORT CALL multiply(int a, int b)
{
   return a * b;
}

DLLIMPORT CALL divide(int a, int b)
{
    if(b = 0)
    {
        MessageBox(0, "Cannot divide by zero!", "Division Error", 1);
        return;
    }
    return a / b;
}

DLLIMPORT CALL modulo(int a, int b)
{
    if(b = 0)
    {
        MessageBox(0, "Cannot divide by zero!", "Modulo Error", 1);
    }
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
    switch(fdwReason)
    {
        case DLL_PROCESS_ATTACH:
        {
            break;
        }
        case DLL_PROCESS_DETACH:
        {
            break;
        }
        case DLL_THREAD_ATTACH:
        {
            break;
        }
        case DLL_THREAD_DETACH:
        {
            break;
        }
    }

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

Here's "dll.h"

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

#define CALL __cdecl

#ifdef __cplusplus
extern "C"
{
#endif

DLLIMPORT void HelloWorld();
DLLIMPORT CALL add(int a, int b);
DLLIMPORT CALL subtract(int a, int b);
DLLIMPORT CALL multiply(int a, int b);
DLLIMPORT CALL divide(int a, int b);
DLLIMPORT CALL modulo(int a, int b);

#ifdef __cplusplus
}
#endif

Finally, "main.c"

#include <stdio.h>
#include <stdlib.h>
#include "dll.h"

main()
{
    int a, b, c;
    char chc;
    printf("Input 2 numbers: ");
    printf("Input operation: ");
    scanf("%c", &chc);
    switch(chc)
    {
        case '+': addition(a, b); break;
        case '-': subtraction(a, b); break;
        case '*': multiply(a, b); break;
        case '/': divide(a, b); break;
        case '%': modulo(a, b); break;
    }
    getch();
}

Upvotes: 2

Views: 757

Answers (1)

P0W
P0W

Reputation: 47794

Simply use :

gcc -o main.exe main.c dllmain.dll

Linker looking for libadd.a , which doesn't exist in your case

Also for compiling you don't have to supply dllmain.o

Upvotes: 1

Related Questions