Rakefileの雛形

C++用のRakefileの雛形を書いてみた。(gcc専用)

require 'rake/clean'
require 'rake/loaders/makefile'

CC = 'g++'
CFLAGS = '-Wall'
LDFLAGS = ''
SRCS = FileList['*.cpp']
OBJS = SRCS.ext('o')
LIBS = FileList[]
TARGET = 'sample'

CLEAN.include(OBJS)
CLOBBER.include(TARGET)

task 'default' => TARGET

file TARGET => OBJS do |t| 
  sh "#{CC} #{LDFLAGS} -o #{t.name} #{t.prerequisites.join(' ')} #{LIBS.map{|s|'-l'+s}}"
end

rule '.o' => '.cpp' do |t| 
  sh "#{CC} #{CFLAGS} -o #{t.name} -c #{t.source}"
end

# generate dependency information
SRCS.each do |src|
  depfile = '.' + src.sub(/\.[^.]+\Z/, '.mf')
  file depfile => src do |t| 
    sh "#{CC} #{CFLAGS} -MM #{t.prerequisites.join(' ')} > #{t.name}"
  end 
  import depfile
  CLEAN.include(depfile)
end
  • 同じディレクトリにあるC++ファイル(拡張子cpp)をすべてコンパイルしてリンクする。
  • ソースファイルの依存関係はgccに自動的に出力させる。(aaa.cpp というファイルがあるとき aaa.cpp の依存情報は .aaa.mf に出力される)
  • rake clean で中間出力ファイルを削除する。rake clobber で中間出力ファイル及び実行ファイルを削除する。

参考URL

No Change

久しぶりに日記を書いてみる。サークルの友達から次の問題を教えてもらった。

整数 k, x 及び v_i (i=1,..,k) が与えられるので、

\sum_{i=1}^k a_i v_i = x
a_1 \ge a_2 \ge ... \ge a_k \ge 0

を満たす整数 a_i (i=1,..,k) が存在するかどうかをYES, NOで答える問題。1≦k≦5, 1≦x,v_i≦100000.

以下解答。

続きを読む

SDL on Windows でハードウェアアクセラレーションが効かない問題

サークル内で問題になったのでメモ。

どうやら1.2.10からデフォルトのビデオドライバとして DirectX ではなく windib が使用されるようになったのが原因っぽい。

http://www.libsdl.org/release/changes-1.2.html

The "windib" video driver is the default now, to prevent problems with certain laptops, 64-bit Windows, and Windows Vista. The DirectX driver is still available, and can be selected by setting the environment variable SDL_VIDEODRIVER to "directx".

ある種のラップトップ、64 bit の Windows 及び Windows Vista における問題を防ぐため、"windib" をデフォルトのビデオドライバにした。DirectXドライバはまだ利用可能であり、環境変数 SDL_VIDEODRIVER を "directx" に設定することで選択できる。

ということで Windows において DirectX を有効にするにはプログラムの先頭で

_putenv_s("SDL_VIDEODRIVER", "directx");

としておけばいいっぽい。

Vimのエンコーディングの設定

fileencodings(複数形)に設定するらしい。

set fileencodings=ucs-bom,utf-8,iso-2022-jp,euc-jp,cp932,utf-16,utf-16le

より左にあるエンコーディングがより優先される。ucs-bomは一番先頭にしておくのが吉。

URL短縮サービス j.mp のAPIを呼び出してみた

j.mpのアカウントをとるとAPI KeyがもらえてAPIでURLを短縮できるようになる。
ということで、短縮してみた。

require 'rubygems'
require 'net/http'
require 'json'
require 'uri'

class BitLy
  def initialize(login_id, api_key)
    @login_id = login_id
    @api_key = api_key
  end 

  def shorten(url)
    query = "version=2.0.1&longUrl=#{URI.escape(url)}&login=#{@login_id}&apiKey=#{@api_key}"
    result = JSON.parse(Net::HTTP.get("api.j.mp", "/shorten?#{query}"))
    result['results'][url]['shortUrl']
  end 
end

クラス名がBitLyなのはサービスを行っているのがbit.lyだから。Net::HTTP.get("api.j.mp", ... のところを Net::HTTP.get("api.bit.ly", ... に書き換えるとbit.lyで短縮できる。

使い方

bitly = BitLy.new(アカウント名, API Key)
short_url = bitly.shorten('http://www.example.com/long_long_url')
puts short_url

[追記]
URIエスケープが抜けていたので修正。