?
Solved

DLL build using gcc under cygwin.

Posted on 2002-04-29
23
Medium Priority
?
574 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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

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 2000 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
 

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Screencast - Getting to Know the Pipeline

807 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