Suppress output to cout from linked library

后端 未结 6 1151
感情败类
感情败类 2020-12-31 19:49

I need to link my C++ programs against a couple shared libraries which generate way too much output to std::cout and std::cerr rendering them both

6条回答
  •  攒了一身酷
    2020-12-31 20:31

    Since stdout (file descriptor 1) and stderr (file descriptor 2) are valid for the whole process and you can't make one part of the program have it point to a different file, there's only one way to do it: use dup(2) to duplicate them and use those file descriptors in your own code. Close the fd's 1 and 2, open /dev/null for writing and use dup2 to try to set them to 1 or 2 respectively if it isn't already. Pretty ugly, but that'd work.

提交回复
热议问题