This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Can't build a dynamically loadable library


(I apologize for any formatting problems in the
following.  I tried to post from my mail server but
you have it blocked.  :-(  )

I want to build a dynamically loadable library that
resolves certain
variables and functions from the main executable.

These sample source files illustrate.  file1.c and
file2.c
are to be linked into the dynamic library.  themain.c
will be
the main application.

======
/* file1.c */
#define EXP __declspec (dllexport)
extern int i_main;      /* Defined in main.c */
int f_main(int);
int file2(void);
EXP int dd(int i) {
        printf("In d,i=%i\n", i);
        return f_main(i) + i_main + 5 + file2();
}

/* file2.c -- Just so Fergus H.'s code will run */
int file2(void) { return 1; }
/* themain.c */
int i_main = 42;
int f_main(int i) {
        return 10*i;
}
int dd(int);

int main(void) {
        int answer = dd(100);
        printf("dd(100)=%d\n", answer);
        return 0;
}

======

"Fergus H." is Fergus Henderson.  I based my procedure
on a 1997 posting
of his.  (By the way, I found that posting in the DJ
Delorie archives
referenced from the Cygwin main page.  The latest
posting I saw was
dated 1997.  Are these the archives I should be
searching?)

Here's what I tried and the results in each case:

1. ld file1.o file2.o -o whatever.dll
ld refuses to create whatever.dll because of the
undefined references
in file1.o and file2.o.

2. Same as above, but with a --no-inhibit-exe.  This
time ld creates
whatever.dll but includes some startup code in it. 
This gives me
multiple-definition errors when I try to link.

3. The following, which is adapted from Fergus H.'s
posting:

=====
# compile up the parts of the dll

gcc -c file1.c       
gcc -c file2.c

# put them in a library (you don't have to, you
# could name all the .os on the dlltool line)

ar  qcv thedll.in file1.o file2.o
ranlib thedll.in

# JMS:  Added this
dlltool -v -z thedll.def --no-export-all-symbols
thedll.in

# run this tool over the library and the def file
# JMS:  Removed leading './'
dlltool --def thedll.def --output-exp thedll.o
--output-lib thedll.a

# build the dll with the library with file1.o, file2.o
and the export
table
ld -o thedll.dll thedll.o thedll.in

# build the mainline
gcc -c themain.c 

# link the executable with the import library
# JMS:  Removed -Tthemain.ld.   Added --export-dynamic
# JMS:  Changed ld to gcc and removed -e main.
gcc -Wl,--export-dynamic -o themain.exe themain.o
thedll.a

=====

4. I also tried Mumit Khan's dllwrapper but couldn't
get it to
work.  Again, ld complained about those pesky
unresolveds.

My questions are:

A. How can I get the above example to work?

B. What, if anything, do I change if the code is in
C++ instead of C?

C. What, if anything, do I change if I want to use the
dlopen(),
dlsym() and dlclose() calls instead of depending on
the usual
kind of dynamic linking.

I am running Cygwin Beta 20.1 under NT.

Thank you.


===
-- 
Opinions expressed above are not necessarily my employer's.
James M. Stern
ITG Inc.  Culver City, CA (213) 270-7955
__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]