Unable to link to an exe's import library.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to link to an exe's import library.

Dave Wilcoxson
I have been trying to upgrade a project of mine to use MinGW 5.1.4 (gcc 3.4.5) from 1.1 (gcc 2.95).  We call MinGW internally within an MSVC 8 C++ application to export some auto-generated C code as a .dll and then link into it.  The auto-generated code can also call back into the parent exe to call some utility functions.  The .dll gets linked to the import library that Visual Studio generates for the .exe.  This whole process worked fine with the 2.x version of gcc.  There are no warnings or errors generated during either compile or link.  I also confirmed this happens in gcc 4.x, as well.

The really strange thing is that when I compare the non-working dll to the working dll, Dependency Walker shows both dll's being dependent on the exe, but only the gcc 2.95 built dll actually imports any symbols from the exe.

Here's some simple code which duplicates the issue:

---- MSVC built Driver Program (main.cpp) ---

#include <windows.h>
#include <iostream>

#include "Export.h"

typedef int(*Function)(int*);

extern "C" __declspec(dllexport) void Increment(int* ptr)
{
  std::cout << "Increment" << std::endl;
  ++(*ptr);
}

int main(int argc, char** argv)
{
  int x = 4;

  std::cout << "x=" << x << std::endl;

  HMODULE hModule = LoadLibrary("test.dll");
  Function fp = (Function)(void*)GetProcAddress(hModule, "xplusplus");

  x = fp(&x);

  FreeLibrary(hModule);

  std::cout << "x=" << x << std::endl;

  return 0;
}

---- MinGW built DLL (test.c) ----

#include "Export.h"
#include <stdio.h>

__declspec(dllexport) int xplusplus(int* xp)
{
  printf("Calling increment from dll\n");
  // Crash on next line!
  Increment(xp);
  printf("Done incrementing\n");
  return ++(*xp);
}

---- Export.h ----

#ifdef __cplusplus
extern "C" {
#endif

 __declspec(dllexport) void Increment(int* ptr);

#ifdef __cplusplus
}
#endif

I use the following commands to compile and link:
gcc -c test.c -o test.obj
gcc -shared test.obj main.lib -o test.dll

Any ideas?

Thanks,
Dave


     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Unable to link to an exe's import library.

Ross Ridge
Dave Wilcoxson writes:
> The really strange thing is that when I compare the non-working dll to
> the working dll, Dependency Walker shows both dll's being dependent on
> the exe, but only the gcc 2.95 built dll actually imports any symbols
> from the exe.

For some reason the GNU linker isn't creating the import table in the
DLL correctly.  It's probably something to do with short import records,
although a GCC 2.95 era linker wouldn't have handled them correctly
at all.

A quick hack to fix your problem would be to not to use the VC++ generated
import library.  You can use a DLL file as an import library with GCC,
so try this:

        copy main.exe main.dll
        gcc -shared test.c main.dll -o test.dll

That should be enough to make your test case work.

                                        Ross Ridge


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Unable to link to an exe's import library.

Dave Wilcoxson
Ross Ridge writes:
> A quick hack to fix your problem would be to not to use the VC++ generated
> import library.  You can use a DLL file as an import library with GCC,
> so try this:
>
>     copy main.exe main.dll
>     gcc -shared test.c main.dll -o test.dll

This gives an undefined reference to Increment (the exported function from my
exe) during link.  It definitely seems like a dllexport from an exe is being
treated differently than one from a dll by the linker.

--Dave



     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Unable to link to an exe's import library.

Ross Ridge
In reply to this post by Dave Wilcoxson
Ross Ridge writes:
> A quick hack to fix your problem would be to not to use the VC++ generated
> import library.  You can use a DLL file as an import library with GCC,
> so try this:
>
>     copy main.exe main.dll
>     gcc -shared test.c main.dll -o test.dll

Dave Wilcoxson writes:
> This gives an undefined reference to Increment (the exported function
> from my exe) during link.

Try again.  If you compile your test case using the following commands
it works:

        cl /EHsc main.cpp
        copy main.exe main.dll
        gcc -shared test.c main.dll -o test.dll
        del main.dll

Running main.exe then gives the following output:

        x=4
        Calling increment from dll
        Increment
        Done incrementing
        x=6

The linker will only use your executable as an import library if it has a
".dll" extension.

                                        Ross Ridge


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users