Fwd: How to link and compile with gcc ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: How to link and compile with gcc ?

Timothy Madden
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
_______________________________________________
Hello

I have this problem I do not know the order libraries and object files
should be specified on the linker/gcc command line.

The gcc man page says symbols are searched in the files in the order
given on the command line, so an object file
that cames before a library can override symbols from the library. So
I reach the conclusion that if an object uses
a library the library should come first, and if  libzip uses libz,
than libz should come first.

But I find that the proper order is the opposite, as I get undefined
references if I use the libraries first.

g++ -o prog /mingw/lib/libz.a /mingw/lib/libzip.a prog.cc prog_module.cc

would give undefined references for symbols from libz and libzip, but

g++ -o prog prog.cc prog_module.cc /mingw/lib/libzip.a /ming/lib/libz.a

would compile and link successfully.

I also tried

g++ -o prog -static -static-libgcc prog.cc prog-module.cc -lzip -lz

And then I get undefined references to snprintf and strtol from libstdc++.a.

Do you know how can I get libzip and lz linked with -l switch to g++ ?

And how do I know the correct order of libraries on the command line if
I want to explicitly list the library files on the command line ?

How can I see all symbols exported from an object file ?

And how do I use functions in a .dll with MinGW and g++ ?
For example I want to use gtk+. I do not know if it compiles
with MinGW, but even if it does it looks like a complicated
task with little chances of success for me, so I would like
to use a pre-compiled .dll and call functions in it. How do I
do this ?

Thank you,
Timothy Madden

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: How to link and compile with gcc ?

Greg Chicares
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
_______________________________________________
On 2008-11-22 21:28Z, Timothy Madden wrote:

>
> I have this problem I do not know the order libraries and object files
> should be specified on the linker/gcc command line.
>
> The gcc man page says symbols are searched in the files in the order
> given on the command line, so an object file
> that cames before a library can override symbols from the library. So
> I reach the conclusion that if an object uses
> a library the library should come first, and if  libzip uses libz,
> than libz should come first.

It's the other way around. On my machine, 'man ld' says (for '-l'):

| The  linker will search an archive only once, at the location where
| it is specified on the command line.  If the archive defines a sym-
| bol  which  was  undefined in some object which appeared before the
| archive on the command line, the linker will include the  appropri-
| ate  file(s)  from the archive.  However, an undefined symbol in an
| object appearing later on the  command  line  will  not  cause  the
| linker to search the archive again.

So you want:
  gcc -o whatever object_that_uses_libfoo.o -lfoo
The '.o' file precedes its required library.

> Do you know how can I get libzip and lz linked with -l switch to g++ ?

You said that this worked:

g++ -o prog prog.cc prog_module.cc /mingw/lib/libzip.a /ming/lib/libz.a

(although you probably don't really have both /mingw/ and /ming/)
so try:

g++ -o prog prog.cc prog_module.cc -lzip -lz

and if that doesn't work then use '-L' (before '-l') to indicate
where the libraries are to be found.

> How can I see all symbols exported from an object file ?

Use the 'nm' program.

> And how do I use functions in a .dll with MinGW and g++ ?

Here's one example:

http://www.mingw.org/MinGWiki/index.php/sample%20DLL

> For example I want to use gtk+. I do not know if it compiles
> with MinGW, but even if it does it looks like a complicated
> task with little chances of success for me, so I would like
> to use a pre-compiled .dll and call functions in it. How do I
> do this ?

http://www.gimp.org/~tml/gimp/win32/

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users