Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve "undefined reference to `main'" error in gitbash terminal (possible duplicate)

I am getting to know C and I have encountered an issue I am not sure how to solve. I have tried to look at other questions on here, but I do not think that my issue is the same, as I do have an int main()-class.

I have three files with several doubles separated by new line. They each look something like this:

...doubles
7540.39
-7741.01
-6349.83
-7670.89
1357.22
-9087.59
-2514.53
1896.5
5708.62
8541.45
7948.56
-8000.89
4115.98
6805.74
9513.29
-3964.84
-7187.46
945.873
-2032.98
-4842.12
-174.058
-2470.14
-1055.73
-8893.61
2755.39
...more doubles

The code I am trying to run is only going to take in the three files and sum over them:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]) {

    // File variable (saved as fp)
    FILE * fp;

    // Character variable (saved as line)
    char * line = NULL;

    size_t len = 0;
    ssize_t read;

    // path to files
    char filename[40] = "../input/file";

    // The # of the file (file0, file1, file2)
    int rank;

    // Check that we get correct # for file
    if (argc <=1 ) {
        printf("Missing argument, an integer in the range 0 to 2.\n");
    } else {

        // atoi() converts numeric strings into an integer value
        rank = atoi(argv[1]);

        /* Format filename as "../input/file" + rank */

        // Create space for rank and
        char numchar[2];

        // Write rank into char array
        sprintf(numchar,"%d",rank);

        // Appending rank to "filename"
        strcat(filename, numchar);

        // Open file in read mode
        fp = fopen(filename, "r");

        // Check for error when opening file
        if (fp == NULL) {
            printf("Could not open file \"%s\".\n",filename);
            // printf("Could not open file: ", filename);
        } else {
            printf("Will sum numbers from file \"%s\".\n",filename);
            // printf("Summing numbers from file: ", filename);
            double sum=0;
            while ((read = getline(&line, &len, fp)) != -1) {
                if (read > 0) sum += atof(line);
            }
            printf("Sum is %f.\n",sum);

            fclose(fp);
            if (line) free(line);
        }
    }

    return 0;
}

I have checked that the path is correct, and I have compiled the code using the command gcc mySum.c -o mySum and g++ mySum.c -o mySum2, both works for compiling and creating an executable program, I am not sure why.

When I try to run with the comman gcc mySum or gcc mySum -o mySumOutput, I get the following error

/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: In function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

And when I try to run g++ mySum or g++ mySum -o mySumOutput, I get this error:

mySum: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.fini+0x0): first defined here
mySum: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.data+0x0): first defined here
mySum:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o:(.rodata+0x0): first defined here
mySum:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
mySum: In function `_dl_relocate_static_pie':
(.text+0x30): multiple definition of `_dl_relocate_static_pie'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text[.text.group]+0x0): first defined here
mySum: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text+0x0): first defined here
mySum: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
mySum:(.data+0x8): first defined here
/usr/bin/ld: error in mySum(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
[student1@mpi1 cprog]$ g++ mySum -o mySumOutput
mySum: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.fini+0x0): first defined here
mySum: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.data+0x0): first defined here
mySum:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o:(.rodata+0x0): first defined here
mySum:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
mySum: In function `_dl_relocate_static_pie':
(.text+0x30): multiple definition of `_dl_relocate_static_pie'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text[.text.group]+0x0): first defined here
mySum: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text+0x0): first defined here
mySum: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
mySum:(.data+0x8): first defined here
/usr/bin/ld: error in mySum(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

I have been trying to run the program but I am at a loss as to how manage it. All help is appreciated!

>Solution :

When you compile and link the result is ready to run executable file.

You just need to run it.

./mySum

or

./mySum2 mySumOutput
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading