Static function declared but not defined in C++

后端 未结 7 1998
小鲜肉
小鲜肉 2020-12-23 18:50

I\'m getting an error from the following code using C++.

Main.cpp

#include \"file.h\"

int main()
{
   int k = GetInteger();
   retu         


        
7条回答
  •  Happy的楠姐
    2020-12-23 19:15

    If everything is in the same translation unit it should work. You probably did not compile File.cpp into the same unit as Main.cpp.

    g++ -Wall File.cpp Main.cpp
    

    If each file is compiled separately the function must be made extern to be used from a different translation unit.

    extern int GetInteger();
    

    which is the same as

    int GetInteger();
    

提交回复
热议问题