Project linkage problem with more than one source file

Asked

Viewed 91 times

0

This is to my function. It gives me error when calling in main function.

This is the header of my function, in the function file1.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
  • Your problem is not in the function code in yes. This problem happens when you call a function that is set in one source file from another, but for some reason Linker is not able to link the call with the function itself. It would be more useful for us to be able to help you if you published how you stated the function: name, parameters. You’re using a header file, right? Make sure the function statement in it matches the statement in the source file.

  • int eleminar_repeated numbers(int *vec, int n, int *vec1); this is my 1.h function (where the header is) ; - @ C. E. Gesser

  • And in the archive funcao1.c, how are you? You also need to ensure that in the file that contains the function main is being given #include "função1.h"

  • I updated my post, put more codes in my program. And in my main function I call #include "funcao1.h" @ C. E. Gesser

2 answers

1

Your build command is wrong:

gcc ex09.c      ### BAD!!!

It must be so:

gcc -o ex09 ex09.c funcao1.c   ### GOOD :)
  • Thanks for your help, I’ve already solved the problem. But I can’t delete repeat numbers @ Jose X.

1


Note 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 consists of more than one. Hence the compiler complains that at the time of generating the executable, he does not find the implementation of a function.

You have two options

Compile and link all sources at the same time:

gcc ex09.c funcao1.c

Or compile the sources individually and link separately:

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

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

  • Thanks for your help, I’ve already solved the problem. But I can’t delete repeat numbers @C. E. Gesser

  • 1

    @Programmer: I thought it best to change the title of this issue to be more in line with the problem dealt with here, which was the linkage. I took a quick look at your algorithm, and found curious the line with the command vec[i]=vec1[i];. It seems that you are discarding the contents of vec, that you organized a few lines above. It is not exactly clear which array is the input and which the output of the function is. Now that we’re past the compilation issues, you can run more tests. If you cannot solve I suggest creating one more question, now yes about the algorithm itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.