g++


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;

 


g++ not found on Fedora 25

On a Fedora 25 (64bit) we got the error g++ not found.

We could have installed g++ using:


sudo dnf install gcc-c++ -y;

But we wanted to install all common additional development tools that we might need for C/C++ development in the future without going over the list of available packages to find which ones.
To do so, we installed all the packages of the group that is marked to be used for C development using dnf as follows:


sudo dnf group install "C Development Tools and Libraries" -y;


C/C++: How NOT to define multiple pointers

When defining pointers in C/C++ you should be careful on how you use the * characters. If you try to define multiple pointers on the same line and you do not add the * character in front of each variable, then the results will not be what you would expect. In the following examples we added the * to the data type definition, hoping that all variables would become pointers of that data type. Unfortunately, as the compiler points out later on while making the comparisons, only the first variable in each line becomes a pointer of the data type.

Wrong Examples

C Source Code:

#include <stdio.h>

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

    return 0;
}

Compiler Output:

$ gcc -o main *.c                                                                                          
main.c: In function 'main':                                                                                      
main.c:9:7: warning: comparison between pointer and integer                                                      
     a == b;                                                                                                     
       ^                                                                                                         
main.c:10:7: warning: comparison between pointer and integer                                                     
     c == d;                                                                                                     
       ^                                                                                                         
main.c:11:7: warning: comparison between pointer and integer                                                     
     e == f;                                                                                                     
       ^                                                                                                         

C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
    int* a, b;
    int *c, d;
    int * e, f;

    a == b;
    c == d;
    e == f;

   return 0;
}

Compiler Output:

$ g++ -std=c++11 *.cpp -o main

main.cpp: In function ‘int main()’:
main.cpp:10:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     a == b;
          ^
main.cpp:11:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     c == d;
          ^
main.cpp:12:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     e == f;
          ^

Correct Examples

#include <stdio.h>

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int* a, * b;
    int *c, *d;
    int * e, * f;

    a == b;
    c == d;
    e == f;

   return 0;
}