# Using Rake to build C++

**URL:** <https://www.ruby-forum.com/t/using-rake-to-build-c/106051>\
**Category:** Ruby\
**Created:** [June 5, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/using-rake-to-build-c/106051 "2007-06-05T23:10:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnus\_falk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@magnus\_falk](https://www.ruby-forum.com/u/magnus_falk)\
**Post date:** [June 5, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/using-rake-to-build-c/106051/1 "2007-06-05T23:10:35Z")

</div>

I’m making myself a build environment and found this Rake thingy that  
seemed just to good to pass upon. But since I don’t know any Ruby I’m  
struggling a bit.

I’m making a couple of tasks to compile and link C++ code and it seems  
like I’ve misunderstood the file task somewhat. In the example code  
below, the compilation and linking is never run, what am I doing wrong?

cppTasks.rb:  
def compileTask inclDirs, flags  
inclDirs = inclDirs.collect { |dir| ‘-I’ + dir }  
inclDirs = inclDirs.join(’ ‘)  
flags = flags.join(’ ')

SRC\_FILES.each do |srcFile|  
objFile = File.join(OBJ\_DIR, srcFile.pathmap("%f").ext(‘o’))  
file objFile =\> [srcFile] do  
sh “g++ #{inclDirs} #{flags} -c #{srcFile} -o #{objFile}”  
end  
end  
end

def linkTask libDirs, libs, flags, target  
libDirs = libDirs.collect { |dir| ‘-L’ + dir}  
libs = libs.collect { |lib| ‘-l’ + (lib[/lib(.+)./, 1])}

flags = flags + ((File.extname(target) == ‘.so’) ? [’-shared’] :  
[’-c’])

libDirs = libDirs.join(’ ‘)  
libs = libs.join(’ ‘)  
flags = flags.join(’ ')

targetFile = File.join(BUILD\_DIR, target)

file targetFile =\> OBJ\_FILES do  
objFiles = OBJ\_FILES.collect { |objFile| objFile + ’ ’ }  
sh “g++ #{libDirs} #{libs} #{flags} #{objFiles} -o #{targetFile}”  
end  
end

Cheers,  
Magnus

---

<div class="post-metadata">

**Author:** ![magnus\_falk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@magnus\_falk](https://www.ruby-forum.com/u/magnus_falk)\
**Post date:** [June 6, 2007, 12:06am UTC](https://www.ruby-forum.com/t/using-rake-to-build-c/106051/2 "2007-06-06T00:06:53Z")

</div>

Oh, another thing while I’m at it. Is there any way to “export”  
variables defined in one task to make them visible in other task?  
Similar to “antcall”  
([http://ant.apache.org/manual/CoreTasks/antcall.html](http://ant.apache.org/manual/CoreTasks/antcall.html)) with the flag  
inheritAll.

I’m having problems with dependencies between variables and I would like  
to have a little more control over when they are assigned instead of  
just at the moment when they are imported.

---

<div class="post-metadata">

**Author:** ![magnus\_falk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@magnus\_falk](https://www.ruby-forum.com/u/magnus_falk)\
**Post date:** [June 11, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/using-rake-to-build-c/106051/3 "2007-06-11T13:07:42Z")

</div>

On 6/5/07, Magnus F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def compileTask inclDirs, flags  
> end  
> flags = flags.join(’ ')
> 
> targetFile = File.join(BUILD\_DIR, target)
> 
> file targetFile =\> OBJ\_FILES do  
> objFiles = OBJ\_FILES.collect { |objFile| objFile + ’ ’ }  
> sh “g++ #{libDirs} #{libs} #{flags} #{objFiles} -o #{targetFile}”  
> end  
> end

Any luck on your own yet?

Take a look at the Rake examples:  
[http://docs.rubyrake.org/read/chapter/1#page3](http://docs.rubyrake.org/read/chapter/1#page3)  
[http://docs.rubyrake.org/read/chapter/9#page31](http://docs.rubyrake.org/read/chapter/9#page31)

You need to make TASKs and FILEs, not just functions,  
although each task and file declaration can call functions.

The tutorial is very good and shows a C example.

Les
