Monthly Archives: September 2017


CentOS 7: C++: static linking cannot find -lstdc++ -lm and -lc

Recently, we were trying to compile a C++ application with the following compilation command on a CentOS 7 64bit :


g++ -static -O2 -lm -Wall -Wno-unused-result -std=c++11 -DCS_ACADEMY -DONLINE_JUDGE 510152025.cpp -o 510152025;

unfortunately, we got the following errors:

 /usr/bin/ld: cannot find -lstdc++
 /usr/bin/ld: cannot find -lm
 /usr/bin/ld: cannot find -lc
 collect2: error: ld returned 1 exit status

To resolve the issues, we performed the following installations to install the static versions of the glibc and libstdc libraries:


sudo yum install glibc-static libstdc++-static -y;

 


Fedora 26: C++: static linking cannot find -lstdc++ -lm and -lc

Recently, we were trying to compile a C++ application with the following compilation command on a Fedora 26 64bit :


g++ -static -O2 -lm -Wall -Wno-unused-result -std=c++14 -DCS_ACADEMY -DONLINE_JUDGE 510152025.cpp -o 510152025;

unfortunately, we got the following errors:

 /usr/bin/ld: cannot find -lstdc++
 /usr/bin/ld: cannot find -lm
 /usr/bin/ld: cannot find -lc
 collect2: error: ld returned 1 exit status

To resolve the issues, we performed the following installations to install the static versions of the glibc and libstdc libraries:


sudo dnf install glibc-static libstdc++-static -y;

 


C/C++: Get a random number that is in a specific range 1

Assuming you need to generate a random number that is in a specified range, you can do the following:


//int rand(void) creates a pseudo-random number in the range of 0 to RAND_MAX
//RAND_MAX is defined in stdlib.h and is the largest number rand will return (same as INT_MAX).
const int new_number = (rand() % (maximum_number + 1 - minimum_number)) + minimum_number;

The above code first creates a pseudo-random number that is in the range of [0, RAND_MAX].
Then it will divide it with the width (+1) of the range we want to use (maximum_number + 1 - minimum_number) and get the remainder (modulo).
The modulo will be in the range of [0, maximum_number - minimum_number], so we add to it the value of minimum_number to shift the result to the proper range.
This solution, as demonstrated in the example below, works for negative ranges as well.

Full example of generating 100000 random numbers that are all in the range [-31, 32].

const int maximum_number = 31;
const int minimum_number = -32;
unsigned int i;
for (i = 0; i <= 100000; i++) {
	const int new_number = (rand() % (maximum_number + 1 - minimum_number)) + minimum_number;
	printf("%d\n", new_number);
}

C/C++: Comparing the performance of syslog vs printf

The following code tries to compare the performance of syslog() with the printf() command. [download id=”3787″]
On our machine, it appears that syslog() is faster than printf().

To be as fair as possible, when the application was executing, we were monitoring the system logs as well, so that they will be printed on screen.
On CentOS 7, you can see the syslog in the file /var/log/messages.
The command we used was: sudo tail -f /var/log/messages

Results:

printf: Seconds elapsed 0.480000
syslog: Seconds elapsed 0.180000

Full source code for test:

[download id=”3787″]


#include <stdio.h>
#include <syslog.h>
// #include <stdlib.h> is needed for the resolution of EXIT_SUCCESS
#include <stdlib.h>
// #include <time.h> is needed for the clock() function and the macro CLOCKS_PER_SEC
#include <time.h>
// #include <unistd.h> and #include <sys/types.h> are needed for the functions uid_t getuid(void); and uid_t geteuid(void);
//getuid() returns the real user ID of the calling process.
//geteuid() returns the effective user ID of the calling process.
//These functions are always successful.
#include <unistd.h>
#include <sys/types.h>

#define RANGE (100000)

int main()
{
    {
        const clock_t start = clock();

        unsigned int i;
        for (i = 0; i < RANGE; i++){
            printf ("Program started by Real User %u (Effective User %u)\n", getuid(), geteuid());
        }
        printf("\n");

        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("printf: Seconds elapsed %f\n", seconds);
    }
    {
        const clock_t start = clock();

        setlogmask (LOG_UPTO (LOG_NOTICE));
        openlog ("bytefreaks", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
        unsigned int i;
        for (i = 0; i < RANGE; i++){
            syslog (LOG_NOTICE, "Program started by Real User %u (Effective User %u)", getuid(), geteuid());
        }
        closelog ();

        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("syslog: Seconds elapsed %f\n", seconds);

    }
    return EXIT_SUCCESS;
}


[download id=”3787″]