Search code examples
cmakefilelinkerflagsautomake

Beginner in writing makefile; checking my Makefile


I am trying to understand how to write my own makefile for small project, so I have a few questions my project consists of src directory that contains main.c, file1.c, file2.c, header1.h, and finally header2.h these files use some library from non standard library that I have created and non standard header file, the library directory is located in usr/lib/pr__lib and the header directory is located in usr/include/lib
so I should create two makefile.am one will be located in src directory and the other one will be in root directory of the project the makefile.am of the src directory is as shown below:

 program_NAME := PRDSL

 bin_PROGRAMS = PRDSL_AutoProject

 program_INCLUDE_DIRS := /usr/bin/PR__bin

 program_LIBRARY_DIRS := /usr/lib/PR__lib

 CFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))

 program_LIBRARIES := \
         libprdependency \
         libprdynarray_pic \
         libprhistogram_pic \
         libprlistofarrays \
         libprlistofarrays_pic \
         libprmalloc \
         libvreo_wrapper_library 

 AM_LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))

 AM_LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))

 PRDSL_AutoProject_SOURCES = \
         main.c \
         file1.c \
         file2.c

 depend :
      makedepend --$(CFLAGS) --$(PRDSL_AutoProject_SOURCES)

 all: $(program_NAME)

2nd makefile.am at the root directory is as shown below:

  SUBDIRS = src

  PRDSL_AutoProjectdocdir = ${prefix}/doc/PRDSL_AutoProject
  PRDSL_AutoProjectdoc_DATA = \
              README\
              COPYING\
              AUTHORS\
              ChangeLog\
              INSTALL\
              NEWS


     INTLTOOL_FILES = intltool-extract.in \
             intltool-merge.in \
             intltool-update.in

     EXTRA_DIST = $(PRDSL_AutoProjectdoc_DATA) \
             $(INTLTOOL_FILES)

     DISTCLEANFILES = intltool-extract \
             intltool-merge \
             intltool-update \
             po/.intltool-merge-cache


     # Remove doc directory on uninstall
     uninstall-local:
            -rm -r $(PRDSL_AutoProjectdocdir)

but I get the below errors and warnings while I was running automake command:

  src/Makefile.am:20: error: 'program_LIBRARIES' is used but 'programdir' is undefined
  src/Makefile.am:18: warning: 'CFLAGS' is a user variable, you should not override it;
  src/Makefile.am:18: use 'AM_CFLAGS' instead
  src/Makefile.am:43: error: AM_LDFLAGS must be set with '=' before using '+='

could any one review it and help me?


Solution

  • For src/Makefile.am:43: error: AM_LDFLAGS must be set with '=' before using '+=' see my answer to your previous question.

    Yes, the following lines are telling you to use AM_CFLAGS instead of CFLAGS because, as the error indicates, CFLAGS is a user-specified value and you should leave it alone.

    src/Makefile.am:18: warning: 'CFLAGS' is a user variable, you should not override it;
    src/Makefile.am:18: use 'AM_CFLAGS' instead
    

    I believe, though I am far from certain, that the reason you are getting src/Makefile.am:20: error: 'program_LIBRARIES' is used but 'programdir' is undefined is because you are using a variable program_LIBRARIES that the autotools believe is a variable they should be using but that you are not using in that way and are instead using in the loop which sets the value for AM_LDFLAGS. As such, and assuming I am correct, if you rename that to not follow the autotool variable naming scheme I believe that error will go away.