7

I need to compile my program with or without some libraries depending on which of the two hosts it is running. I don't know what to use at the right side of HOST= in my makefile to make this work as I want it to:

   ifeq(${HOST},${ADDITIONAL_LIBS_HOST})
   ADD_LIBS= ...   

${ADDITIONAL_LIBS_HOST} is the name of host as gotten from echo ${HOSTNAME}

Shayan Pooya
  • 1,049
  • 1
  • 13
  • 22
morynicz
  • 2,322
  • 2
  • 20
  • 34

2 Answers2

8

A few thoughts:

  • This is the sort of situation GNU autoconf was designed to address. Run ./configure, figure out what libraries are available, and generate an appropriate Makefile.

  • You could get the current hostname by doing something like:

    HOST=$(shell hostname)
    

    You could then use this in your conditional.

  • You could instead have your Makefile do something like:

    include Makefile.local
    

    And then have different Makefile.local files on each host.

Re: your comment, given a Makefile like this:

HOST=$(shell hostname)

all:
    @echo $(HOST)

Will generate the following output:

$ make all
fafnir.local

(Assuming your local host is "fafnir.local". Which mine is.)

larsks
  • 277,717
  • 41
  • 399
  • 399
  • `HOST=$(shell HOSTNAME)` does not work, I've tried it already. But I don't know how could I miss the include directive. Thank You very much. – morynicz Nov 05 '11 at 08:07
  • 2
    `HOST=$(shell hostname)` works fine. Notice that you have decided for whatever reason to capitalize `HOSTNAME`, which is not actually the name of a command. – larsks Nov 05 '11 at 12:02
  • 1
    erm, I've tried to use environmental variable HOSTNAME, and so I used $(shell `HOSTNAME`), `$(shell ${HOSTNAME})`, `$(shell $${HOSTNAME})` and whatever came to my mind. I didn't know that there is actually a command `hostname`. And of course You are right, `HOST=$(shell hostname)` works fine. – morynicz Nov 05 '11 at 14:00
  • 1
    If you're using an environmental variable than you wouldn't use the $(shell ...) directive, which is used to run a command. For an environment variable you just use it like a normal make variable (e.g., $(HOME)). – larsks Nov 05 '11 at 20:03
  • It seems that `HOST=$(HOSTNAME)` doesn't work (tried on two *buntu PC's). HOME on the other hand does. – morynicz Nov 05 '11 at 22:32
1

The hostname console command is not present on all distributions, especially for distributions with systemd.

The uname command can be used as an alternative.

hostname := $(shell uname -n)

default:
  @echo $(hostname)
Kron
  • 1,968
  • 1
  • 15
  • 15