Thread: Visual Studio -- Getting an "Unresolved External Symbol" in a Small C Program

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    Visual Studio -- Getting an "Unresolved External Symbol" in a Small C Program

    I'm getting a "unresolved external symbol" error in Visual Studio and unsure how to resolve. Can anyone give me a hint?

    Included screenshot too.

    Code:
    #include <stdio.h>
    
    #define SIZE sizeof(a) / sizeof(a[0])
    
    
    int largest_element(int a[], int length);
    float average_of_elements(int a[], int length);
    int positive_element_count(int a[], int length);
    
    
    int main(void)
    {
    	int i, a[10] = { 1, 3, 8, 20, 12, 27, 0, 32, 12, 10 };
    
    
    	printf("a[] contains: ");
    	for (i = 0; i < SIZE; i++)
    		printf("%d ", a[i]);
    	printf("\n");
    
    
    	printf("Largest element in a[]: %d\n", largest_element(a, SIZE));
    	printf("Average of elements in a[]: %f\n", average_of_elements(a, SIZE));
    	printf("Number of positive elements in a[]: %d\n", positive_element_count(a, SIZE));
    
    
    	return 0;
    }
    
    
    
    
    /* Returns the largest element in a[] */
    int largest_element(int a[], int length)
    {
    	int i, largest = a[0];
    
    
    	for (i = 1; i < length; i++) {
    		if (a[i] > largest)
    			largest = a[i];
    	}
    
    
    	return largest;
    }
    Visual Studio -- Getting an &quot;Unresolved External Symbol&quot; in a Small C Program-error-jpg

  2. #2
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    It also happens with this code that was written as an alternative solution by another author:

    Code:
    #include <stdio.h>
    
    #define LEN 10
    
    
    int max(int a[], int n);
    float avg(int a[], int n);
    int n_pos(int a[], int n);
    
    
    int main(void)
    {
    	int a[LEN] = { -3, 7, 14, 8, -12, 9, 0, -1, -3, 4 };
    
    
    	printf("Array:");
    	for (int i = 0; i < LEN; i++) {
    		printf(" %d", a[i]);
    	}
    	printf("\n");
    
    
    	printf("Max: %d\n", max(a, LEN));
    	printf("Avg: %.2f\n", avg(a, LEN));
    	printf("Num. Positive: %d\n", n_pos(a, LEN));
    
    
    	return 0;
    }
    
    
    /*
    * Given an int array of length n, return the largest element.
    */
    int max(int a[], int n)
    {
    	int maximum = a[0];
    
    
    	for (int i = 0; i < n; i++) {
    		if (a[i] > maximum) {
    			maximum = a[i];
    		}
    	}
    
    
    	return maximum;
    }
    Visual Studio -- Getting an &quot;Unresolved External Symbol&quot; in a Small C Program-error-2-jpg

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It's incredible (i.e., unbelievable) that the other author made exactly the same bizarre mistake.
    You actually have to write those functions.
    Where have you written (defined) the average_of_elements and positive_element_count functions?

    Your SIZE macro should be:
    Code:
    #define ARRSIZE(a) (sizeof(a) / sizeof((a)[0]))
    Note the better name, the parameter, and the extra parentheses.
    You use it like:
    Code:
    ARRSIZE(a)

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    That's not a compiler error, it's a linker error. Compiling a program to an executable is a three stage process.

    Stage 1:- The compiler runs the preprocessor over the file. That's all the #defines and #includes etc. and creates one file called a translation unit.

    Stage 2:- The compiler compiles the code into little pieces of as yet not joined up machine code.

    On your file the compiler succeeds. The programs are correct. Except.... let's look at stage 3.

    Stage 3:- The linker takes all those little bits of machine code and ties them all together into one executable file.

    Example 2 is slightly easier to look at but let's see what happened.

    Stage 1 did it's job.

    At stage 2 the compiler has compiled the max function into one piece of code, and now let's look at main.

    We are fine until we get down to where the avg symbol is on line 24. The compiler hasn't seen the avg function yet but thanks to the prototype it knows what it returns and what parameters it takes so it can go ahead and compile the main function successfully. At this stage the compiler knows what it needs to know and leaves function address resolution until the link stage. Basically it leaves a note to the linker saying Oi, work to do here.

    At stage 3 the linker resolves the address for the avg function. But hang on, it's not there. It has never been defined.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    I figured it out. In the first example, I had two remove these two statements from the main loop, because the functions were not defined below. They did have prototypes, but the compiler didn't know what to do with them

    Code:
       printf("Average of elements in a[]: %f\n", average_of_elements(a, SIZE));
        printf("Number of positive elements in a[]: %d\n", positive_element_count(a, SIZE));


    This gave me more or less the result I was looking for. The exercise was just to create that array function to find the largest element in the array. I was having trouble getting it to work with the main loop

    Code:
    #include <stdio.h>
    
    #define SIZE sizeof(a) / sizeof(a[0])
    
    int largest_element(int a[], int length);
    
    
    int main(void)
    {
    int i;
    int a[10] = { 1, 3, 8, 20,12, 27, 0, 32, 12, 10 };
    
    
    
    printf("a[] contains:");
    for (i = 0; i < SIZE;i++)
    printf("%d ",a[i]);
    printf("\n");
    
    
    printf("Largestelement in a[]: %d\n", largest_element(a, SIZE));
    
    
    return 0;
    }
    
    
    /* Returns the largest element in a[] */
    
    int largest_element(int a[], int length)
    {
    int i, largest = a[0];
    
    
    for (i = 1; i < length;i++) {
    if (a[i] > largest)
    largest = a[i];
    }
    
    return largest;
    }
    Last edited by potomac; 12-19-2016 at 12:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "error LNK2001: unresolved external symbol"problem
    By gjgcstick in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2010, 09:06 AM
  2. Replies: 6
    Last Post: 08-26-2008, 12:38 PM
  3. "Unresolved External Symbol" Error
    By mikeman118 in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2007, 10:05 AM
  4. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  5. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM

Tags for this Thread