Solved

DLL build using gcc under cygwin.

Posted on 2002-04-29
23
556 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:dpodvaln
  • 12
  • 8
  • 3
23 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 6976954
extern "C" in front of function declaration and definition
0
 

Author Comment

by:dpodvaln
ID: 6976961
doesn't help
0
 
LVL 10

Expert Comment

by:makerp
ID: 6976978
in vs project as well
0
 

Author Comment

by:dpodvaln
ID: 6976984
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
 
LVL 10

Expert Comment

by:makerp
ID: 6977023
have you put extern "C" in front of your declaration in VC++ project aswell
0
 

Author Comment

by:dpodvaln
ID: 6977037
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
 
LVL 86

Expert Comment

by:jkr
ID: 6977171
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
 

Author Comment

by:dpodvaln
ID: 6977197
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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 6977368
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
 

Author Comment

by:dpodvaln
ID: 6977425
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
 
LVL 86

Expert Comment

by:jkr
ID: 6977488
>>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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:dpodvaln
ID: 6977549
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
 
LVL 86

Expert Comment

by:jkr
ID: 6977564
dpodvaln - as you are using IE, try not to use "Refresh" for these pages :o)
0
 

Author Comment

by:dpodvaln
ID: 6977575
Yes, i noticed, sorry.
0
 

Author Comment

by:dpodvaln
ID: 6977803
jkr : how do I link it statically?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6977853
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
 

Author Comment

by:dpodvaln
ID: 6979954
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
 
LVL 86

Expert Comment

by:jkr
ID: 6980406
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
 

Author Comment

by:dpodvaln
ID: 6980495
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
 
LVL 86

Expert Comment

by:jkr
ID: 6980658
>>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
 

Author Comment

by:dpodvaln
ID: 6980709
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
 
LVL 86

Expert Comment

by:jkr
ID: 6984915
BTW, do you get along with that problemor do you need addtional help? If so, just ask here...
0
 

Author Comment

by:dpodvaln
ID: 6985494
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now