Chris
Chris

Reputation: 8442

Trouble using makefile with multiple files in C

Starting to get my head around makefiles for my C programs, but having some trouble when trying to include multiple files. Ignoring the fact that the program below is incomplete (in terms of functionality but not compilation), I'm trying to get this program compiling and running using a make file.

Here is my make file:

main: main.o IntList.o
    gcc -o main main.o IntList.o

main.o: main.c
    gcc -c -ansi -pedantic -Wall main.c

IntList.o: IntList.c IntList.h
    gcc -c -ansi -pedantic -Wall Intlist.c

And here is the error I am receiving:

gcc -c -ansi -pedantic -Wall Intlist.c
gcc -o main main.o IntList.o
ld: duplicate symbol _getNewInt in IntList.o and main.o
collect2: ld returned 1 exit status
make: *** [main] Error 1

The code for the program is below. I'm not sure whether it's the make file or my includes in the program files that are causing problems (or both!)

Any help would be great. Cheers.

Edit: Any tips to steer me in the right direction in terms of modularization would be much appreciated as I'm not sure if I am doing this the best way.

IntList.h

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

/* Constants */
#define MAX_INTS 10

/* Signed ints can have a maximum of 10 digits. We make the length 11 to 
 * allow for the sign in negative numbers */
#define MAX_INPUT_LENGTH 11
#define EXTRA_SPACES 2

/* Typedefs / Structs */
typedef struct {
   int list[MAX_INTS];
   int noInts;
} IntList;

/* Proto Types */
int insertIntToList(int *list);
void shiftList(int offset);
void displayList();

IntList.c

#include "IntList.h"

int getNewInt(int *list)
{
   int valid = 0, inputInt;
   char inputString[MAX_INPUT_LENGTH + EXTRA_SPACES];

   while(!valid)
   {
      printf("Input an int: ");

      valid = 1;

      if((fgets(inputString, MAX_INPUT_LENGTH + EXTRA_SPACES, stdin)) != NULL)
      {
         sscanf(inputString, "%d", &inputInt);
         /* Check first that the input string is not too long */
         if(inputString[strlen(inputString) - 1] != '\n')
         {
            printf("\nError: Too many characters entered \n");
            valid = 0;
         }

         printf("\nThe Int: %d", inputInt);
         printf("\n");
      }
   }
}

void shiftList(int offset)
{
}

void displayList()
{
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "IntList.c"

int main(void)
{
   int intList[10];

   getNewInt(intList);

   return EXIT_SUCCESS;
}

Upvotes: 0

Views: 411

Answers (6)

Rafał
Rafał

Reputation: 1287

As others have mentioned, you include .h files, not .c files Also when you compile, you only compile .c files so you should remove any references to .h files in your Makefile

Upvotes: 0

MaxVT
MaxVT

Reputation: 13244

main.c should include "IntList.h", not "IntList.c".

If you include IntList.c, the functions in IntList.c will be implemented both in IntList.o and in main.o, which would produce the "duplicate symbol" error you're seeing.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882496

You should not have:

#include "IntList.c"

in your main program, it should be:

#include "IntList.h"

By including the C file, you create a getNewInt in both your main and IntList object files, which is why you're getting the duplicate definition error when you try to link them together.

Upvotes: 0

Employed Russian
Employed Russian

Reputation: 213947

Don't #include "IntList.c" into main.c

Upvotes: 0

user2100815
user2100815

Reputation:

#include "IntList.c"

should be:

#include "IntList.h"

Also (though nothing to do with your problem) I would recommend not using mixed case in the names of source files, as it can lead to portability problems and hard to diagnose "no such file" errors - use all lower case, like the standard library headers do.

Upvotes: 1

Mat
Mat

Reputation: 206859

Don't include the .c file in main, include the .h file. Otherwise the code in IntList.c gets compiled both into the IntList.o and the main.o, so you'll get duplicate symbols.

Use this in main.c instead of IntList.c:

#include "IntList.h"

Upvotes: 3

Related Questions