cfunctioncompiler-errorssyntax-errormodular-design

Error with multiple definitions of functions


im learning C in college and have an assignment to for my software development class, Its a application to ask the user which shape they want to choose, ask them for the parameters of the shape and then it outputs the area and volume etc. After making my make file, i get the same error for every function as seen below

gcc -Wall -ggdb -o assignment2 assignment2.c assignment22d.c assignment23d.c
/tmp/ccb5nvtg.o: In function `squarearea':
/home/stephengannon79/assignment22d.c:16: multiple definition of `squarearea'
/tmp/ccBcrWoo.o:/home/stephengannon79/assignment22d.c:16: first defined here
/tmp/ccb5nvtg.o: In function `squareperi':
/home/stephengannon79/assignment22d.c:23: multiple definition of `squareperi'
/tmp/ccBcrWoo.o:/home/stephengannon79/assignment22d.c:23: first defined here
/tmp/ccb5nvtg.o: In function `rectanglearea':
/home/stephengannon79/assignment22d.c:29: multiple definition of `rectanglearea'
/tmp/ccBcrWoo.o:/home/stephengannon79/assignment22d.c:29: first defined here
/tmp/ccb5nvtg.o: In function `rectangleperi':
/home/stephengannon79/assignment22d.c:36: multiple definition of `rectangleperi'

I have a main file, 2 header files for declaring the function and 2 c files for the functions themselves and a make file. Can anyone see my error and point me in the right direction? thanks, code is all below

// assignment2.c
//author: Stephen Gannon
//date: 09/11/19
//id: 18240313
/* Purpose: The purpose of this assignment is to use modular programing
   to write an application that calculates parameters of a shape such 
   as area, volume and perimeter depending on user inputed values for sizing. 
   The application contains a main file, two header files to calculate
   for each dimension and a make file for compilation.
*/

#include <stdio.h>
#include <stdlib.h>
#include "assignment22d.h"
#include "assignment23d.h"
#include "assignment22d.c"
#include "assignment23d.c"


int main()
{
  char option; //expression to choose between the 2d and 3d shapes

  double sqside; //squares side
  double sqarea; //square area
  double sqperi; //sqaure perimeter

  double reclength; //rectangles lentgth
  double recwidth; //rectangles width
  double recarea; //rectangle area
  double recperi; //rectangle perimeter

  double circrad; //circles radius
  double circarea; //circles area
  double circcirc; //circles circumference

  double trib; //triangles base length
  double trih; //triangles perpendicular height
  double tria; //traingles side a
  double tric; //triangles side c
  double triarea; //triangle area
  double triperi; //triangle perimeter

  double parab; //parallelograms base
  double parah; //parallelograms perpendicular height
  double paraa; // parallelograms side a
  double paraarea; //parallelogram area
  double paraperi; //parallelogram perimeter


  double cubeside; //cubes side
  double cubearea1; //cube area
  double cubevolume; //cube volume

  double recprisl; //rectangular prism length
  double recprish; //rectangular prism height
  double recprisw; //rectangular prism width
  double recprisarea; //rectangular prism area
  double recprisvolume; //rectangular prism volume

  double sphrad; //spheres radius
  double spharea; //spheres area
  double sphvolume; //spheres volume

  double rccr; //right circular cylinder radius
  double rcch; //right circular cylinder height
  double rccarea; //right circular cylinder area
  double rccvol; //right circular cylinder volume


  printf("Please choose an object:\na-sqaure\nb-rectangle\nc-circle\nd-triangle\ne-parallelogram\nf-cube\ng-rectangular prism\nh-sphere\ni-right circular cylinder\n-----------------------------\n");
  scanf("%c", &option); //scans in the value of option

  switch(option) //switch cases, the shapes are lettered a to i
    {
    case 'a': //square
      printf("Please enter the length of the side:\n");
      scanf("%lf", &sqside);
      sqarea = squarearea(sqside);
      printf("The squares area is: %lf\n", sqarea);
      sqperi = squareperi(sqside);
      printf("The squares perimeter is: %lf\n", sqperi);
      break;

    case 'b': //rectangle
      printf("Please enter the length and width:\n");
      scanf("%lf %lf", &reclength, &recwidth);
      recarea = rectanglearea(reclength, recwidth);
      printf("The rectangles area is: %lf\n", recarea);
      recperi = rectangleperi(reclength, recwidth);
      printf("The rectangles perimeter is: %lf\n", recperi);
      break;

    case 'c': //circle
      printf("Please enter the radius:\n");
      scanf("%lf", &circrad);
      circarea = circlearea(circrad);
      printf("The circles area is: %lf\n", circarea);
      circcirc = circlecirc(circrad);
      printf("The circles circumference is: %lf\n", circcirc);
      break;

    case 'd': //triangle
      printf("Please enter the base length, height and other sides a and c:\n");
      scanf("%lf %lf %lf %lf", &trib, &trih, &tria, &tric);
      triarea = trianglearea(trib, trih, tria, tric);
      printf("The triangles area is: %lf\n", triarea);
      triperi = triangleperi(trib, trih, tria, tric);
      printf("The triangles perimeter is: %lf\n", triperi);
      break;

    case 'e': //parallelogram
      printf("Please enter the base, height and side a:\n");
      scanf("%lf %lf %lf", &parab, &parah, &paraa);
      paraarea = parallelogramarea(parab, parah, paraa);
      printf("The parallelograms area is: %lf\n", paraarea);
      paraperi = parallelogramperi(parab, parah, paraa);
      printf("The parallelograms perimeter is: %lf\n", paraperi);
      break;

    case 'f': //cube
      printf("Please enter the length of the side:\n");
          scanf("%lf", &cubeside);
      cubearea1 = cubearea(cubeside);
      printf("The cubes area is: %lf\n", cubearea1);
      cubevolume = cubevol(cubeside);
      printf("The cubes volume is: %lf\n", cubevolume);
      break;

    case 'g': //rectangular prism
      printf("Please enter the length, height and width:\n");
          scanf("%lf %lf %lf", &recprisl, &recprish, &recprisw);
      recprisarea = rectangularprismarea(recprisl, recprish, recprisw);
      printf("The rectangular prisms area is: %lf\n", recprisarea);
      recprisvolume = rectangularprismvol(recprisl, recprish, recprisw);
      printf("The rectangular prisms volume is: %lf\n", recprisvolume);
      break;

    case 'h': //sphere
      printf("Please enter the radius:\n");
      scanf("%lf", &sphrad);
      spharea = spherearea(sphrad);
      printf("The spheres area is: %lf\n", spharea);
      sphvolume = spherevol(sphrad);
      printf("The spheres volume is: %lf\n", sphvolume);
      break;

    case 'i': //right circular cyclinder
      printf("Please enter the radius and height:\n");
      scanf("%lf %lf", &rccr, &rcch);
      rccarea = rightcylarea(rccr, rcch);
      printf("The right circular cylinders area is: %lf\n", rccarea);
      rccvol = rightcylvol(rccr, rcch);
      printf("The right circular cylinders volume is: %lf\n", rccvol);
      break;

    case '\n':
      break;

    default: //handles if any value other than a to i is inputted
      printf("Error! The value you entered is incorrect!\n");

    }


  return 0;
}
#ifndef HEADER_FILE2D
#define HEADER_FILE2D

double squarearea(double sqside); //declaring square area function
double squareperi(double sqside); //declaring square perimeter function

double rectanglearea(double reclength, double recwidth); //declaring rectangle area function
double rectangleperi(double reclength, double recwidth); //declaring rectangle perimeter function

double circlearea(double circrad); //declaring circle area function
double circlecirc(double circrad); //declaring circle circumference function

double trianglearea(double trib, double trih, double tria, double tric); //declaring triangle area function
double triangleperi(double trib, double trih, double tria, double tric); //declaring triangle perimeter function

double parallelogramarea(double parab, double parah, double paraa); //declaring parellelogram area function
double parallelogramperi(double parab, double parah, double paraa); //declaring parellelogram perimeter function

#endif
//assignment22d.c
//author: Stephen Gannon
//date: 10/11/19
//id: 18240313
/* Purpose: A file that contains all the functions necessary to 
   calculate the peramteres for the 2d shapes, as well as a defined Pi.
*/

#ifndef assignment22d
#define assignment22d
#define Pi 3.141593
#define circarea(circrad) (Pi)*(circrad*circrad)
#define circcirc(circrad) 2*(Pi)*circrad

double squarearea(double sqside)
{
  double sarea; //squares area
  sarea = sqside*sqside;
  return sarea;
}

double squareperi(double sqside)
{
  double speri; //square perimeter
  speri = 4*sqside;
  return speri;
}
double rectanglearea(double reclength, double recwidth)
{
  double rarea; //rectangles area
  rarea = reclength*recwidth;
  return rarea;
}

double rectangleperi(double reclength, double recwidth)
{
  double rperi; //rectangles perimeter
  rperi = (2*reclength)+(2*recwidth);
  return rperi;
}

double circlearea(double circrad)
{
  double carea; //circles area
  carea = circarea(circrad);
  return carea;
}

double circlecirc(double circrad)
{
  double cperi; //circles perimeter
  cperi = circcirc(circrad);
  return cperi;
}

double trianglearea(double trib, double trih, double tria, double tric)
{
  double tarea=0; //triangles area
  tarea = (trib*trih)/2;
  return tarea;
}

double triangleperi(double trib, double trih, double tria, double tric)
{
  double tperi; //triangles perimeter
  tperi = trib+tria+tric;
  return tperi;
}

double parallelogramarea(double parab, double parah, double paraa)
{
  double parea=0; //parallelograms area
  parea = parab*parah;
  return parea;
}

double parallelogramperi(double parab, double parah, double paraa)
{
  double pperi; //parallelograms perimeter
  pperi = (2*paraa)+(2*parab);
  return pperi;
}


#endif
//assignment23d.h
//author: Stephen Gannon
//date: 15/11/19
//purpose: To declare the functions for all 3d shapes

#ifndef HEADER_FILE3D
#define HEADER_FILE3D

double cubearea(double cubeside); //declaring cube area function
double cubevol(double cubeside); //declaring cube volume function

double rectangularprismarea(double recprisl, double recprish, double recprisw); //declaring rectangular prisms area function
double rectangularprismvol(double recprisl, double recprish, double recprisw); //declaring rectangle prisms volume function

double spherearea(double sphrad); //declaring spheres area function
double spherevolume(double sphrad); //declaring circle circumference function

double rightcylarea(double rccr, double rcch); //declaring cylinder area function
double rightcylvol(double rccr, double rcch); //declaring cylinder volume function

#endif
//assignment23d.c
//author: Stephen Gannon
//date: 11/11/19
//id: 18240313
/* Purpose: A header file that contains all the functions necessary to 
   calculate the peramteres for the 3d shapes, as well as a defined Pi.
*/

#ifndef assignment23dHead
#define assignment23dHead
#define Pi 3.141593
#define csarea(cubeside) 6*(cubeside*cubeside)
#define cvolume(cubeside) (cubeside*cubeside*cubeside)


double cubearea(double cubeside)
{
  double cuarea; //cubes area
  cuarea = csarea(cubeside);
  return cuarea;
}

double cubevol(double cubeside)
{
  double cuvol; //cubes volume
  cuvol = cvolume(cubeside);
  return cuvol;
}

double rectangularprismarea(double recprisl, double recprish, double recprisw)
{
  double recprisarea; //rectangular prisms surface area
  recprisarea = (2*recprisl*recprisw)+(2*recprisl*recprish)+(2*recprisw*recprish);
  return recprisarea;
}

double rectangularprismvol(double recprisl, double recprish, double recprisw)
{
  double recprisvol; //rectangular prisms volume
  recprisvol = recprisl*recprisw*recprish;
  return recprisvol;
}

double spherearea(double sphrad)
{
  double spherearea; //spheres surface area
  spherearea = 4*Pi*(sphrad*sphrad);
  return spherearea;
}

double spherevol(double sphrad)
{
  double spherevol; //spheres volume
  spherevol = (4*Pi*(sphrad*sphrad*sphrad))/3;
  return spherevol;
}

double rightcylarea(double rccr, double rcch)
{
  double cylinderarea; //right circular cylinders surface area
  cylinderarea = (2*Pi*rccr*rcch)+(2*Pi*(rccr*rccr));
  return cylinderarea;
}

double rightcylvol(double rccr, double rcch)
{
  double cylindervol; //right circular cylinders volume
  cylindervol = Pi*rcch*(rccr*rccr);
  return cylindervol;
}

#endif
assignment2: assignment2.c assignment22d.c assignment23d.c assignment22d.h assignment23d.h
    gcc -Wall -ggdb -o assignment2 assignment2.c assignment22d.c assignment23d.c


Solution

  • You're including .c files, alongside the header files which results in multiple definitions for the functions. Replace this

    #include "assignment22d.h"
    #include "assignment23d.h"
    #include "assignment22d.c"
    #include "assignment23d.c"
    

    With this

    #include "assignment22d.h"
    #include "assignment23d.h"