Thread: Undefined reference to main - collect2: ld returned 1 exit status

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Undefined reference to main - collect2: ld returned 1 exit status

    I'm doing an assignment at school in C, but the program won't run.

    I get the error: "/tmp/ccW7r5po.o:functions3.c.text+0x1b9): undefined reference to `myLog2'collect2: error: ld returned 1 exit status"

    Help anyone?

    Code:
    #include <stdio.h>
    
    void myTriangles (int numlines);
    int myPrimeFactor (int number, int primeFactor);
    void myNumbers (int startnum, int endnum);
    int myLog2(unsigned int n);
    
    
    
    
    int main ()
    {
        int numlines, number, primeFactor, startnum, endnum, result, operasjon, f;
        printf("Choose function.\n");
        printf("1. myTriangles.\n");
        printf("2. myPrimeFactor. \n");
        printf("3. myNumbers. \n");
        printf("4. myLog2. \n");
        printf("5. reverseString. \n");
        scanf ("%d",&operasjon);
        switch (operasjon)
        {
            
            case 1:
            {
            printf("Enter number of rows!\n");
            scanf("%d",&numlines);
            myTriangles(numlines);
            }
            break;
            case 2:
            {
            printf("Enter a value.    ");
            scanf("%d",&number);
            printf("Enter a prime factor. \n");
            scanf("%d",&primeFactor);
            f =    myPrimeFactor (number, primeFactor);
            printf("%d", f);
            }
            break;
            case 3:
            {
            printf("Enter a start number.\n");
            scanf("%d", &startnum);
            printf("Enter an ending number.\n");
            scanf("%d", &endnum);
            myNumbers(startnum, endnum);
            }
            break;
            case 4:
            {
            unsigned int n;
     
                printf("Pick a positive number: ");
                scanf("%d", &n);
     
                int result = myLog2(n);
                printf("The position of the most significant bit is %d\n\n", result);
            }
        }
    }
    
    
    // OPPGAVE 1
    void myTriangles (int numlines)
    {
        int i, j;
        
        for (i=1;i<=numlines;i++) 
        {
        for (j=1;j<=i;j++)
        {
            printf("*");
        }
            printf("\n");
        }
    }
    // OPPGAVE 2    
    int myPrimeFactor (int number, int primeFactor)
    {
        int result = number % primeFactor;
    
    
        if(result == 0){
        return 1;
        }
        else
        {
        return 0;
        }
    }
    // OPPGAVE 3
    void myNumbers (int startnum, int endnum)
    {
        
            while (startnum <= endnum)
        {
            int result = myPrimeFactor(startnum, 5);
            
            if (startnum % 2 == 0 && result == 1) {
                printf("%d is even and 5 is a prime factor\n",startnum);
            }
            else if (startnum % 2 == 1 && result == 1) {
                printf("%d is odd and 5 is a prime factor\n",startnum);
            }    
            else if (startnum % 2 == 1 && result == 0) {
                printf("%d is odd and 5 is not a prime factor\n",startnum);
            }
            else if (startnum % 2 == 0 && result == 0) {
                printf("%d is even and 5 is not a prime factor\n",startnum);
            }
        startnum ++;
        }
        
    // OPPGAVE 4
    int myLog2(unsigned int n)
    {
        int result = 0;
       
        while (n > 1)
        {
            n = n >> 1;
            result++;
        }
        return result;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Check your spelling, remember that C is case sensitive.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    Quote Originally Posted by jimblumberg View Post
    Check your spelling, remember that C is case sensitive.

    Jim
    Thanks for the reply.
    I have checked my spelling, but I can't seem to find any problems at all when it comes to spelling..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When I compiled your code, my compiler gave three warnings and an error message:
    Code:
    test.c: In function ‘main’:
    test.c:55:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
                 scanf("%d", &n);
                       ^
    test.c:13:58: warning: unused variable ‘result’ [-Wunused-variable]
         int numlines, number, primeFactor, startnum, endnum, result, operasjon, f;
                                                              ^
    test.c: In function ‘myNumbers’:
    test.c:116:1: warning: ISO C forbids nested functions [-Wpedantic]
     int myLog2(unsigned int n)
     ^
    test.c:126:1: error: expected declaration or statement at end of input
     }
     ^
    The error is because you forgot the closing brace for your myNumbers function. Once I added that in, your program compiled with two warnings:
    Code:
    test.c: In function ‘main’:
    test.c:55:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
                 scanf("%d", &n);
                       ^
    test.c:13:58: warning: unused variable ‘result’ [-Wunused-variable]
         int numlines, number, primeFactor, startnum, endnum, result, operasjon, f;
    But I did not encounter the link error that you observed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    The problem is not with spelling.

    First of all, turn on and turn up your warning level to the highest level.

    Code:
    mylog.c:126:1: error: expected declaration or statement at end of input
     }
     ^
    One of the other problems that could have occurred is mismatched curly braces, square brackets, and parentheses. Start on the line in the error message I have shown you, and check each function, counting delimiters to see if this might be your problem. HINT! HINT!

    Use a programmers editor or IDE that will highlight matching braces, etc..., or look in the documentation how to turn it on. This will save you a lot of time and frustration.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The missing brace at the end of the function (as laserlight stated)
    may have produced the linker warning. It may come down to the compiler
    used and it's config settings etc.

    I ran the code (as it stands shown) using the LCC compiler and I also got
    the linker error, and the two warnings. Strange.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. collect2.exe: error: ld returned 1 exit status
    By umutefiloglu in forum C Programming
    Replies: 4
    Last Post: 05-11-2014, 09:37 AM
  2. collect2: ld returned 1 exit status
    By Bryce Miller in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2012, 10:28 PM
  3. collect2: ld returned 1 exit status error??
    By blindchicken11 in forum C Programming
    Replies: 11
    Last Post: 11-07-2011, 08:38 PM
  4. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  5. ERROR collect2: ld returned 1 exit status
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 12-04-2007, 12:20 PM

Tags for this Thread