Abel Tom
Abel Tom

Reputation: 144

Using rakefile to compile C files

I encounter a problem while compiling C files using rake. I am able to define my constant in the rakefile, and print out the value of this constant in my C file.

This works fine, compiles, links and executes as expected, when the defined constant is an integer. But, when a string is defined as constant in the rake file, i'm not able to compile. I'm sure i'm missing the right syntax here.

Here's my rakefile.rb

require 'rake/clean'

CLEAN.include('*.o')
CLOBBER.include('*.exe')

source_files = Rake::FileList["*.c"]
object_files = source_files.ext(".o")

TEST_CONST="A STRING" #### <---- This causes problem

task :default => "test_app"

task :clean do
    sh "rm -rfv *.o"
end

desc "Build the binary executable"
file "test_app" => object_files do |task|
    sh "gcc #{object_files} -o #{task.name}"
end


rule '.o' => '.c' do |task|
    sh "gcc -c #{task.source} -DVAR=#{TEST_CONST}"
end

This is my C file:

#include <stdio.h>

int main () {
    printf("Running main\n");
    printf("VAR: %s\n",VAR);
}

This is the error log:

<command-line>:0:5: error: ‘A STRING’ undeclared (first use in this function)
main.c:6:24: note: in expansion of macro ‘VAR’
     printf("VAR: %s\n",VAR);
                        ^~~
<command-line>:0:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:6:24: note: in expansion of macro ‘VAR’
     printf("VAR: %s\n",VAR);
                        ^~~
rake aborted!

In a makefile, i would add something like this, to CFLAGS: -DSTRVAR=\"$(VAR)\" I have no idea how to do this using rake, i.e passing a string constant as compile flag. Has anyone come across this specific problem before, and could give some ideas?

EDIT 1:

I changed the asignment to TEST_CONST="'(A STRING)'" as suggested, and this is the error im getting, im running on Ubuntu Linux.

gcc -c main.c -DVAR='A STRING'
main.c: In function ‘main’:
<command-line>:0:5: error: ‘A’ undeclared (first use in this function)
main.c:10:25: note: in expansion of macro ‘VAR’
     printf("VAR: %s \n",VAR);
                         ^~~
<command-line>:0:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:10:25: note: in expansion of macro ‘VAR’
     printf("VAR: %s \n",VAR);
                         ^~~
<command-line>:0:7: error: expected ‘)’ before ‘STRING’
main.c:10:25: note: in expansion of macro ‘VAR’
     printf("VAR: %s \n",VAR);
                         ^~~
rake aborted!
Command failed with status (1): [gcc -c main.c -DVAR='A STRING'...]
/home/abel/Projects/rake_test/rakefile.rb:23:in `block in <top (required)>'
Tasks: TOP => default => test_app => main.o
(See full trace by running task with --trace)

Upvotes: 0

Views: 611

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

TEST_CONST="\'A STRING\'" would work.

It’ll result in:

sh "gcc -c #{task.source} -DVAR=#{TEST_CONST}"

interpolated to:

gcc -c test.c -DVAR='A STRING'

Upvotes: 1

Related Questions