How to get RPATH with $ORIGIN to work on Code::Blocks GCC?

kbluck picture kbluck · Oct 23, 2008 · Viewed 7.6k times · Source

I'm trying to link an RPATH containing the special string $ORIGIN into an executable built using GCC with the Code::Blocks IDE. I've specified

-Wl,-R$ORIGIN

in the linker options for the project, but the command line output to GCC is wrong (stripped for clarity):

g++ -Wl,-R

What is the correct way to specify this argument for Code::Blocks?

Answer

kbluck picture kbluck · Oct 23, 2008

Whoever decided to make the token $ORIGIN is an evil bastard who deserves a special place in programmer hell. Since '$' is a special character for bash and other scripting languages like make, it screws everything up unless carefully escaped. Even worse, depending on which build environment you're using, the specifics of how to escape properly will likely change.

In bash, you need to stick a backslash in front of the $:

-Wl,-R\$ORIGIN

Code::Blocks apparently also treats the $ as special. Then, whatever subprocess controller Code::Blocks sends the command to treats the backslash as special. So, both the backslash and the $ need to be doubled up to get escaped properly. Therefore, in Code::Blocks linker settings, you need to specify:

-Wl,-R\\$$ORIGIN

...which outputs:

-Wl,-R\\$ORIGIN

...to the build log, but the shell actually gets sent:

-Wl,-R\$ORIGIN

...which as mentioned above produces the desired result.

What a pain.