0

I get following error message when I compile my program. Does anybody know what's going on?

collect2: error: ld returned 1 exit status

Program

#include <math.h>
#include <stdbool.h>
#include <stdio.h>

//  (a) Begin the definition of a function called get_array_product
//  which computes the product of the finite elements of an array
//  of double precision floating point values.
//  
//  Parameters:
//      items     - an array of double precision floating point values.
//      num_items - an int which specifies the maximum number of items 
//                  to process.
//
//  Returns:
//      A double precision floating point value:
//      *   If the array contains at least one finite element: the result
//          is equal to the product of the finite elements.
//      *   Otherwise: return NAN.

double get_array_product( double items[], int num_items)
{
    //  (b) Insert logic required to solve the problem here.
    long int result = 0;
    int product = 1;
    for (int i = 0; i < num_items; i++){

        int t = isfinite(items[i]);

        if (t == 0){
            return t;
        }
        else{
            product =+ items[i] * product;
            result = product;
        }
    }
    return result;
}

void run_test(const char * label, double x[], int count)
{
    double prod = get_array_product(x, count);
    printf("%s\n", label);
    printf("\tInput data:\n");

    for (int i = 0; i < count; i++)
    {
        printf("\t%d\t%f\n", i, x[i]);
    }

    printf("\tProduct = %f\n\n", prod);
}

int main(void)
{
    double x1[] = { 0 };
    run_test("Count == 0", x1, 0);

    double x2[] = { NAN, +INFINITY, -INFINITY };
    run_test("No finite values", x2, 3);

    double x3[] = { 1, 2, 3, 4, 5, 6, 7 };
    run_test("Several finite values", x3, 7);

    double x4[] = { 2, M_PI, NAN, 3, INFINITY, 4 };
    run_test("A mix of finite values and infinities", x4, 6);

    double x5[] = { 1 };
    run_test("Product is 1", x5, 1);

    double x6[] = { 1.0, NAN, 1.0, 1.0, INFINITY };
    run_test("Product is also 1", x6, 5);
}

*EDIT the command line used is

gcc -std=gnu99 -Wall -Werror -g array_product.c -o array_product -I../../ZDK -L../../ZDK -I../../TestLib04 -L../../TestLib04 -ltestlib04 -lzdk -lncurses -lm
alk
  • 69,737
  • 10
  • 105
  • 255
  • How are you compiling? – kiran Biradar Aug 20 '18 at 08:00
  • 3
    There is other info in the error message around that. Please post all the relevant parts and your compile command line. – Mat Aug 20 '18 at 08:00
  • 3
    Tangential to the point: Are you sure about `=+` in `product =+ items[i] * product;`? – babon Aug 20 '18 at 08:02
  • 3
    `ld` is the linker, which probably complains about not finding a symbol you use. Since you're importing `math.h`, are you also giving the compiler option `-lm` to link your program against the math library? https://stackoverflow.com/a/103431/5629418 – Roland Weber Aug 20 '18 at 08:03
  • ... and are you sure that `int product = 1;` shouldn't be `double product = 1;`? – Jabberwocky Aug 20 '18 at 08:45
  • Apologies, here is the compile command line, gcc -std=gnu99 -Wall -Werror -g array_product.c -o array_product -I../../ZD K -L../../ZDK -I../../TestLib04 -L../../TestLib04 -ltestlib04 -lzdk -lncurses -lm – Thrillhouse Aug 20 '18 at 11:19
  • And whats the error message? – tkausl Aug 20 '18 at 11:51
  • The error message is the 'collect2 error ld returned 1 exit status'. – Thrillhouse Aug 20 '18 at 11:57
  • You're compiling with includes and libraries you don't need. What do you get if you compile as `gcc -g -Wall -Wextra -o array_product array_product.c -lm` ? – dbush Aug 20 '18 at 12:41

0 Answers0