How do I suppress output while using a dynamic library?

gnu ld options
qt shared library example
--whole-archive
gnu ld manual
linux ld command examples
gcc linker options
gcc linker script
how to add library in qt pro file

I actually have a solution to this problem, but I'm wondering if there is a slicker one.

I have the need to load in a library to my utility using dlopen and then call one of the functions.

Unfortunately, the function spews a whole bunch of information out onto STDOUT and this I do not want.

I have a solution that is non-portable and I'm wondering if there's a better, more generic solution that I could use.

Here's what I have (NB: This is C) :

/*
 * Structure for retaining information about a stream, sufficient to
 * recreate that stream later on
 */
struct stream_info {
    int fd;
    fpos_t pos;
};
#define STDOUT_INFO 0
#define STDERR_INFO 1

struct stream_info s_info[2];
point_stream_to_null(stdout, &s_info[STDOUT_INFO]);
point_stream_to_null(stderr, &s_info[STDERR_INFO]);

void *output = noisy_function();

reset_stream(stderr, &s_info[STDERR_INFO]);
reset_stream(stdout, &s_info[STDOUT_INFO]);

/*
 * Redirects a stream to null and retains sufficient information to restore the stream to its original location
 *** NB ***
 * Not Portable
 */
void point_stream_to_null(FILE *stream, struct stream_info *info) {
    fflush(stream);
    fgetpos(stream, &(info->pos));
    info->fd = dup(fileno(stream));
    freopen("/dev/null", "w", stream);
}

/*
 * Resets a stream to its original location using the info provided
 */
void reset_stream(FILE *stream, struct stream_info *info) {
    fflush(stream);
    dup2(info->fd, fileno(stream));
    close(info->fd);
    clearerr(stream);
    fsetpos(stream, &(info->pos));
}

Any suggestions?


I have a suggestion, which lets you use the preprocessor for portability, or perhaps "portability".

If you try something like

#if defined __unix__
#define DEVNULL "/dev/null"
#elif defined _WIN32
#define DEVNULL "nul"
#endif

(ignoring other OSes, else case, error directive, etc.) and then reopen the file as before

FILE *myfile = freopen(DEVNULL, "w", stream);

then that may give you what you want.

I haven't tried this at home, though. The "nul" file exists; see /dev/null in Windows. And you can get predefined macros at "Pre-defined C/C++ Compiler Macros".

Using LD, the GNU linker, You can also use `-b' to switch formats explicitly (when linking object files of The GNU linker will ignore the -F option when not creating an ELF shared object. If a successfully built fully linked output file does not have a .exe or .dll suffix, this� To verify that everything works so far, compile the dynamic link library. To compile, choose Build > Build Solution on the menu bar. The DLL and related compiler output are placed in a folder called Debug directly below the solution folder. If you create a Release build, the output is placed in a folder called Release. The output should look


You could try using setvbuf to set stdout to have a very large buffer and be fully buffered. Then, after every call to noisy_function, clear out the buffer before flushing it to the stream. I think this invokes undefined behavior though.

Another way would be to redirect stdout to a temp file, like with this macro function.

#include <stdio.h>

#define QUIET_CALL(noisy) { \
    FILE* tmp = stdout;\
    stdout = tmpfile();\
    (noisy);\
    fclose(stdout);\
    stdout = tmp;\
}

int main(){
    QUIET_CALL(printf("blah blah"));
    printf("bloo bloo\n");
    return 0;
}

Libtool: Link mode, If output-file is a program, then do not link it against any shared libraries at all. In general, this flag cannot be used together with ' disable-static ' (see LT_INIT). For an example that uses myPuts, see Using Load-Time Dynamic Linking or Using Run-Time Dynamic Linking. // The myPuts function writes a null-terminated string to // the standard output device. // The export mechanism used here is the __declspec(export) // method supported by Microsoft Visual Studio, but any // other export method supported by


In Windows you can redirect streams too. See http://support.microsoft.com/kb/110930/en-us

How can I capture stdout of a generic shared library method called , I am calling this as part of a C-shared library (DLL) via LOADLIBRARY and CALLLIB. I am unable to view the output when it is called from MATLAB on a Windows //Redirect output to new console so that Matlab users can see error messages. You can use this syntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map. -u symbol. Pretend the symbol symbol is undefined, to force linking of library modules to define it.


Unfortunately, freopening to a platform-specific null filename is about the closest you can get in standard C. You could also consider modifying the library itself to not spew so much output on stdout.

That said, in practice, the only OSes you need to worry about are either unix-based (including MacOS) or Windows - in the case of Windows, stdout is hidden by default, so you can just skip the redirection step, and for *nix you have the code already.

How to create a library with Qt and use it in an application, [hide]. 1 Introduction; 2 Creating a shared library. 2.1 Linking your application On Windows, MinGW will output .a and .dll, MSVC will output .lib and .dll. In order to use the shared library in your application, then you can� An import library is a library that automates the process of loading and using a dynamic library. On Windows, this is typically done via a small static library (.lib) of the same name as the dynamic library (.dll). The static library is linked into the program at compile time, and then the functionality of the dynamic library can effectively be


For Windows console applications:

freopen("NUL", "w", stdout); // redirect stdout to the windows version of /dev/null
NoisyDllFunction();
freopen("CON", "w", stdout); // redirect stdout back to the console

Worked for me.

Chapter 4 Libraries (Fortran Programming Guide), This chapter describes how to use and create libraries of subprograms. LINK EDITOR MEMORY MAP output input virtual section section address size .interp For example, you might have built a.out with your own dynamic libraries in / my Then perform the link step with -nolib (to suppress automatic linking of system� Using a stored procedure with output parameters. 08/12/2019; 3 minutes to read +2; In this article. Download JDBC Driver. A SQL Server stored procedure that you can call is one that returns one or more OUT parameters, which are parameters that the stored procedure uses to return data back to the calling application.


nm -- display symbol table of object, library, or executable file, nm displays the symbol table associated with an object, archive library of library files, which normally end in .lib and contain one or more OMF or COFF files. This is the default, since this implementation does not suppress any output. -g. Dynamic libraries are different from static libraries in a way that by using them, during compilation process, GCC ads only code "hooks" and soname. That "hooks" and library soname are used during the startup of your application to load correct library and connect "inside" with the "outside" code. There are many advantages of using dynamic


QCC, qcc, -Bdynamic: Link dynamically against any subsequent libraries on the command line. Note that the make utility, when used with the default settings, produces an output file with the same name as -w: Suppress all warnings (same as -w0). Dynamic library developers can set a different install name for a library when they compile it using the gcc -install_name option. See the gcc man page for details. The dynamic loader resolves only the undefined external symbols the app actually uses during the launch process.


contextlib — Utilities for with-statement contexts — Python 3.8.5 , While many objects natively support use in with statements, sometimes a resource from contextlib import suppress with suppress(FileNotFoundError): effect on sys.stdout means that this context manager is not suitable for use in library� On Unix, a dynamic library can be specified as input to the linker when code using the dynamic library is linked. On Windows, except when using GCC, dynamic libraries are not specified directly as input to the linker; instead, an import library or module definition file is used.