0
  • I am creating a ros-node talker to read the CAN bus data and decode.
  • To do that, I have to call a function from another cpp file (decoder).

How can I do that?

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
Fourier
  • 3
  • 3
  • 6
    Open your C++ textbook to the chapter that explains how to create your own header files, declare functions, classes, and methods, so that they can be used in different translation units. I would expect this subject matter to be well covered in every C++ textbook. Is there anything specific in your textbook's explanation, or examples, that's unclear to you? – Sam Varshavchik Jun 17 '20 at 11:01
  • yes, I am confused about how to call those functions from other cpp file. That's why I asked this question. – Fourier Jun 17 '20 at 11:34
  • 2
    A given function is always called exactly the same way. Whether it's defined in the same source file or some other one makes no difference, whatsoever. Which specific part of your textbook's explanation and examples of creating and using your own header files is unclear to you? – Sam Varshavchik Jun 17 '20 at 11:42
  • Does this answer your question? [How to call functions from one .cpp file in another .cpp file?](https://stackoverflow.com/questions/51488008/how-to-call-functions-from-one-cpp-file-in-another-cpp-file) – parth_07 Jun 18 '20 at 05:25

1 Answers1

0

In order to use a function from some cpp file (say a.cpp) a matching header file (a.h here) should contain the signature of that function. That is, its name, return type and list of input parameters, without the actual body. Then, if another cpp file b.cpp states #include a.h it ca freely use every function listed in that header file.

$ cat a.cpp
int add(unsigned int x,unsigned int y){return x+y;}
$ cat a.h
int add(unsigned int x,unsigned int y);
$ cat b.cpp
#include "a.h"
int mul(unsigned int p,unsigned int q)
{
    if (p=0) return 0;
    else return add(mul(p-1,q),q);
}
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87