cdefinitionlnk2005

LNK1169 and LNK2005 errors in C


I'm getting the following errors when trying to compile my code:

LNK1169: one or more multiply defined symbols found LNK2005: [function name] already defined in main.obj ^^^ Error LNK2005 for every function

Here's my current code: main.c

#define _CRT_SECURE_NO_WARNINGS –
#pragma once
#include "pa-1.h"
#include "pa-1.c"
int main()
{
    //Declaring an array
    int list[3] = {0,0,0};
    //Declaring variables
    int n = 0;
    int x = 0;
    int r = 0;
    int y = 0;
    int z = 0; 
    int rotateNum = 0;
    int searchRecur = 0; 
    int collatzNum = 0;
    int average = 0;
    bool results;
    char brackets = "";
    char lookFor = "";
    char paragraph = ""; 
    char fString = "";
    char sString = "";
    char *e = "";
    char *pattern = ""; 
    char *text = "";
    char *s1 = "";
    char *s2 = "";
    char *s3 = "";

    //Function 1
    //Will see if input is an armstrong number
    printf("Enter a number and the program will check to see if it's an armstrong number: ");
    scanf("%d", n);

    results = f_armstrong(n);

    if (results == true)
    {
        printf(" is an armstrong number");
    }
    else
    {
        printf("%d is not an armstrong number", n);
    }
/*
    //Function 2
    //Will see if brackets are balanced
    printf("Enter brackets and the program will check to see if they are in order: ");
    scanf("%s", brackets);

    *e = brackets;

    results = f_brackets(*e);

    if (results == true)
    {
        printf("Your brackets are in order");
    }
    else
    {
        printf("Your brackets are not in order");
    }

    //Function 3
    //Will see if input is a perfect number
    printf("Enter a number and the program will check to see if it's a perfect number: ");
    scanf("%d", x);

    results = f_perfect(x);

    if (results == true)
    {
        printf("%d is a perfect number", x);
    }
    else
    {
        printf("%d is not a perfect number", x);
    }

    //Funtction 4
    //Will rotate input by a number that is also given
    printf("Enter a number that you want to rotate its digits: ");
    scanf("%d", n);
    printf("Enter a number that you want to rotate by: ");
    scanf("%d", r);

    rotateNum = f_rotate(n, r);

    printf("The new number is: %d", rotateNum);

    //Funtction 5
    //Will search for a pattern in a text
    printf("Enter a text you want to search through: ");
    scanf("%s", paragraph);
    printf("Enter a pattern you want to search for: ");
    scanf("%s", lookFor);

    *text = paragraph;
    *pattern = lookFor;

    searchRecur = f_str_search(*pattern, *text);

    printf("%s can be found in your text %D times", *pattern, searchRecur);

    //Funtction 6
    //Will perform the Collatz conjucture on input
    printf("Enter a number for the program to perform the Collatz conjucture to: ");
    scanf("%d", n);

    collatzNum = f_collatz_count(n);

    printf("It took %d iterations", collatzNum);

    //Funtction 7
    //This function will take a number and create an array with the amount and perform other operation
    printf("Enter a number to determine how many random numbers are generated: ");
    scanf("%d", n);

    average = f_array(n);

    printf("The numbers were randomly generated between 1-99 and multiplied by 2. The following number is the average of the (up to 5) numbers divisible by 3: %d", average);

    //Funtction 8
    //This function will take two strings and put them into one, in shorter/larger/shorter order
    printf("Enter the first text you want to forge with: ");
    scanf("%s", fString);
    printf("Enter the second text you want to forge with: ");
    scanf("%s", sString);

    *s1 = fString;
    *s2 = sString;

    f_strings(*s1, *s2, *s3);

    printf("Your new string is: %s", *s3);

    //Function 9
    //This will take three numbers and sort them in order
    printf("The program will now take three numbers and sort them in assending order. Enter the first number: ");
    scanf("%d", x);
    printf("Enter the second number: ");
    scanf("%d", y);
    printf("Enter the third number: ");
    scanf("%d", z);

    f_sort(x, y, z, *list);

    printf("The numbers in order are: %d %d %d",list[0], list[1], list[2]);

    //Funtction 10
    //This will look for a set of numbers that equate to the input, when cubed
    printf("Enter a number and the program will see if a set of numbers cubed will equate to it: ");
    scanf("%d", n);

    int * twoValues = f_cubed_sum(n);
    if (twoValues[0] != NULL)
    {
        printf("The two values are: %d and %d", twoValues[0], twoValues[1]);
    }
    else
    {
        printf("There is no pair of integers that satasfy the formula");
    }

    free(twoValues);
    */


    return 0;
}

pa-1.c:

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "pa-1.h"


/*
This function takes an integer n as a parameter.
The purpose of this function is to determine if n is an
"Armstrong Number".
*/
bool f_armstrong(int n)
{
    int numLength;              //Holds how many digits are in n
    int finalSum = 0;           //Holds the final test value to compare with n
    int digits = 0;             //Holds the value of parameter n
    int remainder;              //Holds the value of the remainder 
    int userNum = n;            //Holds the value of n

                                // Count number of digits in n
    while (userNum != 0)
    {
        digits++;

        //Takes away a digit from n
        userNum /= 10;
    }

    //Re-assign the variables
    numLength = digits;
    userNum = n;

    //Test loop to determine if it's an Armstrong number
    while (userNum != 0)
    {
        //Find digit in the number
        remainder = userNum % 10;

        //Exponential increase the digit and add it to the sum
        finalSum += pow(remainder, digits);
        userNum /= 10;
    }

    //If the test was true
    if (finalSum == n)
    {
        return true;
    }

    //If the test fails
    else
    {
        return false;
    }
}



/*
THe user enters a string phrase into the function.
The function will then go through the string and count the
number of open and closed brackets. If the sizes match it
returns true, if not it returns false.
*/
bool f_brackets(char *e)
{
    //Declaring variables
    int brackLen;
    int counter = 0;
    int openCount = 0;
    int closeCount = 0;
    char curChar, brackets;
    bool results = true;

    brackets = *e;

    //Getting the length of the string
    brackLen = strlen(e);

    while (counter < brackLen && results == true)
    {
        curChar = getchar(brackets);

        if (curChar == '[')
        {
            openCount = openCount + 1;
        }
        else
        {
            closeCount = closeCount + 1;
        }

        if (closeCount > openCount)
        {
            results = false;
        }

        counter = counter + 1;
    }

    return results;
}

/*
The user will enter a number theu want to test.
The function will then add up the sum of the divisors.
If the sum and number match it returns true, if not it
returns true.
*/
bool f_perfect(int x)
{
    int userNum = x;                    //Variable to hold the parameter value
    int sum = 1;                        //Holds the final sum of perfect numbers

                                        //Divided by the user's number
                                        //Starts at 2 since all numbers are divisable by 1
    int divisor = 2;

    //While the user's number is greater then the divisor
    while (divisor < userNum)
    {
        //Int to hold the value
        int test = userNum % divisor;

        //If test is 0, then userNum is divisable
        if (test == 0)
        {
            //Add the divisable to the overall sum
            sum += divisor;
        }

        //Incrememnt 
        divisor++;
    }

    //If the sum of divisors is equal to the user's number
    if (userNum == sum)
    {
        return true;
    }

    //If the sum of divisors is not equal to the user's number
    else
    {
        return false;
    }
}


/*
The purpose of this function is for the user to enter a number,
and then they enter another number which the function uses to rotate
the values of their first number.
*/
int f_rotate(int n, int r)
{
    int number = n;                 //Holds the value of users number
    const int mod = 10;             //Constant value used for modulus command
    int rotater = r;                //Value to hold how many rotations user wants
    int multiplier = 10;            //Variable that will be used for multiplications
    int digits = 0;                 //Variable to hold how many values are in number
    int finalNum = 0;               //Variable that holds the final rotated number

                                    //For loop to determine how many numbers are in users entered value
    for (int i = 0; number > 0; i++)
    {
        //Remove a digit from the value and increment
        number /= 10;
        digits++;
    }

    //Loop that will increment multiplier by 10 until it has the same 0's as user number
    for (int i = 1; i < digits - rotater; i++)
    {
        multiplier *= 10;
    }

    //Reset number to original value
    number = n;

    //Loop until it's been rotated the correct amount of times
    for (int i = 0; i < rotater; i++)
    {
        //Temp value that will hold the value of a spot in the user's number
        int temp;

        //Mod 10 to get the digit by itself
        temp = number % 10;

        //Subtract the value from the number and get rid of a 0
        number -= temp;
        number /= 10;

        //If the digit did not equal 0
        if (temp != 0)
        {
            //Multiply the the value by the multiplier and add it to final num
            finalNum += temp * multiplier;
            multiplier *= 10;
        }
    }

    //Add the numbers not being rotated to the final number
    finalNum += number;

    //Return the rotated number
    return finalNum;
}


/*
The user enters a pattern that they would like to search for.
The user also enters a text that they want to check for patterns.
The function will return the amount of times it finds the pattern
in the test.
*/
int f_str_search(char *pattern, char *text)
{
    int patSize = strlen(pattern);          //Varaible to hold size of pattern
    int textSize = strlen(text);            //Varaible to hold size of text
    int occurence = 0;                      //Variable to hold amount of times the pattern is matched
    int patElement = 0;                     //Array element for pattern
    int textElem = 0;                       //Array element for text
    int match = 0;                          //Variable to hold how many matched elements are found

                                            //While there's still chars left to test in the text string
    while (textElem < textSize)
    {
        //If the values in the element match
        if (*(pattern + patElement) == *(text + textElem))
        {
            //Increment match and the pattern element
            match++;
            patElement++;
        }

        //If the values in the element do not match
        else
        {
            //Reset match and go back to the first char in pattern
            match = 0;
            patElement = 0;;
        }

        //If matches equal size of pattern, pattern is found
        if (match == patSize)
        {
            //Reset match and pattern element to continue checking
            match = 0;
            patElement = 0;

            //Increment the occurence
            occurence++;

            //Decrement text element to start checking again in last element a match was found
            textElem--;
        }

        //
        textElem++;
    }

    return occurence;
}


/*
The user enters a natural number.
The program will use the "The Collatz Conjecture" and return
the amount of iterations it took to get the user's number to
equal 1.
*/
int f_collatz_count(int n)
{
    int userNum = n;                //Variable to hold the user's number
    int iteration = 0;              //Variable to hold amount of iterations

                                    //While userNum is not 1
    while (userNum != 1)
    {
        //testNum will hold the remainder of the number divided by 2
        int testNum = userNum % 2;

        //If the number was divisable by 2 (even)
        if (testNum == 0)
        {
            //Divide number by 2 and increment iteration
            userNum /= 2;
            iteration++;
        }

        //If the number was not divisable by 2 (odd)
        else
        {
            //Multiply the number by 3 and add 1.
            userNum = (userNum * 3) + 1;
            //Increment iterations
            iteration++;
        }
    }

    //Return total iterations
    return iteration;
}


/*
User enters a number.
The function will add however many random numbers the user entered.
It will then multiply all the random numbers by 2, and will return the
average of the first 5 numbers divisable by 3.
*/
int f_array(int n)
{

    //Creating variables
    int remainder, average;
    int counter = 0;
    int avgCount = 0;
    int total = 0;
    bool results = false;

    //Creating arrayAn
    int* A;
    A = malloc(n * sizeof(int));

    //Initializing random seed
    srand(time(NULL));

    //Populating array with random numbers
    while (counter < n)
    {
        A[counter] = rand() % 99 + 1;

        counter = counter + 1;
    }
    //Resetting counter
    counter = 0;

    //Multiplying array by 2
    while (counter < n)
    {
        A[counter] = A[counter] * 2;

        counter = counter + 1;
    }

    //Restting counter
    counter = 0;

    //Getting the average of first 5 numbers divisible by 3
    while (counter < n && results == false)
    {
        //Checking if divisible by 3
        remainder = A[counter] % 3;

        if (remainder == 0)
        {
            total = total + A[counter];
            avgCount = avgCount + 1;
        }

        if (avgCount = 5)
        {
            results = true;
        }

        counter = counter + 1;

    }

    average = total / avgCount;

    free(A);

    return average;

}



/*
The user enters two different strings.
The function will test to see which one is larger and then return a
combined string with the shorter string, the longer string, and the
shorter string again.
*/
void f_strings(char *s1, char *s2, char *s3)
{
    //Declaring variables
    int firstCount, secCount;


    //Comparing the string lengths
    firstCount = strlen(s1);
    secCount = strlen(s2);

    if (firstCount > secCount)
    {
        *s3 = *s1 << *s2 << *s1;
    }
    else
    {
        *s3 = *s2 << *s1 << *s2;
    }

    return;
}


/*
The user enters 3 numbers.
The function then sorts those numbers and lists them in ascending
order.
*/
void f_sort(int x, int y, int z, int *list)
{
    const int arraySize = 3;            //Constant for the size of array
    int element = 0;                    //Holds numerical value for array element
    int num1 = x;                       //Holds value of first entered number
    int num2 = y;                       //Holds value of second entered number
    int num3 = z;                       //Holds value of third entered number
    int temp;                           //Holds value of number being swapped

                                        //If the first number is larger then the second
    if (num1 > num2)
    {
        //Swap their values
        temp = num2;
        num2 = num1;
        num1 = temp;
    }

    //If the first number is larger then the third
    if (num1 > num3)
    {
        //Swap their values
        temp = num3;
        num3 = num1;
        num1 = temp;
    }

    //If the second number is larger then the third
    if (num2 > num3)
    {
        //Swap their values
        temp = num3;
        num3 = num2;
        num2 = temp;
    }

    //Allocate memory
    list = (int *)malloc(arraySize);

    //Add the values into the array in ascending order
    *(list + element) = num1;
    element++;
    *(list + element) = num2;
    element++;
    *(list + element) = num3;

    printf("%s \n", "Here is your numbers sorted in ascending order:");

    //Loops until all 3 values are printed
    for (int i = 0; i < arraySize; i++)
    {
        printf("%d \n", *(list + i));
    }
}


/*
The user enters a number.
The function will test if the cubed sum of 2 numbers is equal
to the number they entered.
*/
int *f_cubed_sum(int n)
{
    //Variable to hold the max value the program can go to
    const int maxValue = cbrt(n);

    const int exponant = 3;             //Constant exponant value
    int element = 0;                        //Holds value of array
    int test1 = 0;                          //Value of first number cubed
    int test2 = 0;                          //Value of second number cubed
    int *pair1;
    int *pair2;
    int *pairs;
    int totalPairs = 0;                   //Holds if pairs have been found

    pair1 = (int *)malloc(5 * maxValue);
    pair2 = (int *)malloc(5 * maxValue);

    //While the first test number is less then or equal to max value
    while (test1 <= maxValue)
    {
        //Reset second test number to 0
        test2 = 0;

        //While the second test number is less then or equal to max value
        while (test2 <= maxValue)
        {
            //Sum of the two test numbers
            int sum = 0;

            //Cube the test numbers and add them to sum
            sum += pow(test1, exponant);
            sum += pow(test2, exponant);

            //If sum is equal to users number
            if (sum == n)
            {
                //If one of the test numbers is 0
                if (test1 == 0)
                {
                    *(pair1 + element) = 0;
                    *(pair2 + element) = test2;
                    totalPairs += 2;
                }

                else if (test2 == 0)
                {
                    *(pair1 + element) = test1;
                    *(pair2 + element) = 0;
                    totalPairs += 2;
                }

                else
                {
                    *(pair1 + element) = test1;
                    *(pair2 + element) = test2;

                    element++;

                    totalPairs += 2;
                }
            }
            //Increment test number 2
            test2++;
        }
        //Increment test number 1
        test1++;
    }

    //If no pairs were found
    if (totalPairs == 0)
    {
        //Free memory and return NULL
        free(pair1);
        free(pair2);
        return NULL;
    }

    //If there were pairs found
    else
    {
        int pairElement = 0;
        int pairsElement = 0;
        pairs = (int *)malloc(100 * totalPairs);

        for (int i = 0; pairElement < sizeof(pair1); i++)
        {
            if (*(pair2 + pairElement) != 0 && *(pair1 + pairElement) != 0)
            {
                *(pairs + pairsElement) = *(pair1 + pairElement);
                pairsElement++;
                *(pairs + pairsElement) = *(pair2 + pairElement);
                pairsElement++;
                pairElement++;
            }

            else
            {
                pairElement++;
            }
        }

        //Return array of pairs
        return pairs;
    }


}

pa-1.h

#pragma once
// define a boolean type to use in the functions

#ifndef _BOOL_TYPE_
#define _BOOL_TYPE_
typedef enum { false, true } bool;
#endif

bool f_armstrong(int n);
bool f_brackets(char *e);
bool f_perfect(int x);
int f_rotate(int n, int r);
int f_str_search(char *pattern, char *text);
int f_collatz_count(int n);
int f_array(int n);
void f_strings(char *s1, char *s2, char *s3);
void f_sort(int x, int y, int z, int *list);
int *f_cubed_sum(int n);

I'm using Visual Studio.

Thank you!


Solution

  • You should not include the .c file. Remove this line

    #include "pa-1.c"
    

    And compile all .c files, for example:

    gcc main.c pa-1.c -o prog.exe
    

    Edit: Didn't noticed you are using Visual Studio, in that case just adding all the files in the same project and building it should suffice. So ignore the above gcc command.