0

I am currently starting work on a project where i would have a file of shared tool functions which i can use without having to instance the class, i have read about using static functions for this so i decided to try to do the same. However i seem to keep getting an error of undefined reference to my static function. Looking for an answer I came upon several answers like the one in the following link but i still fail to compile my code: undefined reference to a static function

I decided to copy and paste the code given in the link above, a.cpp/.h and b.cpp/.h files, but even then i get compilation errors: undefined reference to main [this error i can remove by adding a simple main function in b.cpp] undefined reference to A::funcA(int)

Am i forgeting something when compiling? I am simply using g++ b.cpp am using g++ 4.7.2.

Thanks in advance.

Community
  • 1
  • 1
  • I posted the new question because i coudn´t comment on the existing answers of the one i quoted (need 50 reputation). You can close the question if its not useful thanks :) – Rasistlin89 Mar 14 '15 at 18:15

2 Answers2

1

You need to compile a.cpp as well:

g++ b.cpp a.cpp

Otherwise the linker can't find the definition for A::funcA because it's located in a.cpp.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

Using the code from the linked question as an example: since the definition of the function A::funcA(int) is in a.cpp, you also need to include that source file in your compilation, like

g++ b.cpp a.cpp
Akos Bannerth
  • 1,964
  • 18
  • 14