[This question is not about fixing the error. But about redirecting it]
I have a program (C/linux) which displays error on the console due to missing shared library. It says “can’t load library ….” . How can I redirect this output into a file ?
I tried this inside my program:
close(2);
open("/home/user/test.txt", O_CREAT|O_RDWR);
It correctly redirects the output generated from the program printfs. But the “can’t load library ….” still comes on console!
I don’t want to use the > operator for this purpose. I want to do it from inside my program. Any suggestions?
Thanks
The error message is generated by the loader, which happens before the program even starts. So there’s nothing you can do from within a program that doesn’t even get to run to influence the behaviour of the loader.
If you really need to fiddle with the file descriptors used by the shell, check out the
execshell command to close and redirect file descriptors permanently. That way you can get around using the redirection operator>, although that’s arguably a far less tidy approach.