error: LNK2005 Approach method already defined in *.obj

Make a record of this problem,
and then define the int poly20[] array and the int generateNoise function in noise.cc.
Declare the int generateNoise function in the header file noise.h, and include noise.h in the main function, call the generateNoise function and
find that the linker reports an error. error: LNK2005 already defined poly20 and generateNoise in *.obj

In noise.cc, add static in front of int to solve it, but I don’t understand the principle. . .

(1) When modifying variables, the static local variables modified by static are only initialized once, and the life cycle of the local variables is extended, and they are not released until the end of the program.
(2) When static modifies a global variable, the global variable can only be accessed in this file, and cannot be accessed in other files, even if it is an external declaration of extern.

Maybe it can only be initialized once

Guess you like

Origin blog.csdn.net/weixin_39987672/article/details/121610427