1
votes

I was learning GNU autotools (autoconf, automake) with this helloworld example. My project tree is like this:

|-- aclocal.m4
    |-- AUTHORS
    |-- autom4te.cache
    |   |-- output.0
    |   |-- output.1
    |   |-- requests
    |   |-- traces.0
    |   `-- traces.1
    |-- autoscan.log
    |-- ChangeLog
    |-- config.h.in
    |-- configure
    |-- configure.ac
    |-- COPYING
    |-- depcomp
    |-- include
    |   |-- hello.hpp
    |   `-- world.hpp
    |-- INSTALL
    |-- install-sh
    |-- lib
    |   |-- hello.cpp
    |   |-- Makefile.am
    |   |-- Makefile.in
    |   `-- world.cpp
    |-- Makefile.am
    |-- Makefile.in
    |-- missing
    |-- NEWS
    |-- README
    `-- src
        |-- main.cpp
        |-- Makefile.am
        `-- Makefile.in

I build like this:

$autoreconf -vfi 
$./configure
$make 

... and get a compilation failure:

Making all in src
make[2]: Entering directory `/home/suddin/package_directory/src'
g++ -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
main.cpp:7:21: fatal error: hello.hpp: No such file or directory
 #include "hello.hpp"
                     ^

I was able to build successfully by changing my #include directives to these:

#include "../include/hello.hpp"
#include "../include/world.hpp"

, but I would prefer to keep them as they are:

#include "hello.hpp"
#include "world.hpp"

My src/Makefile.am file contains the following line; why does it not resolve the issue?

helloWorld_CXXFLAGS=-I../include      ##Add path to header file

For context, here are all my Makefile.am files:

===== src/Makefile.am ======================

bin_PROGRAMS=helloworld
helloworld_SOURCES=main.cpp
helloworld_CXXFLAGS= -I../include ## add path to headerfiles
helloworld_LDADD=../lib/libhw.a ## link with static library

===== lib/Makefile.am =======================

noinst_LIBRARIES=libhw.a ## static library which is not to be installed
libhw_a_SOURCES=hello.cpp hello.hpp world.cpp world.hpp
libhw_a_CXXFLAGS=-I../include ## add path to headerfiles

===== Makefile.am (top level) =================

 SUBDIRS=lib src ## processing subdirs in given order
2
I observe that the compilation command that make issues for file main.cpp does not include the needed -I directive. This is the proximal cause of the compilation error. I also note that your question has inconsistent name capitalization: the text says you've set a make variable named helloWorld_CXXFLAGS (with a capital 'W'); if indeed you have done so then that is irrelevant to building a target named helloworld (with lowercase 'w'). Your src/Makefile.am uses the appropriate capitalization, however.John Bollinger
I don't see anything wrong with the three Makefile.am files you have presented, and I was unable to use them as a model for reproducing the error. I am therefore inclined to suspect that the actual situation is different in some way than you have presented. Perhaps one or more variable names really are wrong, or perhaps you did not really re-run autoreconf since the last time you modified your Makefile.am files (and you have maintainer mode disabled).John Bollinger
first of all helloWorld_CXXFLAGS was my typing mistake .'w' should be small 'w', but how to include header using -I directive , those header file are kept in " include" directory ?SK17
As I said, I was unable to reproduce your problem. What you presented should work, and a slightly simplified version (still referencing a header from the include/ directory) did work for me. Whatever the problem is, you do not seem to have captured it in the question.John Bollinger
sorry you're right, I recheck my code now , it was just a typing mistake, "helloWorld_CXXFLAGS " should be "helloworld_CXXFLAGS" , and all compiled successfully , thanks again for the help.SK17

2 Answers

3
votes

Use

helloworld_CPPFLAGS = -I$(top_srcdir)/include

to compile all object files for foo adding the include dir from the source tree.

0
votes

Typing mistake, "helloWorld_CXXFLAGS " should be "helloworld_CXXFLAGS".