g++:致命错误:无法将 -o 与 -c、-S 或 -E 与多个文件一起指定 [英] g++: fatal error: cannot specify -o with -c, -S or -E with multiple files

查看:45
本文介绍了g++:致命错误:无法将 -o 与 -c、-S 或 -E 与多个文件一起指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其他库文件编译库文件.我在 makefile 中使用以下行来创建 gameobject.o:

I am trying to compile a library file using other library files. I am using the following line in my makefile to create gameobject.o:

lib/gameobject.o: src/gameobject.cpp src/vector.hpp lib/objectevent.o lib/sprite.o
g++ $^ -c -o $@ $(SFML_FLAGS)

所有依赖项都正确编译,但在尝试编译 gameobject.o 时出现以下错误:

All the dependencies comile correctly, but I get the following error when it tries to compile gameobject.o:

g++: fatal error: cannot specify -o with -c, -S or -E with multiple files

我对使用 make/separating 编译还是有点陌生​​,所以我不太确定我应该做什么.我只需要编译它而不设置输出吗?我必须在不使用任何其他 .o 文件的情况下编译 gameobject.o 吗?如果这是真的,如果您不能使用其他库编译库,那么对于大型对象来说编译时间不会变得相当长吗?还是我只是读错了这个错误?

I'm still a bit new to using make/separating compilation, so I'm not quite sure what I should do. Do I just have to compile it without setting an output? Do I have to compile gameobject.o without using any of my other .o files? If that's true, wouldn't compile times get pretty big for large objects if you can't compile libraries with other libraries? Or am I just reading this error completely wrong?

推荐答案

你不是在这里构建库文件..o 文件是一个目标文件.通常每个源文件有一个目标文件.当您使用编译器的 -c 选项时,它采用单个源文件并将其编译为单个目标文件.您不能将其他目标文件添加到现有目标文件中,因此将 .o.cpp 文件添加到与 -c 相同的编译器行中不会工作.

You're not building a library file here. A .o file is an object file. Typically there is one object file per source file. When you use the compiler's -c option, it takes a single source file and compiles it into a single object file. You cannot add other object files into an existing object file, so adding both .o and .cpp files into the same compiler line with -c is not going to work.

如果你想创建一个库,那就是 libfoo.a (这里的a"代表归档").如果你想创建一个可执行文件,你也可以这样做.

If you want to create a library, that would be something like libfoo.a (the "a" here stands for "archive"). If you want to create an executable you can do that as well.

在我们描述如何得到它之前,你需要更清楚你想要什么结果.

You need to be more clear about exactly what result you want before we can describe how to get it.

这篇关于g++:致命错误:无法将 -o 与 -c、-S 或 -E 与多个文件一起指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆