The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AutoCons::HOWTO::C - HOWTO write a Construct.PL for a C program

DESCRIPTION

AutoCons is a cons Construct generator similar to ExtUtils::MakeMaker or Gnu Autoconf, except that cons is far more portable than make. For the developer, this means that your program will build on any system that cons will. For a user, this means that you don't need a "make" program to build your program.

This HOWTO describes how to write a Construct.PL to build C programs.

Install

Modules are installed with a simple group of commands.

 perl Construct.PL
 cons
 cons t
 su -c "cons <prefix>"

Where <prefix> is where Your program will install to. Usually it's /usr on UNIX like systems and C:\Program Files on Windows ones.

Adding targets

In order to build a C program, you have to add new targets that match up to your source. For example, to build the simple Hello World!:

 Use AutoCons;
 WriteCS(
   Name => "Hello World!",
   Version => "1",
 );
 Targ("Program","blib/hello","hello.c");

"Why is the hello executable stored in blib?" Well, when AutoCons configures the Construct, it sets it up to build the blib directory by default (when the user runs cons.) So by storing the output (hello) in blib, you tell cons to build it by default.

Other types of targets are available too.

 Use AutoCons;
 WriteCS(
   Name => "Hello World!",
   Version => "1",
 );
 Targ("Program","blib/bin/hello","[ hello.c, blib/lib/libworld ]");
 Targ("Library","blib/lib/libworld","libworld.c");

Your program also needs to be installed.

 Use AutoCons;
 WriteCS(
   Name => "Hello World!",
   Version => "1",
 );
 Targ("Program","blib/bin/hello","[ hello.c, blib/lib/libworld ]");  
 Targ("Install","$installbin","blib/bin/hello");
 Targ("Library","blib/lib/libworld","libworld.c");
 Targ("Install","$installlib","blib/lib/libworld");

For a full list of target types, see "Default construction methods" in cons.

Config.h

It is also possible to find system-dependent information. This is done by creating a Config.h.PL.

This file will load AutoCons::ConfigH as opposed to the regular AutoCons. The Config.h file it generates will contain ALL of the info contained in Config.pm (a database of system-dependent information packages with perl with over 900 values) as well as any information you add to it. See Config for more info. A basic Config.h.PL would look like this.

 use AutoCons::ConfigH;
 ConfigH();

Config.t and Config.t.d

For more elaborate results, use a Config.t or Config.t.d. The Config.t is a script that outputs in TAP format (see Test::Harness::TAP . Here is a dummy written in standard SH.

 #!/bin/bash
 echo "1..2"
 echo "ok 1" # Ok
 #echo "not ok 2" # Error

Adding to the Config.h is done by writing to Config.h.part. Any code added to it will be appended to Config.h. For example

 #!/bin/bash
 echo "1..2"
 echo "ok 1" # Ok
 #echo "not ok 2" # Error
 echo "blablabla" >>Config.h.part

You can write this in any language (I used shell since almost anyone knows it).

Config.t.d

This directory contains .t files. They contain the same syntax that Config.t does, though it is easier to manage multiple files that test multiple features.

SEE ALSO

Other HOWTOs: AutoCons::HOWTO(1) AutoCons::HOWTO::Perl(1)

Other: AutoCons(3) cons(1) perl(1)