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

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

对象.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 解决了这个问题。

关于c++ - 无法识别 g++ 文件格式;将 Object.o 视为链接描述文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12205222/

相关文章:

c++ - 制作C++脚本的问题

c - 链接不正确,函数未定义,eclipse GCC 库路径

dynamic - libstdc++ 的库搜索路径

windows - MSYS 错误 "rem: command not found"

string - Makefile字符串函数可以嵌套吗?

c++ - Makefile - 通配符,如何正确执行?

c++ - 如何使用 boost::filesystem "normalize"路径名?

c++ - 为什么 pthread_getname_np() 在 Linux 的线程中失败?

c++ - FT4222 设备信息在 Windows 下正确,在 Linux 下不正确?

C++ 在哪里定义全局变量(链接器错误 : Symbol already defined)