Skip to content

Instantly share code, notes, and snippets.

@nofxx
Created November 13, 2014 03:20
Show Gist options
  • Save nofxx/43f104b879cc07c60f2d to your computer and use it in GitHub Desktop.
Save nofxx/43f104b879cc07c60f2d to your computer and use it in GitHub Desktop.
C++/Cpp Rakefile with includes, links... OpenCV example
#
# CPP Rakefile
#
EXEC = "square" # Your app executable
FLAGS = "-Wall -Wextra" # Flags for building
EXTRA = %w{ OpenCV } # Extra includes
#
# Nothing to see here, move along...
#
COMPILER = "g++"
# Compilation (add flags as needed)
CXXFLAGS = EXTRA.map { |e| `pkg-config #{e.downcase} --cflags` }.join.chomp
# Linking (add flags as needed)
LDFLAGS = EXTRA.map { |e| `pkg-config #{e.downcase} --libs` }.join.chomp
C_FILES ||= Dir.glob("./*.c")
CPP_FILES ||= Dir.glob("./*.cpp")
BUILD_OUTPUT ||= 'build'
def build_output_path(file)
Dir.mkdir(BUILD_OUTPUT) if Dir.exist?(BUILD_OUTPUT) == false
File.join(BUILD_OUTPUT, file)
end
desc "Running unit tests"
task :spec do
end
task :c do
C_FILES.each do |source|
end
end
task :cpp do
CPP_FILES.each do |source|
output = build_output_path(File.basename(source, File.extname(source)) + ".o")
sh "#{COMPILER} #{FLAGS} #{CXXFLAGS} -c -o #{output} #{source}"
end
end
task :link do
os = CPP_FILES.map { |f| BUILD_OUTPUT + f[1..-1][0...-3] + "o" }
sh "#{COMPILER} #{FLAGS} #{LDFLAGS} #{os.join(" ")} -o #{EXEC}"
end
desc "Clean stuff"
task :clean do
files = (Dir["#{EXEC}"] + Dir[BUILD_OUTPUT]).uniq
rm_rf files unless files.empty?
end
desc "Compile App"
task :compile => [:c, :cpp, :link]
task :default => [:compile]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment