g++文件格式无法识别;将Object.o视为链接器脚本

g++ file format not recognized; treating Object.o as linker script

本文关键字:链接 脚本 Object 格式 文件 识别 g++      更新时间:2023-10-16

对象.hpp

#ifndef OBJECT_HPP
#define OBJECT_HPP
#include <SFML/Graphics.hpp>
using namespace std;
class Object {
  private:
    sf::Image image;
  public:
    float x;
    float y;
    int width;
    int height;
    sf::Sprite sprite;
    virtual void update();
};
#endif

对象.cpp

void Object::update() {
}

这是我的Makefile:

LIBS=-lsfml-graphics -lsfml-window -lsfml-system
all:
    @echo "** Building mahgame"
State.o : State.cpp
    g++ -c State.cpp
PlayState.o : PlayState.cpp
    g++ -c PlayState.cpp
Game.o : Game.cpp
    g++ -c Game.cpp
Object.o : Object.cpp
    g++ -c Object.cpp
Player.o : Player.cpp
    g++ -c Player.cpp
mahgame : Game.o State.o PlayState.o Object.o Player.o
    g++ -o mahgame Game.o State.o PlayState.o Object.o Player.o $(LIBS)
    #g++ -c "State.cpp" -o State.o
    #g++ -c "PlayState.cpp" -o PlayState.o
    #g++ -c "Game.cpp" -o Game.o
    #g++ -c "Object.hpp" -o Object.o
    #g++ -c "Player.hpp" -o Player.o
    #g++ -o mahgame Game.o State.o PlayState.o Object.o Player.o $(LIBS)
clean:
    @echo "** Removing object files and executable..."
    rm -f mahgame
install:
    @echo '** Installing...'
    cp mahgame /usr/bin
uninstall:
    @echo '** Uninstalling...'
    rm mahgame

以下是我在构建时遇到的错误(构建后,这是一个链接器错误):

/usr/bin/ld:Object.o: file format not recognized; treating as linker script
/usr/bin/ld:Object.o:1: syntax error
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

知道发生了什么事吗?提前谢谢。

您是否在使用ccache?我刚刚遇到了一个与您非常相似的问题,在编译中省略了ccache解决了这个问题。

您的Makefile看起来很完美,虽然有点冗长,并且缺少标头依赖项。我假设shell命令有一个前导制表符。我假设您的构建命令是make mahgame

正如您所说,您有一个链接器错误。CCD_ 2似乎不是有效的CCD_ 3。让编译器重新生成它。

$ rm Object.o
$ make mahgame
g++ -c Object.cpp
g++ -o mahgame Game.o State.o PlayState.o Object.o Player.o...

生成文件的格式为:

xxx.o : xxx.cpp
   g++ -c xxx.cpp

你的一点也不像。所以,将您的更改为:

LIBS=.....
[EDIT]
all : mahgame rmerr
rmerr :
   rm -f err
[/EDIT]
State.o : State.cpp
   g++ -c State.cpp 2>>err
PlayState.o : PlayState.cpp
   g++ -c PlayState.cpp 2>>err
.....
mahgame : Game.o State.o .....
   g++ -o mahgame Game.o State.o PlayState.o Object.o Player.o $(LIBS) 2>>err

请注意,这是您的第一步,还有更好的方法来编写makefile,不包括源文件/对象文件等的每个细节。