0

对象.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

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

4

3 回答 3

1

Were you, by any chance, using ccache? I just had a very similar problem to yours and omitting ccache in the compilation solved it.

于 2012-11-14T18:23:23.637 回答
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。

于 2012-08-30T20:49:27.050 回答
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...
于 2012-08-31T10:13:51.140 回答