DLL build using gcc under cygwin.

Hi.

I'm trying to build dll on WinNT platform usign gcc under cygwin, that aftewards will be used in MSVC. I wrote small example with just one function :
__declspec(dllexport) __stdcall int foo();

int foo()
{
     return 1;
}

na built a dll from it using following command :
gcc -c dll_main.c -o dll_main.o
gcc -Wl,--out-implib,dll.a -shared -o dll.dll dll_main.o

afterthat I built a project in VS with only one file looking :
__declspec(dllimport) int __stdcall foo();

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

and compiled it with dll.a. Everything was fine, compilation succed.

But now I have a problem, when I'm executing it, it falls with exception. Debugging shows that call to foo points to some uninitialized memory.

If someone knows how to solve it, please answer.

Thanks.
dpodvalnAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Hmm - I am already wondering how VC uses a lib produced by gcc... Try the following steps to create an import lib from your gcc-made DLL using VC:

Step.1 - Get the list of functions. 'dumpbin.exe /exports thedll.dll' will
display the exports along with any lingering decorations.

Step.2 - Make a .def file for the dll. It should look like:

EXPORTS
DllFunction1
DllFunction2
DllFunction3

Step.3 - use the MSVC LIB.EXE command line tool to make a .lib using only
the .def

C:\Some Path>lib /def:mydef.def

0
 
makerpCommented:
extern "C" in front of function declaration and definition
0
 
dpodvalnAuthor Commented:
doesn't help
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
makerpCommented:
in vs project as well
0
 
dpodvalnAuthor Commented:
both of them are written in C already so extern "C" just cause to compilation error.

As I said I succed to compile it, I have a run time error.
0
 
makerpCommented:
have you put extern "C" in front of your declaration in VC++ project aswell
0
 
dpodvalnAuthor Commented:
yes, it caused to compilation error, so I removed it.

It written in C!!! You can't use extern "C" in c files. C compiler returns error on such directive.
0
 
jkrCommented:
Hmm, what I am missing in your code is the calling convention (WINAPI), e.g.:

__declspec(dllexport) __stdcall int WINAPI foo();
__declspec(dllimport) __stdcall int WINAPI foo();


Does it work when you load the DLL dynamically, e.g.

typedef int ( WINAPI *PFOO)();

HINSTANCE hFooDll = LoadLibrary ( "foo.dll");

PFOO foo = GetProcAddress ( hFooDll, "foo");

foo ();
0
 
dpodvalnAuthor Commented:
jkr : You right it has __stdcall.I forgot to mention it.
It works when I load DLL dynamically.
I suppose that problem is in the lib file.
0
 
dpodvalnAuthor Commented:
jkr : It solves it. Thanks a lot.
I'll accept your comment as answer.  
But now I have another problem. When I add call to printf in foo function it causes to access violation. Do you have a guess what can be a cause?

Thanks.
0
 
jkrCommented:
>>When I add call to printf in foo function it causes to
>>access violation

Hard to tell - do you link the C runtime statically to your DLL/EXE? If not, try it for both...
0
 
dpodvalnAuthor Commented:
jkr : It solves it. Thanks a lot.
I'll accept your comment as answer.  
But now I have another problem. When I add call to printf in foo function it causes to access violation. Do you have a guess what can be a cause?

Thanks.
0
 
jkrCommented:
dpodvaln - as you are using IE, try not to use "Refresh" for these pages :o)
0
 
dpodvalnAuthor Commented:
Yes, i noticed, sorry.
0
 
dpodvalnAuthor Commented:
jkr : how do I link it statically?
0
 
jkrCommented:
On VC++: Go to "Project Settings", the "C++" tab, choose "Code Generation" from the ComboBox and select the appropriate option that does NOT read "DLL" - the equivalent is using "/MT" from the command line (as opposed to "/MD")
0
 
dpodvalnAuthor Commented:
it works with flag -mno-cygwin on the linkage stage.
But I still don't know how to make it work with the cygwin library...
0
 
jkrCommented:
The problem is that the MS CRT and the cygwin CRT differ but implement the same functions - that's why you should link statically at least regarding the VC++ part...
0
 
dpodvalnAuthor Commented:
jkr : I tried to link statically(multithreaded DLL) on VC it doesn't help. Im afraid that some kind of initialization of cygwin is missing.
0
 
jkrCommented:
>>I tried to link statically(multithreaded DLL) on VC it doesn't help.

That's exactly what you should NOT use - I wrote 'select
the appropriate option that does NOT read "DLL"'. Just select "multithreaded" or "sigle-threaded" WITHOUT the "DLL" option.
0
 
dpodvalnAuthor Commented:
Sorry.
I tried to do this as well (multithreaded, single-threaded) without DLL it behavios the same.

The strange thing is that if I do dynamic load DLL :
HINSTANCE  dll = LoadLibrary("dll.dll");
int (__stdcall *f)(void);
f = (int (__stdcall *)(void))GetProcAddress(dll, "foo@0");
f();
// foo();
then it works.

but when I uncoment call to foo it falls already on f();

0
 
jkrCommented:
BTW, do you get along with that problemor do you need addtional help? If so, just ask here...
0
 
dpodvalnAuthor Commented:
jkr : Yes, I'm sure do need help. I didn't make a progress since my last comment. Right now I'm trying to understand why this is happenning.
You think that opening a new question might help?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.