c++ - 使用目标文件(架构的未定义符号)
问题描述
我正在为一个至少使用 15 个标头的长项目编写代码。我是头文件内的内联定义的爱好者,但是对于这么长的项目,我在使用这种方法时遇到了一些问题,所以我决定编写一个带有 .cpp 文件的头文件 .h 用于定义。由于我对这种方法有点陌生,我不确定如何编写makefile。我试试运气,但它一直给我著名的“架构 x86_64 的未定义符号”错误。我认为在makefile中我应该只使用目标文件编译所有那些使用伙伴.cpp文件的头文件,而不是内联定义(.h)。这个想法对吗?下面的 makefile 是对还是错?
生成文件
CC = gcc
G = g++
SDLFLAGS = -lSDL_image -lSDL_ttf -lSDL2
INCS= `sdl2-config --cflags --libs`
AssetManager.o: AssetManager.cpp
${CC} -std=c++17 -c AssetManager.cpp
Collision.o: Collision.cpp
${CC} -std=c++17 -c Collision.cpp
Game.o: Game.cpp
${CC} -std=c++17 -c Game.cpp
Map.o: Map.cpp
${CC} -std=c++17 -c Map.cpp
TextureManager.o: TextureManager.cpp
${CC} -std=c++17 -c TextureManager.cpp
ECS.o: ECS/ECS.cpp
${CC} -std=c++17 -c ECS/ECS.cpp
main: main.cpp *.o ECS/Animation.h ECS/ColliderComponent.h ECS/Components.h ECS/KeyboardController.h ECS/ProjectileComponent.h ECS/SpriteComponent.h ECS/TileComponent.h ECS/TransformComponent.h ECS/UILabel.h colors.h
${G} -std=c++17 main.cpp -o main *.o ${INCS} ${SDLFLAGS}
clean:
rm *.o
cleanmain:
rm main
edit:
vim main.cpp
正如我之前预期的那样,所有使用 .o 的命令都适用于 .cpp 文件附带的标头,所有内联标头 .h 都包含在该行中main: main.cpp *.o ECS/Animation.h ECS/ColliderComponent.h etc..
它给我的错误示例如下:
Undefined symbols for architecture x86_64:
"Vector2D::Zero()", referenced from:
TransformComponent::init() in AssetManager.o
TransformComponent::TransformComponent() in AssetManager.o
TransformComponent::init() in Game.o
TransformComponent::TransformComponent() in Game.o
TransformComponent::TransformComponent() in Map.o
TransformComponent::init() in Map.o
"Vector2D::Vector2D(float, float)", referenced from:
Game::init(char const*, int, int, bool) in Game.o
"Vector2D::Vector2D()", referenced from:
TransformComponent::TransformComponent(float, float, int, int, int) in AssetManager.o
TransformComponent::TransformComponent() in AssetManager.o
TransformComponent::TransformComponent(float, float, int, int, int) in Game.o
TransformComponent::TransformComponent() in Game.o
TransformComponent::TransformComponent() in Map.o
TileComponent::TileComponent(int, int, int, int, int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in Map.o
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Vector2D const&)", referenced from:
Game::update() in Game.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
其中 TransformComponent 实际上只是一个 .h 标头...我哪里出错了?
解决方案
推荐阅读
- c# - LINQ 将数组与 DataRow 中的另一个数组匹配
- javascript - 我可以使用 javascript 在我的 html 文档中抓取其他网站吗?
- javascript - 简单的香草 JS 游戏
- python - 具有嵌套循环的理解列表中的迭代计数器
- javascript - Puppeteer 无法拦截 xhr 响应 json
- javascript - 触摸图像时如何使用javascript检测右键单击
- java - 合并多个 SDK
- node.js - 具有其他模式数组字段的猫鼬模式
- python - 通过腌制文件在python中加载模型的特征重要性
- build - 如何将依赖的第三方库复制到目标文件夹