putenv() doesnt really export the variable

Hi, I am trying to export LD_LIBRARY_PATH variable using putenv() in my code. but when executing the binary, it still prompts for the necessary libraries. But when I export the LD_LIBRARY_PATH from command prompt using export, the binary is able to link to the libraries. Where am I going wrong?

Let me understand. You call putenv() inside a program that relies on LD_LIBRARY_PATH?

You do know that dld() opens the runtime libraries BEFORE your putenv() code is called?
How else could it access the external code for putenv? putenv is linked as a symbol reference that has to be dereferenced at runtime.

So. Part of exectuable image activation is opening the runtime libraries. Do you have truss or strace or trace on your machine? If so, run your code under it. Note that before the executable runs the stuff you wrote it does a lot of file mapping and file opening. Most of that is shared libraries.

One way -

Instead of all this pain, statically link your exectuable against the library that now requiresLD_LIBRARY_PATH. Usually this means supplying the name of the .a form of the library on the command line (or makefile)

cc myfile.c /some/odd/path/liboddlibrary.a  -o myfile

Now, you do not need LD_LIBRARY_PATH in order to use liboddlibrary

Hi Jim, first of all..Thanx for the reply....
The code i am using has to link with shared libraries only. and other thing is that it is using set-uid. I read that when set-uid is used, the binary will ignore LD_LIBRARY_PATH and will look into only standard paths like /usr/lib. One option i am left with is to provide runtime path while compiling. Is there any other option... Please specify

You can extend the trusted library path with crle.
You can explicitly load the shared libraries in your code with dlopen.