在 C++ 中包含跨多个文件的库
Including a library across multiple files in C++
我必须在 Game
class 中创建一个 Character
对象。两者都包含 SDL2。
Game
class对SDL_BlitSurface
和SDL_UpdateWindowSurface
使用SDL,Character
class对[=16=使用SDL ].
有没有办法避免这个包含的冗余?编译器会只包含 SDL2 一次吗?
Will the compiler include SDL2 just once?
如果包含的 header 有一个 include guard,那么是的。
如果 header 包含任何定义,那么它 必须 有一个包含保护,否则多次包含它会使程序格式错误。
我没有遍历所有 header 的 SDL,但我假设他们每个人都使用包含保护。
I included "SDL.h" in both Game.h and Character.h. Is it a problem for source health ?
我不知道来源健康是什么。如果 Game.h
依赖于 SDL.h
中的 definitions/declarations,那么它必须包含 SDL.h
。如果 Character.h
依赖于 SDL.h
中的 definitions/declarations,那么它也必须包含 SDL.h
。没有什么不妥。很典型。
在头文件中,需要添加如下代码:
#ifndef TEST_H
#define TEST_H
// your code
#endif
或
#pragma once
这可以帮助您避免 class 多次包含!
我必须在 Game
class 中创建一个 Character
对象。两者都包含 SDL2。
Game
class对SDL_BlitSurface
和SDL_UpdateWindowSurface
使用SDL,Character
class对[=16=使用SDL ].
有没有办法避免这个包含的冗余?编译器会只包含 SDL2 一次吗?
Will the compiler include SDL2 just once?
如果包含的 header 有一个 include guard,那么是的。
如果 header 包含任何定义,那么它 必须 有一个包含保护,否则多次包含它会使程序格式错误。
我没有遍历所有 header 的 SDL,但我假设他们每个人都使用包含保护。
I included "SDL.h" in both Game.h and Character.h. Is it a problem for source health ?
我不知道来源健康是什么。如果 Game.h
依赖于 SDL.h
中的 definitions/declarations,那么它必须包含 SDL.h
。如果 Character.h
依赖于 SDL.h
中的 definitions/declarations,那么它也必须包含 SDL.h
。没有什么不妥。很典型。
在头文件中,需要添加如下代码:
#ifndef TEST_H
#define TEST_H
// your code
#endif
或
#pragma once
这可以帮助您避免 class 多次包含!