Newbie: Linker's Link order 
Author Message
 Newbie: Linker's Link order

Hi,

   I am using VC++ sp5
   Where can I view or set the link order ? What's impact (under what
condition) of the link order on the outputed exe or dll ? Any suggestion and
link is appreciated.

Thanks in advance!

Peter Lin



Sat, 20 Nov 2004 13:54:17 GMT  
 Newbie: Linker's Link order


Quote:
> Hi,

>    I am using VC++ sp5
>    Where can I view or set the link order ?

You can't. The linker is free to treat the modules you give him in any
order.

Quote:
> What's impact (under what
> condition) of the link order on the outputed exe or dll ? Any suggestion
and
> link is appreciated.

If your program is well written, it won't have any impact. Now, if you have
some buffer overrun somewhere in your code, it may happen that the symptoms
will be different if the link order change, because the buffer that is
overrun may be close to another variable, thus changing the behavior.

Arnaud
MVP - VC



Sat, 20 Nov 2004 16:18:30 GMT  
 Newbie: Linker's Link order

So there should be no effect if I  change LINK32_OBJS in the following
snippet in the makefile ( I changed it, could not found any difference).
-----------------------------------------------------------------
LINK32=link.exe
LINK32_FLAGS=kernel32.lib .... /nologo /subsystem:console /incremental:no
/pdb:"$(OUTDIR)\ex.pdb" /machine:I386 /out:"$(OUTDIR)\ex.exe"
LINK32_OBJS= \
 "$(INTDIR)\ch10.obj" \
 "$(INTDIR)\ch11.obj" \
 "$(INTDIR)\ch12.obj" \
 "$(INTDIR)\ch13.obj" \
 "$(INTDIR)\ch14.obj"
-----------------------------------------------------------------

However there is a problem from a book, I don't know if there is any
connection between the LINK ORDER and INITIALIZATION ORDER, I am not clear,I
just copy it:
-----------------------------------------------------------------

In one file you have at the global scope:

extern int y;
int x = y + 1;
and in a second file you have at the global scope:

extern int x;
int y = x + 1;

...  if the files are initialized in the order they are shown above, y
begins as statically initialized to zero, so x becomes one, and y is
dynamically initialized to two. However, if the files are initialized in the
opposite order, x is statically initialized to zero, y is dynamically
initialized to one, and x then becomes two.

-----------------------------------------------------------------

Thanks in advance!

Peter Lin


Quote:


> > Hi,

> >    I am using VC++ sp5
> >    Where can I view or set the link order ?
> You can't. The linker is free to treat the modules you give him in any
> order.

> > What's impact (under what
> > condition) of the link order on the outputed exe or dll ? Any suggestion
> and
> > link is appreciated.
> If your program is well written, it won't have any impact. Now, if you
have
> some buffer overrun somewhere in your code, it may happen that the
symptoms
> will be different if the link order change, because the buffer that is
> overrun may be close to another variable, thus changing the behavior.

> Arnaud
> MVP - VC



Sat, 20 Nov 2004 16:52:15 GMT  
 Newbie: Linker's Link order



Quote:

> However there is a problem from a book, I don't know if there is any
> connection between the LINK ORDER and INITIALIZATION ORDER, I am not
clear,I
> just copy it:
> -----------------------------------------------------------------

> In one file you have at the global scope:

> extern int y;
> int x = y + 1;
> and in a second file you have at the global scope:

> extern int x;
> int y = x + 1;

> ...  if the files are initialized in the order they are shown above, y
> begins as statically initialized to zero, so x becomes one, and y is
> dynamically initialized to two. However, if the files are initialized in
the
> opposite order, x is statically initialized to zero, y is dynamically
> initialized to one, and x then becomes two.

I see we are reading the same books:) You have read this in "Thinking in
C++", haven't you? :)

The statements above invoke undefined behavior, because you have no
guarantee of the initialization order of global variables. It is up to the
implementation (the linker) to define how it reacts in such circumstances.
Therefore, as stated in the book, don't do it or put x and y in the same cpp
file. Alternatively, you can use one of the 2 techniques explained in the
book, through I find them really clumsy.

If you want a Visual C++ specific way to solve the problem, you can also
look the #pragma init_seg directive.

Arnaud
MVP - VC



Sat, 20 Nov 2004 17:06:20 GMT  
 Newbie: Linker's Link order
Yeah! we are reading the same book, however I assume this book is just for
beginners like me. I am going through all exercises in the (highly
recommanded:)?) books list: 1. Think in C++ 2. Primer. and then Win32
programming. Long Long way to go ... Trying to become a professional is so
hard!

// Without including a header file, declare the function puts( ) from the
Standard C Library.
// Call this function from main( ).
I don't the purpose of this exercise,  I know the following is not the
answer of the exercise, but how ? headache.
//int puts( const char *string );
#include <stdio.h>
int main() {
 puts("call puts()");
 return 0;

Quote:
}

Thanks!

Peter Lin


Quote:



> > However there is a problem from a book, I don't know if there is any
> > connection between the LINK ORDER and INITIALIZATION ORDER, I am not
> clear,I
> > just copy it:
> > -----------------------------------------------------------------

> > In one file you have at the global scope:

> > extern int y;
> > int x = y + 1;
> > and in a second file you have at the global scope:

> > extern int x;
> > int y = x + 1;

> > ...  if the files are initialized in the order they are shown above, y
> > begins as statically initialized to zero, so x becomes one, and y is
> > dynamically initialized to two. However, if the files are initialized in
> the
> > opposite order, x is statically initialized to zero, y is dynamically
> > initialized to one, and x then becomes two.

> I see we are reading the same books:) You have read this in "Thinking in
> C++", haven't you? :)

> The statements above invoke undefined behavior, because you have no
> guarantee of the initialization order of global variables. It is up to the
> implementation (the linker) to define how it reacts in such circumstances.
> Therefore, as stated in the book, don't do it or put x and y in the same
cpp
> file. Alternatively, you can use one of the 2 techniques explained in the
> book, through I find them really clumsy.

> If you want a Visual C++ specific way to solve the problem, you can also
> look the #pragma init_seg directive.

> Arnaud
> MVP - VC



Sat, 20 Nov 2004 17:38:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Load order and DLL entry order of implicitly linked DLLs, Environment variables

2. Changing Order Of Linker Arguments (OBJ-Files) from IDE

3. Linker problem that depends on *.h include order

4. Controlling the linker and function ordering

5. 'ORDER BY' problems

6. order of 'external' declarations

7. Order of 'global' object creation

8. Linker cannot open file 'mfc70ud.lib'

9. Linker error L2029: '_asciiopen' : unresolved external

10. Linker error L2029: '_asciiopen' : unresolved external

11. Linker gives 'internal error'

12. Insert Function for an ordered linked list

 

 
Powered by phpBB® Forum Software