?
Solved

DLL build using gcc under cygwin.

Posted on 2002-04-29
23
Medium Priority
?
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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