invoke functions defined in other files in c ++ [duplicate]

1

I have the need to include a function defined in an r.cpp file in another called a.cpp, I tried the following:

In the r.cpp file:

    #include "r.h"

    void lee(){
     cout<<" Hola "<<endl;
    }

In the r.h file:

    #ifndef R_H
    #define R_H

    void leer();

    #endif

In the a.cpp file:

    #include <iostream>
    #include "r.h"
    using namespace std;
    int main(){
      lee();
      return 0;
    }

Compiled in the console as follows:

g++ a.cpp

But I threw the following error:

  

/tmp/ccFRVxGt.o: In function main': a.cpp:(.text+0x5f): undefined reference to reads () 'collect2: error: ld returned 1 exit status

How could I solve it?

    
asked by tk071 22.02.2018 в 18:07
source

1 answer

1

Notice that the head of the function matches the body. In your case, you define the read () function in the r.h file, but in r.cpp it is called read (). They have to be both equal, and as in a.cpp you call to read (), you would have to change read () by read () in r.h

    
answered by 22.02.2018 в 18:12