Linking problem in project with more than one source file

0

This is my job. Give me an error when I call the main function.

This is the header of my function, in the file function1.h

int eleminar_numeros_repetidos(int *vec, int n, int *vec1); 

File function1.c:

void organizar_vetor(int *vec, int n){

   int i,j;

   for(i=0 ; i <n-1; i=i+1)
   {
       for(j=i+1 ; j<n ; j=j+1)
       {
           if (*(vec +i) >*(vec+j))
           {
            int aux =*(vec+i);
            *(vec+i)=*(vec+j);
            *(vec+j)=aux;
           }

       }

   }
}

int eleminar_numeros_repetidos(int *vec, int n, int *vec1){

 organizar_vetor(vec,n);

 int i,j;

   for(i=0 ; i <n-1; i++)
   {
       for(j=i+1 ; j<n ; j++)
       {
           vec[i]=vec1[i];

            if (vec1[i] == vec1[j])
           {

            int k;   
              for (k = j; k <n-1 ; k++)
              {
                  vec1[k]=vec1[k+1];
                  j--;n--;
              }


           } 

       }

   }
   return n;

}

Error:

gcc ex09.c
/tmp/ccYQaeVL.o: In function 'main':
ex09.c:(.text+0x112): undefined reference to 'eleminar_numeros_repetidos'
collect2: error: ld returned 1 exit status
make: *** [ex09.o] Error 1
    
asked by anonymous 01.10.2016 / 22:53

2 answers

1

Notice the command line that was used to compile:

gcc ex09.c

You are instructing the compiler to generate an executable from a source file only, but your project is composed of more than one. So the compiler complains that at the time of generating the executable, it does not find the implementation of a function.

You have two options

Compile and link all sources at once:

gcc ex09.c funcao1.c

Or compile fonts individually and link separately:

gcc -c ex09.c
gcc -c funcao1.c
gcc ex09.o funcao1.o

Note that the -c parameter instructs gcc to only compile the source files, without linking the final executable.

    
01.10.2016 / 23:53
1

Your compile command is wrong:

gcc ex09.c      ### BAD!!!

It should look like this:

gcc -o ex09 ex09.c funcao1.c   ### GOOD :)
    
01.10.2016 / 23:47