Home > Software engineering >  Prevent my C code from printing (seriously slows down the execution)
Prevent my C code from printing (seriously slows down the execution)

Time:11-25

I have an issue.

I finally found a way to use an external library to solve my numerical systems. This library automatically prints the matrices. It is fine for dim=5, but for dim=1.000.000, you understand the problem...

Those parasite "printf"s slow down considerably the execution, and I would like to get rid of them. The problem is: I don't know where they are ! I looked in every ".h" and ".c" file in my library: they are nowhere to be found.

I suspect they already are included in the library itself: superlu.so. I can't access them, thus. libsuperlu

How could I possibly prevent my C code from printing anything during the execution ?

Here is my Makefile. I use the libsuperlu-dev library, directly downloaded from Ubuntu. The .so file was already there.

LIB = libsuperlu.so

main: superlu.o read_file.o main.o sample_arrays.o super_csr.o
    cc $^ -o $@ $(LIB)

clean:
    rm *.o
    rm main

CodePudding user response:

Just to explain the LD_PRELOAD method that was mentioned, that I use sometimes precisely for that usage (or, on the contrary to add some printf, for example, when I want to pipe the output of a GUI), here is how you can do a rudimentary version of it

myprint.c:

int printf(char *, ...){
    return 0;
}

int putchar(int){
    return 0;
}

Then

gcc -shared -std=gnu99 -o myprint.so myprint.c

Then

LD_PRELOAD=./myprint.so ./main

Forces the load of your printf and putchar symbols before any other library has the opportunity to load them force. So, no printing occurs. At least none with printf. But you may have to add some other functions to the list, such as fprintf, fputc, fputs, puts, ...

And of course, another problem of overloading the fthing functions (and even possibly the others), is that you might also prevent some wanted behavior. Such as writing files. Or interacting with some devices.

It may be even worse if those printing are done with low level write function. That one, you very likely can't afford to overload (unless you overload it with a function that calls the real write, loaded manually by dlopen) filtering only the ones that you want to avoid, based on target file descriptor (1) or on content of written data.

Note: if you want to verify if the libsuperlu.so is responsible of those printing, you can check with nm libsuperlu.so if it is referring to some well known printing functions, such as printf

  • Related