How to redirect linker errors without using redirection operator?
[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 com开发者_运维问答es 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 exec
shell 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.
You will need a wrapper program. It could be a shell script. Do the redirection then try to run the original program.
A program cannot catch errors that happen to it before it even starts running. Library link happens before any other code runs.
Seems like the message is generated before your program starts - so to redirect it, you'll have to use the 2> operator. Otherwise you'll have to use the dlopen... etc. calls to do the linking on runtime.
try making sure that the environment variable LD_LIBRARY_PATH
is correct.
that warnings & error mesages comes befor your program starts to work so only option is > operator
use this way
./a.out >& filename
精彩评论