g ++文件格式无法识别;将Object.o视为链接描述文件

时间:2012-08-30 20:46:49

标签: c++ linker makefile g++ linker-errors

Object.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

Object.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

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

3 个答案:

答案 0 :(得分:1)

你有没有机会使用ccache? 我刚刚遇到了一个非常类似的问题,在编译中省略了ccache解决了它。

答案 1 :(得分:0)

makefile的格式如下:

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。

答案 2 :(得分:0)

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

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

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