编译C ++和SFML。适用于Xcode,但如何在终端中使用?

时间:2015-10-18 11:37:03

标签: c++ xcode terminal sfml

进行一些C ++编码并使用SFML库。我按照教程使用Xcode从模板运行示例代码,并进行了一些修改。代码如下

//
// Disclamer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resource, use the helper
// method resourcePath() from ResourcePath.hpp
//

#include <SFML/Graphics.hpp>

#include <cmath>

class Pixels : public sf::Drawable, public sf::Transformable
{
public:
  Pixels(int nrOfPixels):
  nPixels(nrOfPixels),
  nVertices(sf::Points,nrOfPixels)
  {
    initializePixels();
  }


  void initializePixels()
  {
    for (int i=0;i<nPixels.size();i++)
    {
      Pixel& p = nPixels[i];

      p.angle =  (std::rand() % 360) * 3.14f / 180.f;
      p.speed = (std::rand() % 50) + 50.f;
      p.position = sf::Vector2f(1+rand() % 500,1+rand()%500);
      p.color = sf::Color(rand()%256,rand()%256,rand()%256);
      p.velocity = sf::Vector2f(std::cos(p.angle)*p.speed,std::sin(p.angle)*p.speed);
      nVertices[i].position = p.position;
      nVertices[i].color = p.color;
    }
  }

  void updatePixels(sf::Time elapsed)
  {
    for (int i=0;i<nPixels.size();i++)
    {
      Pixel& p = nPixels[i];


      nVertices[i].position += p.velocity * elapsed.asSeconds();

      if (nVertices[i].position.x < 1)
      {
        nVertices[i].position.x = 1;
        p.velocity.x = - p.velocity.x;
      } else if ( nVertices[i].position.x > 500)
      {
        nVertices[i].position.x = 500;
        p.velocity.x = -p.velocity.x;
      }

      if (nVertices[i].position.y < 1)
      {
        nVertices[i].position.y = 1;
        p.velocity.y = - p.velocity.y;
      } else if ( nVertices[i].position.y > 500)
      {
        nVertices[i].position.y = 500;
        p.velocity.y = -p.velocity.y;
      }

    }
  }

private:
  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
  {
    // apply the transform
    states.transform *= getTransform();

    // our particles don't use a texture
    states.texture = NULL;

    // draw the vertex array
    target.draw(nVertices, states);
  }


private:

  struct Pixel
  {
    sf::Vector2f velocity;
    sf::Vector2f position;
    sf::Color color;
    float angle;
    float speed;
  };

  std::vector<Pixel> nPixels;
  sf::VertexArray nVertices;
};


int main(int, char const**)
{
  // Create the main window
  sf::RenderWindow window(sf::VideoMode(500, 500), "Random Particles");

  Pixels pixels(10000);

  // create a clock to track the elapsed time
  sf::Clock clock;

  // Start the game loop
  while (window.isOpen())
  {

    // Process events
    sf::Event event;
    while (window.pollEvent(event))
    {
      // Close window: exit
      if (event.type == sf::Event::Closed) {
        window.close();
      }

      // Escape pressed: exit
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
        window.close();
      }
    }



    // Clear screen
    window.clear();

    sf::Time elapsed = clock.restart();
    pixels.updatePixels(elapsed);


    // draw it
    window.draw(pixels);

    // Update the window
    window.display();
  }

  return EXIT_SUCCESS;
}

Xcode可以编译,链接和运行代码。但是我想知道如何从终端编译它。

首先我尝试了

g++ main.cpp

但导致找不到#include <SFML/Graphics.hpp>。所以我进入了

g++ main.cpp -I /usr/local/include

链接标题。但是这会产生以下错误:

Undefined symbols for architecture x86_64:
  "sf::VertexArray::VertexArray(sf::PrimitiveType, unsigned long)", referenced from:
      Pixels::Pixels(int) in main-de105c.o
  "sf::VertexArray::operator[](unsigned long)", referenced from:
      Pixels::updatePixels(sf::Time) in main-de105c.o
      Pixels::initializePixels() in main-de105c.o
  "sf::RenderStates::Default", referenced from:
      _main in main-de105c.o
  "sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)", referenced from:
      _main in main-de105c.o
      Pixels::draw(sf::RenderTarget&, sf::RenderStates) const in main-de105c.o
  "sf::RenderTarget::clear(sf::Color const&)", referenced from:
      _main in main-de105c.o
  "sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)", referenced from:
      _main in main-de105c.o
  "sf::RenderWindow::~RenderWindow()", referenced from:
      _main in main-de105c.o
  "sf::Transformable::Transformable()", referenced from:
      Pixels::Pixels(int) in main-de105c.o
  "sf::Transformable::~Transformable()", referenced from:
      Pixels::~Pixels() in main-de105c.o
      Pixels::Pixels(int) in main-de105c.o
  "sf::Clock::restart()", referenced from:
      _main in main-de105c.o
  "sf::Clock::Clock()", referenced from:
      _main in main-de105c.o
  "sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)", referenced from:
      _main in main-de105c.o
      Pixels::initializePixels() in main-de105c.o
  "sf::Color::Color()", referenced from:
      Pixels::Pixel::Pixel() in main-de105c.o
  "sf::String::String(char const*, std::__1::locale const&)", referenced from:
      _main in main-de105c.o
  "sf::Window::close()", referenced from:
      _main in main-de105c.o
  "sf::Window::display()", referenced from:
      _main in main-de105c.o
  "sf::Window::pollEvent(sf::Event&)", referenced from:
      _main in main-de105c.o
  "sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)", referenced from:
      _main in main-de105c.o
  "sf::operator*=(sf::Transform&, sf::Transform const&)", referenced from:
      Pixels::draw(sf::RenderTarget&, sf::RenderStates) const in main-de105c.o
  "sf::Transformable::getTransform() const", referenced from:
      Pixels::draw(sf::RenderTarget&, sf::RenderStates) const in main-de105c.o
  "sf::Time::asSeconds() const", referenced from:
      Pixels::updatePixels(sf::Time) in main-de105c.o
  "sf::Window::isOpen() const", referenced from:
      _main in main-de105c.o
  "typeinfo for sf::Transformable", referenced from:
      typeinfo for Pixels in main-de105c.o
  "vtable for sf::VertexArray", referenced from:
      sf::VertexArray::~VertexArray() in main-de105c.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决

编译:

g++ main.cpp -I /usr/local/include -L /usr/local/lib -lsfml-graphics -lsfml-window -lsfml-system

或使用clang ++:

clang++ -std=c++11 -stdlib=libc++ main.cpp -o main -I /usr/local/include -L /usr/local/lib -lsfml-system -lsfml-window -lsfml-graphics

0 个答案:

没有答案