0

I might not be able to give full code but I could create a smaller situation. I have an api, say abc which is declared as below in abc.h

extern int abc ( int x, int y = 0);

This api is used at multiple files such as x.cpp, y.cpp and z.cpp.

In file, z.cpp I wanted to use this api along with passing one extra paramter to it. So, I have changed the declaration inside abc.h as :

extern int abc ( int x, int y = 0, int z = 0)

This way, I would not need to change the call to these api calls at files x.cpp and y.cpp. Right? I could easily do my work under 'z' inside definition of abc api. Something like:

int abc(int x, int y, int z) {
  ...
  ...
  if (z) {
    <do_whatever>
  }
}

But when I compile it, I see 'undefined reference to `abc' at files x.cpp & y.cpp. What is happening?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45

1 Answers1

3

This way, I would not need to change the call to these api calls at files x.cpp and y.cpp. Right?

You are right that you don't have to change the calls in x.cpp and y.cpp.

However, you still need to recompile them, otherwise you will get the link editor errors that you mention. Ideally your build system should see that x.o and y.o are dependencies of abc.h and that if the header changes both object files need to be regenerated.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43