0

I have a problem compiling separate module in C with gcc:

basically, i have my header, common.h:

#ifndef COMMON_M
    #include<stdio.h>
    #define COMMON_M
    void print_array(int *a,int n);
    void swap(int *v,int i,int j);
#endif

my functions.c with implementations of functions above:

#include "common.h"
void print_array(int *a,int n) {
    int i=0;
    for(;i<n;++i)
        printf( (i==n-1) ? "%d\n" : "%d ", a[i]);
}
void swap(int *v,int i,int j) {
    int t=v[i];
    v[i]=v[j];
    v[j]=t;
}

and my main:

#include "common.h"
void main() {
    int a[10]={6,2,0,12,4,3,7,-4,12,7};
    print_array(a,10);
    qsort_c(a,0,10);
    print_array(a,10);
}

if i try compiling funcionts.c as a module ( gcc -c functions.c ) i get this error:

error: redefinition of ‘swap’ error: redefinition of ‘print_array’

would anyone kindly help me understand why? I am declaring a function in common.h and defining it in functions.c! I don't see why this shouldn't work in theory.

Thanks in advance,

Shai
  • 111,146
  • 38
  • 238
  • 371
sowdust
  • 87
  • 1
  • 9

1 Answers1

0

From the above code, what I am seeing is: You included common.h two times. One in myfunction.c and another in your file, where you have main().

cipher
  • 2,414
  • 4
  • 30
  • 54
  • That should not be a problem, and is, in fact probably for the best as long as `main.c` and `myfunction.c` are compiled separately. That's obviously different if `main.c` `#include`s `myfunction.c`, but that doesn't seem to be the case here (although that would be the reason for the include guards...). – twalberg Dec 03 '12 at 16:50
  • @twalberg . Since the code worked fine on many of SO user's compiler and mine too, I just guessed if `main.c` `#include`s `myfunction.c` . Then it may have been an cause. If only the user could elaborate – cipher Dec 03 '12 at 16:56