Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

environment variables in ld scripts

508 views
Skip to first unread message

Jay Braun

unread,
Apr 22, 2013, 8:31:19 PM4/22/13
to
I have inherited an application that makes extensive use of ld scripts. The former developer hard-coded many absolute directory paths into these scripts, e.g.:

-L /home/harold/libs -( -lnwiss -lr -)
@/home/harold/libs lib.ld

I would like to use environment variables so that I can use, say /home/jay/ rather than /home/harold. So I'll define:

export APP_LIBS=/home/jay/libs

but neither of the following works:

-L $APP_LIBS -( -lnwiss -lr -)
@$APP_LIBS lib.ld

Is there a special notation for employing environment variables in ld scripts?

Thanks,
Jay

Tauno Voipio

unread,
Apr 23, 2013, 7:50:56 AM4/23/13
to
This seems more as the shell script to start ld (or maybe even gcc),
than the ld script.

Please check.

--

Tauno Voipio


John Reiser

unread,
Apr 23, 2013, 11:34:29 AM4/23/13
to
> I have inherited an application that makes extensive use of ld scripts. ...

> -L $APP_LIBS -( -lnwiss -lr -)
The shell expands environment variable APP_LIBS here as part of a ld (or gcc) command line.

> @$APP_LIBS lib.ld
Inside a linker script, then ld does not expand shell environment variables.

> Is there a special notation for employing environment variables in ld scripts?
No; instead you must perform the expansion before invoking ld.
One way to do this is with a shell "Here Document" which is invoked by the syntax "<<".
See the documentation for any shell, such as "man bash" or "info bash".
Example:

cat - > my_linker_script.txt <<EOF
@$APP_LIBS lib.ld
EOF

The shell will expand "$APP_LIBS" as it reads the lines until EOF.
The utility program 'cat' then writes those lines to its stdout,
which in this case is re-directed to file "my_linker_script.txt".

ld ... -T my_linker_script.txt ...

Use the file my_linker_script.txt with the already-expanded environment variables.

--

Jay Braun

unread,
Apr 23, 2013, 3:56:08 PM4/23/13
to

Jay Braun

unread,
Apr 23, 2013, 3:56:28 PM4/23/13
to
We decided to build a bash script that generates the ld script with a multi-line echo. Environment variables are part of the script, and are written to the temporary ld file as full path names. The bash script them does a "chmod +x" on the ld file, and executes it. The ld file can then be deleted because the bash script has the permanent contents.

Thanks very much,
Jay

On Tuesday, April 23, 2013 8:34:29 AM UTC-7, John Reiser wrote:
> > I have inherited an application that makes extensive use of ld scripts. ...
>
>
>
> > -L $APP_LIBS -( -lnwiss -lr -)
>
> The shell expands environment variable APP_LIBS here as part of a ld (or gcc) command line.
>
>
>
> > @$APP_LIBS lib.ld
>
> Inside a linker script, then ld does not expand shell environment variables.
>
>
>
> > Is there a special notation for employing environment variables in ld scripts?
>
> No; instead you must perform the expansion before invoking ld.
>
> One way to do this is with a shell "Here Document" which is invoked by the syntax "<<".
>
> See the documentation for any shell, such as "man bash" or "info bash".
>
> Example:
>
>
>
> cat - > my_linker_script.txt <
0 new messages