GithubHelp home page GithubHelp logo

5-minutes-to-buck's Introduction

5 Minutes to Buck

This is a very quick Buck tutorial. The final source-files are above.

๐Ÿšจ This tutorial was written with Linux in mind. Windows and macOS users, please refer to Facebook's setup instructions. Otherwise, the steps are the same.

0. What tools will I need?

You will need GCC / Clang, a text editor and Buck.

You probably have the first two, so here's how to install Buck for Linux:

wget https://github.com/njlr/buck-warp/releases/download/v0.3.0/buck-2019.01.10.01-linux -O buck
chmod +x ./buck
sudo mv ./buck /usr/local/bin/buck
buck --version

1. How do I create an executable?

Create the files we need:

touch .buckconfig
touch BUCK
touch main.cpp

main.cpp will contain a hello-world application:

#include <iostream>

using namespace std;

int main() {
  cout << "Hello, world. " << endl;

  return 0;
}

BUCK will describe how to build our application:

cxx_binary(
  name = 'app',
  srcs = [
    'main.cpp',
  ],
)

Now you can run the application:

buck run :app

You should see:

Hello, world. 

2. How do I create a library?

These instructions follow on from the previous.

We will put our library into a folder called math. Create these files:

mkdir math
touch ./math/BUCK
touch ./math/square.hpp
touch ./math/square.cpp

You project should look like this:

tree . 
.
โ”œโ”€โ”€ BUCK
โ”œโ”€โ”€ main.cpp
โ””โ”€โ”€ math
    โ”œโ”€โ”€ BUCK
    โ”œโ”€โ”€ square.cpp
    โ””โ”€โ”€ square.hpp

1 directory, 5 files

Now we need to fill write the contents.

math/square.hpp

#ifndef MATH_SQUARE_HPP
#define MATH_SQUARE_HPP

int square(int x);

#endif

math/square.cpp

#include <math/square.hpp>

int square(int x) {
  return x * x;
}

math/BUCK:

cxx_library(
  name = 'math',
  exported_headers = [
    'square.hpp',
  ],
  srcs = [
    'square.cpp',
  ],
  visibility = [
    'PUBLIC',
  ],
)

Now we should be able to build the library:

buck build //math:math

Success!

We can request static or shared flavors:

# Shared
buck build //math:math#linux-x86_64,shared --show-output

# Static
buck build //math:math#linux-x86_64,static --show-output

The --show-output flag asks Buck where it put the file. In this case:

ls buck-out/gen/math/math\#linux-x86_64,shared
ls buck-out/gen/math/math#linux-x86_64,static/

3. How do I use the library from the application?

First, we need to edit BUCK to tell Buck that //:app depends on //math:math:

cxx_binary(
  name = 'app',
  srcs = [
    'main.cpp',
  ],
  deps = [
    '//math:math',
  ],
)

Notice how we added a deps property.

Now, we can change main.cpp to actually use the library:

#include <iostream>
#include <math/square.hpp>

using namespace std;

int main() {
  cout << "Hello, world. " << endl;
  cout << "square(4) = " << square(4) << endl;

  return 0;
}

And for the big finale buck run :app...

$ buck run :app
Parsing buck files: finished in 0.7 sec
Building: finished in 0.8 sec (100%) 8/8 jobs, 2 updated
  Total time: 1.8 sec
Hello, world. 
square(4) = 16

5-minutes-to-buck's People

Contributors

njlr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.