neversaint
neversaint

Reputation: 64054

Howto incorporate -I in makefile

I have no problem compiling specific code the following way:

g++ -I /opt/local/include Code1.cc -o Code1

However when I tried to do that in the makefile:

CXX = g++ -Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1


Code3: Code3.cc Tools.cc
    $(CXX)   $^ -o $@

Code2: Code2.cc Tools.cc
    $(CXX)   $^ -o $@

Code1: Code1.cc Tools.cc
    $(CXX) -I /opt/local/include $^ -o $@

It complains. What's the correct way to do it? Note that only Code1.cc require the external library as include.

The header of Code1.cc looks like this:

#include <iostream>      
#include <vector>        
#include <fstream>       
#include <sstream>       
#include <iomanip>       
#include <boost/math/distributions/chi_squared.hpp>
using namespace std;     
using boost::math::chi_squared; 
using boost::math::quantile;

The error I get is as follows:

g++ -Wall -Werror -gstabs -pedantic -O2 -g -I/opt/local/include Code1.cc Tools.cc -o EstimateErrorMean
In file included from /opt/local/include/boost/detail/lcast_precision.hpp:16,
                 from /opt/local/include/boost/lexical_cast.hpp:31,
                 from /opt/local/include/boost/math/special_functions/gamma.hpp:23,
                 from /opt/local/include/boost/math/distributions/chi_squared.hpp:13,
                 from EstimateErrorMean.cc:19:
/opt/local/include/boost/integer_traits.hpp:164:66: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:164:77: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:170:70: error: use of C99 long long integer constant

Upvotes: 1

Views: 1888

Answers (5)

Gayan
Gayan

Reputation: 1737

INCLUDE_PATH = -I/opt/local/include
LINK_PATH = -L <Library paths>
LIBS = -l<libraryname>

OBJECTS = Code1.o\
          Code2.o\
          Code3.o\

CXXFLAGS += $(INCLUDE_PATH)

Code1 : $(OBJECTS)
          $(CXX) $(LINK_PATH) -o$@ $(OBJECTS) $(LIBS)

This MIGHT work...

Upvotes: 1

rkb
rkb

Reputation: 3542

-pedantic causes all required warnings to be reported, and -Werror causes warnings to be reported as errors. As C++ does not define the "ULL" long long integer constant syntax (C99 does), this is probably being reported and then promoted to full-on error status by g++.

Try removing -pedantic.

Or try #including <boost/cstdint.hpp>.

Upvotes: 4

Eric Melski
Eric Melski

Reputation: 16820

In a GNU Make makefile, the convention is to use CXXFLAGS for C++ compiler flags, and to make an addition to the flags for a specific target, you can use target-specific variables. For example:

CXX=g++

# Set CXXFLAGS to include the base set of flags

CXXFLAGS=-Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1

Code3: Code3.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

Code2: Code2.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

# Add a target-specific addition to CXXFLAGS for Code1:

Code1: CXXFLAGS += -I/opt/local/include

Code1: Code1.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

Note that you may also want to switch to using pattern rules, rather than explicitly declaring rules for all your (very similar) targets. For example, you could replace the Code1, Code2 and Code3 rules with just this:

%: %.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

EDIT: In response to the updated post regarding the specific error seen: it looks like you are probably getting burned because you include -Wall -Werror in the flags when you are using the Makefile, but not on the command line. -Wall causes g++ to be a lot more picky about syntax; -Werror causes g++ to promote things that are normally just warnings into full-blown errors. Since the errors are being reported against code in a third-party library, perhaps you can get by with less strict warning options (ie, remove -Wall); or perhaps you need to switch to a newer version of the compiler that can handle the constructs without complaining; or perhaps you just need to explicitly specify -std=c99 to alert the compiler that you want C99 support.

Hope that helps,

Eric Melski

Upvotes: 5

If you're using GNU make you might consider a structure like:

CXXFLAGS += -Wall -Werror -gstabs -pedantic -O2 -g
CXXFLGAS += -I/opt/local/include

SRCS:=Code1.cc Code2.cc Code3.cc
OBJS:=$(subst .cc,.o,$(SRCS))
EXES:=$(subst .o,,$(OBJS))

all: $(EXES)

where you count on the built in rule to manage compiling from .cc files to .o files to the executables.

Note that you may also have to define the linker flags

LDFLAGS:=-g

and the like...

Upvotes: 1

Dan Breslau
Dan Breslau

Reputation: 11522

It would be helpful to know what the complaint is -- but try taking out the space between the -I and /opt/local/include :

 -I/opt/local/include $^ -o $@

Upvotes: 1

Related Questions