0

Reference: Weak symbol

In the link the following linker option is mentioned:

cc  main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main2

The purpose of all of the above flags have been documented in the GCC Manual, except -R.

What does this flag instruct the linker?

TIA

Vinod

0

2 Answers 2

2

For GCC, this is just one option, not two:

-Wl,-R`pwd` 

The -Wl,<something> construct is used to pass the <something> part as option(s) to the linker (ld). In this case, it tells gcc to invoke ld as:

ld -R`pwd` <other parameters...>

And if you look at man ld in Linux, you'll find (emphasis mine):

-R filename

[...]

Read symbol names and their addresses from filename, but do not relocate it or include it in the output. This allows your output file to refer symbolically to absolute locations of memory defined in other programs. You may use this option more than once.

For compatibility with other ELF linkers, if the -R option is followed by a directory name, rather than a file name, it is treated as the -rpath option.

And the -rpath option is:

-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. [...]

So the entire construct means "tell the runtime linker to search shared objects for linking in the current working directory".

The ld man page also notes:

The -rpath option may also be used on SunOS. By default, on SunOS, the linker will form a runtime search path out of all the -L options it is given. If a -rpath option is used, the runtime search path will be formed exclusively using the -rpath options, ignoring the -L options. This can be useful when using gcc, which adds many -L options which may be on NFS mounted file systems.

So, the overall goal for this option construct in e.g.

cc -g -c -o main.o main.c
cc -g -fpic -c -o power_slow.po power_slow.c
cc -shared -fpic -o libpowerslow.so power_slow.po
cc  main.o -L`pwd` -Wl,-R`pwd` -lpowerslow -o main

is to ensure the resulting main binary will search for libpowerslow.so only in the current working directory, so it will pick the exact version compiled by the third command, and not any other version of libpowerslow.so that might exist elsewhere on the system.

You should realize that while "baking in" the current working directory is useful with short-lived example binaries like the one built here, you should specify something else as the runtime library search path when building something to be installed for system-wide use.

0

From the ld manual page

-R filename
--just-symbols=filename
.....
For compatibility with other ELF linkers, if the -R option is followed by a directory name, rather than a file name, it is treated as the -rpath option.

It means that the executable will add the current directory to the list of places that the runtime linker will search for shared libraries, which will help it locate the powerslow library.

It would be less confusing to use -rpath rather than -R.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.