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?

4
  • 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? Commented Jun 17, 2020 at 11:01
  • yes, I am confused about how to call those functions from other cpp file. That's why I asked this question. Commented Jun 17, 2020 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? Commented Jun 17, 2020 at 11:42
  • Does this answer your question? How to call functions from one .cpp file in another .cpp file? Commented Jun 18, 2020 at 5:25

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.