Archive

Archive for January, 2020

C compiler for Windows

January 15, 2020 Leave a comment

Problem
You need a C compiler under Windows.

Solution
At https://nuwen.net/mingw.html you can find a MinGW distribution, which is x64-native and contains GCC and Boost. Download the installer, install it, and add its “bin” folder to your PATH.

Then you have a C compiler under Windows.

Using sqrt in C lang.

January 3, 2020 Leave a comment

Problem
The following code compiles with “gcc in.c“:

#include 
#include 

int main()
{
    double result = sqrt(9.0);
    printf("%lf\n", result);

    return 0;
}

The following code drops an error if you try to compile with “gcc in2.c“:

#include 
#include 

int main()
{
    double value = 9.0;
    double result = sqrt(value);
    printf("%lf\n", result);

    return 0;
}

The error message says: “undefined reference to `sqrt'”.

Solution and Explanation
In the second case, compile it with “gcc -lm in2.c“, which links the math library. But why does the first example work? In that case the compiler figures out (knows) the value of the square root operation and replaces the function call with a constant 3.0 value. The optimizer is that clever :) In the second case however it tries to call the sqrt() function but it’s not found. If you link the math library, it’ll find it.