cperlcompilationmodule-build

How can I have Module::Build compile and link my small C program?


I have a simple C source file in a src directory in my project. My Build.PL contains the following lines:

c_source => ['src'],
extra_compiler_flags => ['-std=c99']

However, all this does is compile it to a .o file in the src directory corresponding to the C file. What I'd really like is to have it compiled and linked to form an executable, then put in my bin directory.

Is this possible and advisable with Module::Build?


Solution

  • Though ikegami said this is not the proper way to do things (and I agree it probably isn't...), I just ended up using the cbuilder getter on the Module::Build object to manually do the compilation. With only one C file, I figured it was a small sin, and this lets me move on!

    my $b = $builder->cbuilder();
    
    my $obj_file = $b->compile(
        source => 'src/myfile.c',
        extra_compiler_flags => ['-std=c99'],
        include_dirs => ['/my/path/zeromq-3.2.4/include']
    );
    
    my $lib_file = $b->link_executable(
        objects => $obj_file,
        extra_linker_flags => [
            '-lpthread',
            '-L/my/path/sw/zmq/lib/',
            '-lzmq'
        ],
        exe_file => 'my_file'
    );