I don't understand -Wl,-rpath -Wl,
Asked Answered
R

5

323

For convenience I added the relevant manpages below.

My (mis)understanding first: If I need to separate options with ,, that means that the second -Wl is not another option because it comes before , which means it is an argument to the -rpath option.

I don't understand how -rpath can have a -Wl,. argument!

What would make sense in my mind would be this:

-Wl,-rpath .

This should invoke -rpath linker option with the current directory argument.


man gcc:

-Wl,option

Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with `-Wl,-Map=output.map'.

man ld:

-rpath=dir

Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link;

Reinhardt answered 3/7, 2011 at 10:41 Comment(2)
Isn’t the title missing dot character at the end?Nyasaland
Yes, I suspect the SO software may have removed itCrude
A
376

The -Wl,xxx option for gcc passes a comma-separated list of tokens as a space-separated list of arguments to the linker. So

gcc -Wl,aaa,bbb,ccc

eventually becomes a linker call

ld aaa bbb ccc

In your case, you want to say "ld -rpath .", so you pass this to gcc as -Wl,-rpath,. Alternatively, you can specify repeat instances of -Wl:

gcc -Wl,aaa -Wl,bbb -Wl,ccc

Note that there is no comma between aaa and the second -Wl.

Or, in your case, -Wl,-rpath -Wl,..

Aldredge answered 3/7, 2011 at 10:47 Comment(9)
Oh I understand now, there is no discrimination between option or argument while passing stuff to the linker, it's just a string. So the second -Wl is redundant! Thanks :)Reinhardt
@Blub: It's not redundant! It's an alternative form, you either say -Wl,-rpath,. or you say -Wl,-rpath -Wl,.. Precisely one of the two, you cannot omit anything.Aldredge
Has anyone had success in setting the rpath on OS X, i.e. with clang & ld64?Salian
What would the syntax look like if you wanted to specify multiple rpaths? -Wl,-rpath -Wl,. -Wl,-rpath -Wl,/my/lib?Affixation
@Hintron: Yes, or -Wl,-rpath,dir1,-rpath,dir2, or -Wl,-rpath=dir1,-rpath=dir2.Aldredge
Just in case anyone else wastes several minutes figuring this out - like I just did - the character after -W is a lowercase 'L', not the digit 'one'! Doh!Sirois
So gcc -Wl,-rpath,. needs two commas as it adds two space-separated arguments to ld, as in ld -rpath .. Meanwhile gcc -Wl,rpath=. only needs one comma, as it adds one argument to ld, as in ld -rpath=.. See the comment from @Affixation after the answer from @Mike on support for the -rpath=PATH syntax.Womble
will -rpath . even work? I thought it was necessary to use -rpath '$ORIGIN'Corium
@thomasrutter: those mean differennt things: $ORIGIN refers to the location of the executable, . is the current working directory. $ORIGIN is useful for shipping a set of libraries next to an executable. . is a major security hazard.Aldredge
B
76

You could also write

-Wl,-rpath=.

To get rid of that pesky space. It's arguably more readable than adding extra commas (it's exactly what gets passed to ld).

Bongbongo answered 27/3, 2013 at 19:21 Comment(2)
From gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options under -Wl,option: "When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map." To me, that seems to indicate that if you use a linker other than the GNU linker, the = syntax may not be supported.Affixation
You saved my day. Every other suggestion I saw said to use -rpath='$ORIGIN', but this never worked for me, whereas -rpath=. worked perfectly. +1 for youLiam
G
45

One other thing. You may need to specify the -L option as well - eg

-Wl,-rpath,/path/to/foo -L/path/to/foo -lbaz

or you may end up with an error like

ld: cannot find -lbaz
Gunsel answered 8/3, 2012 at 1:46 Comment(0)
S
13

The man page makes it pretty clear. If you want to pass two arguments (-rpath and .) to the linker you can write

-Wl,-rpath,.

or alternatively

-Wl,-rpath -Wl,.

The arguments -Wl,-rpath . you suggested do NOT make sense to my mind. How is gcc supposed to know that your second argument (.) is supposed to be passed to the linker instead of being interpreted normally? The only way it would be able to know that is if it had insider knowledge of all possible linker arguments so it knew that -rpath required an argument after it.

Scudder answered 3/7, 2011 at 10:47 Comment(2)
it is not unthinkable that the gcc analyses arguments and if something doesn't make sense, it automatically groups.Reinhardt
@Reinhardt That (9-year-old) argument would make sense, if . were a nonsensical argument to gcc. But it's not, there are all sorts of valid reasons you could be passing . as an argument to GCC itself. Which is why, unless it's introduced with a -Wl, redirection flag in front of it, the . (or any other argument) wouldn't be included as part of the linker command line, because in the absence of an explicit -Wl, or other subcommand redirection, naturally gcc will default to consuming every argument itself.Surcease
W
0

i could solve this problem with this, change line and flag

LDFLAGS = -Wl,--export-dynamic

LIBDIR = -L insert lib dir

Willmert answered 12/12, 2023 at 8:4 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been extensively validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Heliochrome

© 2022 - 2024 — McMap. All rights reserved.