I am writing a makefile for my program (that is tested and works) The problem I can tell is that some how my '-std=c++11' is not running in the function. The code pops out a unrecognized error that I dont get when I simply run 'g++ -o exe -std=c++11 *.cpp'. Here is my make file.
HEADERS = card.h sortedLinkedList.h deck.h
OBJECTS = card.o sortedLinkedList.o deck.o main.o
exe: $(OBJECTS)
g++ -std=c++11 $^ -o $#
%.o: %.cpp $(HEADERS)
g++ -c -std=c++11 $< -o $#
clean:
rm -i *.o exe
The output of my makefile is
me#root:~/Documents/CS216/Lab8$ make
g++ -c -o card.o card.cpp
card.cpp: In member function ‘void Card::print()’:
card.cpp:55:28: error: ‘to_string’ was not declared in this scope
cardN=to_string(point);
^
<builtin>: recipe for target 'card.o' failed
make: *** [card.o] Error 1
note: to_string() is not defined until c++11
daniel#Reimann:~/Documents/CS216/Lab8$ ls
card.cpp deck.cpp Lab8 lab8source.zip SortedLinkedList.h
card.h deck.h lab8.cpp makefile SortedLinkedList.o
card.o deck.o lab8.o SortedLinkedList.cpp
Output of xxd makefile
00000000: 4845 4144 4552 5320 3d20 6361 7264 2e68 HEADERS = card.h
00000010: 2073 6f72 7465 644c 696e 6b65 644c 6973 sortedLinkedLis
00000020: 742e 6820 6465 636b 2e68 0a4f 424a 4543 t.h deck.h.OBJEC
00000030: 5453 203d 2063 6172 642e 6f20 536f 7274 TS = card.o Sort
00000040: 6564 4c69 6e6b 6564 4c69 7374 2e6f 2064 edLinkedList.o d
00000050: 6563 6b2e 6f20 6c61 6238 2e6f 0a4c 6162 eck.o lab8.o.Lab
00000060: 383a 2024 284f 424a 4543 5453 290a 0967 8: $(OBJECTS)..g
00000070: 2b2b 202d 7374 643d 632b 2b31 3120 245e ++ -std=c++11 $^
00000080: 202d 6f20 2440 0a25 2e6f 3a20 252e 6370 -o $#.%.o: %.cp
00000090: 7020 2428 4845 4144 4552 5329 200a 0967 p $(HEADERS) ..g
000000a0: 2b2b 202d 6320 2d73 7464 3d63 2b2b 3131 ++ -c -std=c++11
000000b0: 2024 3c20 2d6f 2024 400a 636c 6561 6e3a $< -o $#.clean:
000000c0: 0a09 726d 202d 6920 2a2e 6f20 4c61 6238 ..rm -i *.o Lab8
000000d0: 0a
I believe the problem is in this line:
HEADERS = card.h sortedLinkedList.h deck.h
#----------------^
You have specified a dependency on sortedLinkedList.h, but that file doesn't exist. You only have SortedLinkedList.h with a capital S.
The rule
%.o: %.cpp $(HEADERS)
g++ -c -std=c++11 $< -o $#
is considered for making card.o but rejected because one of the prerequisites (namely sortedLinkedList.h) doesn't exist.
So make falls back to its built-in rule for making .o files from .cpp files, which is $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c.
CXX is set to g++ by default, but CPPFLAGS and CXXFLAGS are empty. This explains the extra spaces between g++ and -c in the output:
g++ -c -o card.o card.cpp
You can confirm this by running make -r, which disables all built-in rules. It should now fail to find a rule for making card.o.
To solve this, you should fix the capitalization of SortedLinkedList.h. You should also consider using the built-in rules and just set CXXFLAGS = -std=c++11.
Generally you want to use the CXXFLAGS implicit variable to specify this switch. I am not sure why the current pattern rule is failing, but using CXXFLAGS will allow getting rid of it entirely
Related
I have a problem with a Makefile. Maybe the problem is the linker, but I can't
find the error. At some point (CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o #
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:65: recipe for target 'doMyAnalysisHeavyIon' failed
make: *** [doMyAnalysisHeavyIon] Error 1
Makefile:
1 all: libs programs
2
3 libs: HxSimParticle_cxx.so HxSimEvent_cxx.so
4
5 programs = doMyAnalysisHeavyIon
6
7 #LHAPDF = LHAPDF-6.1.6
8 PYTHIA8 = /home/andre/pythia8235
9 HXPATH = /home/andre/pythia8235/examples/Analysis
10
11 # define compiler/linker flags
12 CXX = g++
13
14 CXXFLAGS = -O3 -Wall -fPIC
15 CXXFLAGS += $(shell root-config --cflags)
16 CXXFLAGS += $(shell fastjet-config --cxxflags)
17 CXXFLAGS += -I$(HXPATH)
18
19 #LDFLAGS = -O3
20 LDFLAGS = -O3 -Wl,-rpath,'$(HXPATH)'
21 LDFLAGS += $(shell root-config --ldflags)
22 #LDFLAGS += -Wl,-rpath,$(PYTHIA8)/lib/
23 #LDFLAGS += -Wl,-rpath,$(HXPATH)/
24
25 SOFLAGS = -shared
26
27 ROOTLIBS = $(shell root-config --libs --glibs --evelibs)
28 #ROOTLIBS += -lEG -lGui -lASImage -lASImageGui
29
30 #PDFLIBS = $(shell ${LHAPDF}/install/bin/lhapdf-config --libs)
31 #P8LIBS = $(shell $(PYTHIA8)/bin/pythia8-config --libs)
32
33 #HXLIBS = $(HXPATH)/HxSimParticle_cxx.so $(HXPATH)/HxSimEvent_cxx.so
34
35 PROGRAMS = doMyAnalysisHeavyIon
36
37 # compile/link HxSimEvent
38 HxSimEvent_cxx.so: HxSimEventDict.o HxSimEvent.o HxSimParticleDict.o HxSimParticle.o
39 $(CXX) $(SOFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $#
40
41 HxSimEvent.o: HxSimEvent.cxx
42 $(CXX) $(CXXFLAGS) -c $^ -o $#
43
44 HxSimEventDict.o: HxSimEventDict.cxx
45 $(CXX) $(CXXFLAGS) -c $^ -o $#
46
47 HxSimEventDict.cxx: HxSimEvent.h HxSimEventLinkDef.h
48 rootcint -f $# -c $(CXXFLAGS) -p $^
49
50 # compile/link HxSimParticle
51 HxSimParticle_cxx.so: HxSimParticleDict.o HxSimParticle.o
52 $(CXX) $(SOFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $#
53
54 HxSimParticle.o: HxSimParticle.cxx
55 $(CXX) $(CXXFLAGS) -c $^ -o $#
56
57 HxSimParticleDict.o: HxSimParticleDict.cxx
58 $(CXX) $(CXXFLAGS) -c $^ -o $#
59
60 HxSimParticleDict.cxx: HxSimParticle.h HxSimParticleLinkDef.h
61 rootcint -f $# -c $(CXXFLAGS) -p $^
62
63 # compile/link doMyAnalysisHeavyIon
64 doMyAnalysisHeavyIon: doMyAnalysisHeavyIon.o HxSimEvent_cxx.so HxSimParticle_cxx.so
65 $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o #
66
67 doMyAnalysisHeavyIon.o: doMyAnalysisHeavyIon.C
68 $(CXX) $(CXXFLAGS) -c $^ -o $#
69
70 .PHONY: clean distclean
71
72 # remove object files
73 clean:
74 #rm -f *.o
75
76 # remove objects, libraries and dicts
77 distclean: clean
78 #rm -f *.so *Dict.* $(PROGRAMS)
When building an executable linker (ld) is looking for main() to use it as a function to call / start your program with. Most likely causes would be:
You've forgotten to compile the source / link in the object where main is defined. Normally I would expect you to have it in doMyAnalysisHeavyIon.C with your Makefile content. It is however listed as prerequisite to be compiled and linked into doMyAnalysisHeavyIon.
Or there is a typo (perhaps capitalization?) causing main to not be found there? (Assuming it really isn't missing altogether? Should the result be an executable or perhaps a library?)
Line 65 is missing a dollar sign:
$(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $#
I'm not sure if that is your problem. Please show the contents of doMyAnalysisHeavyIon.C, especially whether or not it contains main and if main is the correct namespace. It must be at the top level of the namespace, and defined as int main(int argc, char* argv[]) {
I'm in Ubuntu 16.04.3 LTS xenial and I'm trying to run the command make on the terminal, but I'm getting this error:
g++ -g -O2 -Wall -Wuninitialized -fno-strict-aliasing -Iinclude -
I/usr/local/include -DOS_LINUX -DHAVE_USB -DHAVE_LIBUSB10 -
DUSE_DRS_MUTEX -I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-
3.1-unofficial3 -I/usr/include/wx-3.1-unofficial -
D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread -c src/DRS.cpp
In file included from /usr/include/c++/5/type_traits:35:0,
from /usr/include/wx-3.1-unofficial/wx/strvararg.h:22,
from /usr/include/wx-3.1-unofficial/wx/string.h:37,
from /usr/include/wx-3.1-unofficial/wx/memory.h:15,
from /usr/include/wx-3.1-unofficial/wx/object.h:19,
from /usr/include/wx-3.1-unofficial/wx/wx.h:15,
from src/DRS.cpp:15:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file
requires compiler and library support for the ISO C++ 2011 standard.
This support must be enabled with the -std=c++11 or -std=gnu++11 c
ompiler options.
#error This file requires compiler and library support \
^
In file included from /usr/include/wx-3.1-unofficial/wx/string.h:37:0,
from /usr/include/wx-3.1-unofficial/wx/memory.h:15,
from /usr/include/wx-3.1-unofficial/wx/object.h:19,
from /usr/include/wx-3.1-unofficial/wx/wx.h:15,
from src/DRS.cpp:15:
/usr/include/wx-3.1-unofficial/wx/strvararg.h:345:18: error: ‘is_enum’
in namespace ‘std’ does not name a template type
typedef std::is_enum<T> is_enum;
^
/usr/include/wx-3.1-unofficial/wx/strvararg.h:349:54: error: ‘is_enum’
was not declared in this scope
enum { value =
wxFormatStringSpecifierNonPodType<is_enum::value>::value };
^
/usr/include/wx-3.1-unofficial/wx/strvararg.h:349:68: error: template
argument 1 is invalid
enum { value =
wxFormatStringSpecifierNonPodType<is_enum::value>::value };
^
src/DRS.cpp: In member function ‘void DRSBoard::InteractSpeed()’:
src/DRS.cpp:3986:25: warning: ignoring return value of ‘int
scanf(const char*, ...)’, declared with attribute warn_unused_result
[-Wunused-result]
scanf("%lf", &vds);
^
Makefile:81: recipe for target 'DRS.o' failed
make: *** [DRS.o] Error 1
I think that I must do this correction:
This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
I have never tried to do something like this before, so I don't know how to correct.
Follows the makefile:
# check if wxWidgets is installed
HAVE_WX = $(shell which wx-config)
ifeq ($(HAVE_WX),)
$(error Error: wxWidgets required to compile "drsosc")
endif
# check for OS
OS = $(shell uname)
ifeq ($(OS),Darwin)
DOS = OS_DARWIN
else
DOS = OS_LINUX
endif
CFLAGS = -g -O2 -Wall -Wuninitialized -fno-strict-aliasing -
Iinclude -I/usr/local/include -D$(DOS) -DHAVE_USB -DHAVE_LIBUSB10 -
DUSE_DRS_MUTEX
LIBS = -lpthread -lutil -lusb-1.0
ifeq ($(OS),Darwin)
CFLAGS += -stdlib=libstdc++
endif
# wxWidgets libs and flags
WXLIBS = $(shell wx-config --libs)
WXFLAGS = $(shell wx-config --cxxflags)
CPP_OBJ = DRS.o averager.o ConfigDialog.o DOFrame.o DOScreen.o
DRSOsc.o MeasureDialog.o Measurement.o Osci.o InfoDialog.o
DisplayDialog.o AboutDialog.o EPThread.o TriggerDialog.o rb.o
OBJECTS = musbstd.o mxml.o strlcpy.o
ifeq ($(OS),Darwin)
all: drsosc drscl drs_exam drs_exam_multi DRSOsc.app
else
all: drsosc drscl drs_exam drs_exam_multi
endif
DRSOsc.app: drsosc
-mkdir DRSOsc.app
-mkdir DRSOsc.app/Contents
-mkdir DRSOsc.app/Contents/MacOS
-mkdir DRSOsc.app/Contents/Resources
-mkdir DRSOsc.app/Contents/Resources/English.lproj
echo 'APPL????' > DRSOsc.app/Contents/PkgInfo
cp Info.plist DRSOsc.app/Contents/Info.plist
cp DRSOsc.icns DRSOsc.app/Contents/Resources
cp drsosc DRSOsc.app/Contents/MacOS/DRSOsc
drsosc: $(OBJECTS) $(CPP_OBJ) main.o
$(CXX) $(CFLAGS) $(OBJECTS) $(CPP_OBJ) main.o -o drsosc $(LIBS)
$(WXLIBS)
drscl: $(OBJECTS) DRS.o averager.o drscl.o
$(CXX) $(CFLAGS) $(OBJECTS) DRS.o averager.o drscl.o -o drscl
$(LIBS) $(WXLIBS)
drs_exam: $(OBJECTS) DRS.o averager.o drs_exam.o
$(CXX) $(CFLAGS) $(OBJECTS) DRS.o averager.o drs_exam.o -o drs_exam
$(LIBS) $(WXLIBS)
drs_exam_multi: $(OBJECTS) DRS.o averager.o drs_exam_multi.o
$(CXX) $(CFLAGS) $(OBJECTS) DRS.o averager.o drs_exam_multi.o -o
drs_exam_multi $(LIBS) $(WXLIBS)
main.o: src/main.cpp include/mxml.h include/DRS.h
$(CXX) $(CFLAGS) $(WXFLAGS) -c $<
drscl.o: src/drscl.cpp include/mxml.h include/DRS.h
$(CXX) $(CFLAGS) -c $<
drs_exam.o: src/drs_exam.cpp include/mxml.h include/DRS.h
$(CXX) $(CFLAGS) -c $<
drs_exam_multi.o: src/drs_exam_multi.cpp include/mxml.h include/DRS.h
$(CXX) $(CFLAGS) -c $<
$(CPP_OBJ): %.o: src/%.cpp include/%.h include/mxml.h include/DRS.h
$(CXX) $(CFLAGS) $(WXFLAGS) -c $<
$(OBJECTS): %.o: src/%.c include/mxml.h include/DRS.h
$(CC) $(CFLAGS) -c $<
clean:
rm -f *.o drscl drsosc
In my research I didn't find anything helpful.
Sorry for the long post, couldn't make it more simple.
You simply need to set the C++ standard by adding it to the CFLAGS, ie:
CFLAGS += -std=c++11
I have the same problem you have and I was looking for an answer but can't find one so I solved it on my own. In my case, the problem is with the string.h header file I removed it and my code is working now.
When i run the program i get the following error
fatal error: 'Gl/glx.h' file not found
I am attempting to run a c++ xll graphics program on my mac. I have already downloaded Xquartz and used it to run multiple graphics programs on my computer and now i am trying to figure out what i'm missing in order to get this program to run. if anybody can point me in the right direction it would be wonderful!
Thanks
here is a copy of the make file provided
1 ## If you receive XRR errors, include -lXrandr option on LFLAGS line
2 all: atrace
3 ##LIB = ./libggfonts.so
4 LIB = ./libggfonts.a
5 LFLAGS = -L -lX11 -lGLU -lGL -lm #-lXrandr
6 CFLAGS = -O2 -Wall -Wextra -I
7 SOURCE = atrace.cpp init.cpp scene.cpp render.cpp sphere.cpp ppm.cpp log.cpp \
8 vector.cpp input.cpp files.cpp perlin.cpp texture.cpp divide.cpp \
9 cubemap.cpp matrix.cpp fresnel.cpp haze.cpp animate.cpp \
10 photons.cpp subsurf.cpp
11
12 #all: atrace atrace.o init.o scene.o render.o sphere.o ppm.o log.o \
13 # vector.o input.o files.o perlin.o texture.o divide.o \
14 # cubemap.o matrix.o fresnel.o haze.o animate.o
15 #
16 #atrace: $(SOURCE) fonts.h defs.h ppm.h extern.h bvh.h animate.h $(LIB)
17 # g++ $(CFLAGS) $(SOURCE) $(LFLAGS) $(LIB) -o atrace
18
19 atrace: atrace.o init.o scene.o render.o sphere.o ppm.o log.o \
20 vector.o input.o files.o perlin.o texture.o divide.o \
21 cubemap.o matrix.o fresnel.o haze.o animate.o photons.o subsurf.o \
22 defs.h ppm.h extern.h bvh.h animate.h
23 g++ atrace.o init.o scene.o render.o sphere.o ppm.o log.o vector.o \
24 input.o files.o perlin.o texture.o divide.o cubemap.o matrix.o fresnel.o \
25 haze.o animate.o photons.o subsurf.o $(LIB) -oatrace $(LFLAGS)
26
27
28 atrace.o: atrace.cpp defs.h ppm.h extern.h bvh.h animate.h
29 g++ atrace.cpp -c $(CFLAGS) $(LFLAGS)
30 init.o: init.cpp defs.h
31 g++ init.cpp -c $(CFLAGS) $(LFLAGS)
32 scene.o: scene.cpp defs.h
33 g++ scene.cpp -c $(CFLAGS) $(LFLAGS)
34 render.o: render.cpp defs.h
35 g++ render.cpp -c $(CFLAGS) $(LFLAGS)
36 sphere.o: sphere.cpp defs.h
37 g++ sphere.cpp -c $(CFLAGS) $(LFLAGS)
38 ppm.o: ppm.cpp defs.h ppm.h
39 g++ ppm.cpp -c $(CFLAGS) $(LFLAGS)
40 log.o: log.cpp defs.h
41 g++ log.cpp -c $(CFLAGS) $(LFLAGS)
42 vector.o: vector.cpp defs.h
43 g++ vector.cpp -c $(CFLAGS) $(LFLAGS)
44 input.o: input.cpp defs.h
45 g++ input.cpp -c $(CFLAGS) $(LFLAGS)
46 files.o: files.cpp defs.h files.h
47 g++ files.cpp -c $(CFLAGS) $(LFLAGS)
48 perlin.o: perlin.cpp defs.h
49 g++ perlin.cpp -c $(CFLAGS) $(LFLAGS)
50 texture.o: texture.cpp defs.h
51 g++ texture.cpp -c $(CFLAGS) $(LFLAGS)
52 divide.o: divide.cpp defs.h bvh.h
53 g++ divide.cpp -c $(CFLAGS) $(LFLAGS)
54 cubemap.o: cubemap.cpp defs.h
55 g++ cubemap.cpp -c $(CFLAGS) $(LFLAGS)
56 matrix.o: matrix.cpp defs.h
57 g++ matrix.cpp -c $(CFLAGS) $(LFLAGS)
58 fresnel.o: fresnel.cpp defs.h
59 g++ fresnel.cpp -c $(CFLAGS) $(LFLAGS)
60 haze.o: haze.cpp defs.h
61 g++ haze.cpp -c $(CFLAGS) $(LFLAGS)
62 animate.o: animate.cpp defs.h animate.h ppm.h
63 g++ animate.cpp -c $(CFLAGS) $(LFLAGS)
64 photons.o: photons.cpp defs.h
65 g++ photons.cpp -c $(CFLAGS) $(LFLAGS)
66 subsurf.o: subsurf.cpp defs.h
67 g++ subsurf.cpp -c $(CFLAGS) $(LFLAGS)
68
69 clean:
70 rm -f atrace
71 rm -f *.o
I am trying to create a system of makefiles to build my whole project. I am using cygwin and gcc compiler. I am running into a linking error on shared libraries that I cannot figure out.
I am concerned with ld: cannot find -lbase and ld: cannot find -lsimsimA429.
These files were clearly built. I even did a directory listing as part of the build command to show that they exist.
-rwxr-xr-x+ 1 user group 116832 Mar 25 10:09 /cygdrive/d/myProj/lib/libbase.so
-rwxr-xr-x+ 1 user group 75972 Mar 25 10:09 /cygdrive/d/myProj/lib/libsimsima429.so
and the link command also shows that I am including the correct link directory
g++ -shared -L/cygdrive/d/myProj/lib -lpthread -lrt -lbase -lsimsimA429 ...
What am I doing wrong to incur this error?
Also, I am relatively new to writing makefiles. If you have any comments about how I could improve them, I would kindly appreciate a few comments.
Full makefiles and output are listed below...
/cygdrive/d/myProj/src/sfi/base/makefile
#------------------------------------------------------------------------------
#-- BASE
#------------------------------------------------------------------------------
#--
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
MKDIR=mkdir
ROOTDIR=$(realpath ../../..)
PROJDIR=$(notdir $(CURDIR))
LIBDIR=$(ROOTDIR)/lib
OBJDIR=$(ROOTDIR)/obj/$(PROJDIR)
SRCDIR=$(ROOTDIR)/src
#OUTPUT FILE lib file or executable
TARGET=$(LIBDIR)/lib$(PROJDIR).so
INCLUDES=
#INCLUDES+=
LIBS=
#LIBS+=
LDFLAGS=
LDFLAGS+= -shared
LDFLAGS+= -L$(LIBDIR)
#LDFLAGS+= -Wl,-rpath
CCFLAGS=
#CCFLAGS+= -fPIC
#CCFLAGS+= -ansi
#CCFLAGS+= -pedantic
CCFLAGS+= -g
CCFLAGS+= -Wall
#CCFLAGS+= -std=c++11
CCC=g++
SRC= $(call rwildcard,./,*.cpp)
OBJECTS=$(patsubst %.cpp,$(OBJDIR)/%.o,$(SRC))
# Set our own Goal.
.DEFAULT_GOAL := all
.PHONY: all
all: $(TARGET)
$(OBJDIR)/%.o: %.cpp
#$(MKDIR) -p $(abspath $(dir $#))
$(CCC) -c $(CCFLAGS) $(INCLUDES) $< -o $#
$(TARGET): $(OBJECTS)
$(CCC) $(LDFLAGS) $(LIBS) -o $(TARGET) $(OBJECTS)
ls -l $(TARGET)
.PHONY: clean
clean:
rm -f $(OBJECTS) $(TARGET)
test:
#echo $(OBJECTS) | tr " " "\n"
/cygdrive/d/myProj/src/sfi/drivers/a429/simsimA429/makefile
#------------------------------------------------------------------------------
#-- SIMSIMA429
#------------------------------------------------------------------------------
#--
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
MKDIR=mkdir
ROOTDIR=$(realpath ../../../../..)
PROJDIR=$(notdir $(CURDIR))
LIBDIR=$(ROOTDIR)/lib
OBJDIR=$(ROOTDIR)/obj/$(PROJDIR)
SRCDIR=$(ROOTDIR)/src
#OUTPUT FILE lib file or executable
TARGET=$(LIBDIR)/lib$(PROJDIR).so
INCLUDES=
INCLUDES+= -I$(SRCDIR)/face
INCLUDES+= -I$(SRCDIR)/sfi/base
INCLUDES+= -I$(SRCDIR)/sfi/drivers/a429/include
LIBS=
LIBS+= -L$(LIBDIR)
#LIBS+= -lbase
LDFLAGS=
LDFLAGS+= -shared
#LDFLAGS+= -Wl,-rpath
CCFLAGS=
#CCFLAGS+= -fPIC
#CCFLAGS+= -ansi
#CCFLAGS+= -pedantic
CCFLAGS+= -g
CCFLAGS+= -Wall
#CCFLAGS+= -std=c++11
CCC=g++
SRC= $(call rwildcard,./,*.cpp)
OBJECTS=$(patsubst %.cpp,$(OBJDIR)/%.o,$(SRC))
# Set our own Goal.
.DEFAULT_GOAL := all
.PHONY: all
all: $(TARGET)
$(OBJDIR)/%.o: %.cpp
#$(MKDIR) -p $(abspath $(dir $#))
$(CCC) -c $(CCFLAGS) $(INCLUDES) $< -o $#
$(TARGET): $(OBJECTS)
$(CCC) $(LDFLAGS) $(LIBS) -o $(TARGET) $(OBJECTS)
ls -l $(TARGET)
.PHONY: clean
clean:
rm -f $(OBJECTS) $(TARGET)
test:
#echo $(OBJECTS) | tr " " "\n"
/cygdrive/d/myProj/src/sfi/ioss/makefile
#------------------------------------------------------------------------------
#-- IOSS
#------------------------------------------------------------------------------
#--
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
MKDIR=mkdir
ROOTDIR=$(realpath ../../..)
PROJDIR=$(notdir $(CURDIR))
LIBDIR=$(ROOTDIR)/lib
OBJDIR=$(ROOTDIR)/obj/$(PROJDIR)
SRCDIR=$(ROOTDIR)/src
#OUTPUT FILE lib file or executable
TARGET=$(LIBDIR)/lib$(PROJDIR).so
INCLUDES=
INCLUDES+= -I$(SRCDIR)/face
INCLUDES+= -I$(SRCDIR)/sfi
INCLUDES+= -I$(SRCDIR)/sfi/base
INCLUDES+= -I$(SRCDIR)/sfi/drivers/a429/include
INCLUDES+= -I$(SRCDIR)/sfi/ioss
LIBS=
LIBS+= -L$(LIBDIR)
LIBS+= -lpthread
LIBS+= -lrt
LIBS+= -lbase
LIBS+= -lsimsima429
LDFLAGS=
LDFLAGS+= -shared
#LDFLAGS+= -Wl,-rpath
CCFLAGS=
#CCFLAGS+= -fPIC
#CCFLAGS+= -ansi
#CCFLAGS+= -pedantic
CCFLAGS+= -g
CCFLAGS+= -Wall
#CCFLAGS+= -std=c++11
CCC=g++
SRC= $(call rwildcard,./,*.cpp)
OBJECTS=$(patsubst %.cpp,$(OBJDIR)/%.o,$(SRC))
# Set our own Goal.
.DEFAULT_GOAL := all
.PHONY: all
all: $(TARGET)
$(OBJDIR)/%.o: %.cpp
#$(MKDIR) -p $(abspath $(dir $#))
$(CCC) -c $(CCFLAGS) $(INCLUDES) $< -o $#
$(TARGET): $(OBJECTS)
$(CCC) $(LDFLAGS) $(LIBS) -o $(TARGET) $(OBJECTS)
ls -l $(TARGET)
.PHONY: clean
clean:
rm -f $(OBJECTS) $(TARGET)
test:
#echo $(OBJECTS) | tr " " "\n"
Full Output:
10:09:19 **** Build of configuration Default for project base ****
make -j all
g++ -c -g -Wall udpserver.cpp -o /cygdrive/d/myProj/obj/base/./udpserver.o
g++ -c -g -Wall udpclient.cpp -o /cygdrive/d/myProj/obj/base/./udpclient.o
g++ -c -g -Wall impl/clock.cpp -o /cygdrive/d/myProj/obj/base/./impl/clock.o
g++ -c -g -Wall impl/deque.cpp -o /cygdrive/d/myProj/obj/base/./impl/deque.o
g++ -c -g -Wall impl/thread.cpp -o /cygdrive/d/myProj/obj/base/./impl/thread.o
g++ -shared -L/cygdrive/d/myProj/lib -o /cygdrive/d/myProj/lib/libbase.so /cygdrive/d/myProj/obj/base/./udpserver.o /cygdrive/d/myProj/obj/base/./udpclient.o /cygdrive/d/myProj/obj/base/./impl/clock.o /cygdrive/d/myProj/obj/base/./impl/Stdafx.o /cygdrive/d/myProj/obj/base/./impl/deque.o /cygdrive/d/myProj/obj/base/./impl/thread.o
ls -l /cygdrive/d/myProj/lib/libbase.so
-rwxr-xr-x+ 1 user group 116832 Mar 25 10:09 /cygdrive/d/myProj/lib/libbase.so
10:09:25 Build Finished (took 5s.994ms)
10:09:25 **** Build of configuration Default for project simsimA429 ****
make all
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include A429HwCtrlTx.cpp -o /cygdrive/d/myProj/obj/simsima429/./A429HwCtrlTx.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include A429HwCtrlRx.cpp -o /cygdrive/d/myProj/obj/simsima429/./A429HwCtrlRx.o
g++ -shared -L/cygdrive/d/myProj/lib -o /cygdrive/d/myProj/lib/libsimsima429.so /cygdrive/d/myProj/obj/simsima429/./A429HwCtrlTx.o /cygdrive/d/myProj/obj/simsima429/./A429HwCtrlRx.o
ls -l /cygdrive/d/myProj/lib/libsimsima429.so
-rwxr-xr-x+ 1 user group 75972 Mar 25 10:09 /cygdrive/d/myProj/lib/libsimsima429.so
10:09:29 Build Finished (took 4s.339ms)
10:09:29 **** Build of configuration Default for project ioss ****
make -j all
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss IOS.cpp -o /cygdrive/d/myProj/obj/ioss/./IOS.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss message/IOSmsg.cpp -o /cygdrive/d/myProj/obj/ioss/./message/IOSmsg.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss message/MsgId.cpp -o /cygdrive/d/myProj/obj/ioss/./message/MsgId.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss message/IOSmsgA429.cpp -o /cygdrive/d/myProj/obj/ioss/./message/IOSmsgA429.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss interface/IOSInterfaceHandle.cpp -o /cygdrive/d/myProj/obj/ioss/./interface/IOSInterfaceHandle.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss interface/IOSInterface.cpp -o /cygdrive/d/myProj/obj/ioss/./interface/IOSInterface.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss interface/direct/IOSDirect_Interface.cpp -o /cygdrive/d/myProj/obj/ioss/./interface/direct/IOSDirect_Interface.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429MajorFrame.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MajorFrame.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429MsgRxControl.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgRxControl.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429BusTx.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429BusTx.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429MsgRegistry.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgRegistry.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429BusRx.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429BusRx.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429Message.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429Message.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/a429/A429MsgBuffer.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgBuffer.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss bus/m1553/M1553Connection.cpp -o /cygdrive/d/myProj/obj/ioss/./bus/m1553/M1553Connection.o
g++ -c -g -Wall -I/cygdrive/d/myProj/src/face -I/cygdrive/d/myProj/src/sfi -I/cygdrive/d/myProj/src/sfi/base -I/cygdrive/d/myProj/src/sfi/drivers/a429/include -I/cygdrive/d/myProj/src/sfi/ioss config/IOSConfig.cpp -o /cygdrive/d/myProj/obj/ioss/./config/IOSConfig.o
g++ -shared -L/cygdrive/d/myProj/lib -lpthread -lrt -lbase -lsimsima429 -o /cygdrive/d/myProj/lib/libioss.so /cygdrive/d/myProj/obj/ioss/./IOS.o /cygdrive/d/myProj/obj/ioss/./message/IOSmsg.o /cygdrive/d/myProj/obj/ioss/./message/MsgId.o /cygdrive/d/myProj/obj/ioss/./message/IOSmsgA429.o /cygdrive/d/myProj/obj/ioss/./interface/IOSInterfaceHandle.o /cygdrive/d/myProj/obj/ioss/./interface/IOSInterface.o /cygdrive/d/myProj/obj/ioss/./interface/direct/IOSDirect_Interface.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MajorFrame.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgRxControl.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429BusTx.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgRegistry.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429BusRx.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429Message.o /cygdrive/d/myProj/obj/ioss/./bus/a429/A429MsgBuffer.o /cygdrive/d/myProj/obj/ioss/./bus/m1553/M1553Connection.o /cygdrive/d/myProj/obj/ioss/./config/IOSConfig.o
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lbase
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lsimsima429
collect2: error: ld returned 1 exit status
makefile:61: recipe for target '/cygdrive/d/myProj/lib/libioss.so' failed
make: *** [/cygdrive/d/myProj/lib/libioss.so] Error 1
10:09:42 Build Finished (took 13s.205ms)
Cygwin shared libraries are *.dll files, not lib*.so files. I suppose your linker is struggling to find base.dll and simsimA429.dll.
How do I use different compiler flags for different source files in a Makefile? For example, I'd like a Makefile that will produce:
g++ -c -COMPILER_FLAGS_1 -g source1.cpp -o source1.o
g++ -c -COMPILER_FLAGS_2 -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2 -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2 -g source4.cpp -o source4.o
g++ -c -COMPILER_FLAGS_3 -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3 -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3 -g source7.cpp -o source7.o
g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o
At the moment I've got about 20 source files (and that's expected to grow), so an easy to maintain file would be nice.
Thanks in advance.
You could do something like the following (untested, so the syntax might be slightly off):
OBJS_1 := source1.o
OBJS_2 := source2.o source3.o source4.o
OBJS_3 := source5.o source6.o source7.o
OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)
output: $(OBJS)
$(CXX) -g -o $# $^
$(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
$(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
$(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)
$(OBJS): %.o: %.cpp
$(CXX) -c $(CXXFLAGS) -g $< -o $#
Here is UNIX/LINUX makefile which I wrote and tested on Solaris LINUX to handle different compilation flags for different sections of a GNUmakefile. Please let me know if it can be improved. Thank you
# GNUmakefile
#
# makefile for mdRightFielder
#
# Builds:
# libmdRightFielder.so or libmdRightFielder.sl
ifndef SUB
include ../header.mk
else
VPATH=../Source:../Source/PCRE:../Source/SQLite:../../cpswindows/Source:../../util/mdLicense
INCL=-I../Include -I../Include/PCRE -I../Include/SQLite -I../../cpswindows/Include -I ../../util -I../../util/mdLicense
APIOBJ=cGlobalDataDestructor.o cPCRE.o CppInterface.o cRightFielder-FillTokenGaps.o
PCREOBJ=pcre_chartables.o pcre_compile.o pcre_exec.o pcre_fullinfo.o pcre_get.o pcre_globals.o pcre_newline.o \
pcre_tables.o pcre_try_flipped.o
SQLITEOBJ=sqlite3.o
CPSWINDOWSOBJ=BinarySearch.o cConfigFile.o cCriticalSection.o cDateTime.o cException.o cFile.o cSQLite.o \
QuickSort.o StringFunctions.o
MDLICENSEOBJ=CBigNum.o mdLicense.o RSA.o
ifeq ($(CPU),sparc)
ifdef workshop
CALIGN=-xmemalign=1s
ifdef release
CXXALIGN=-Qoption cg -xmemalign=1s
else
CXXALIGN=-Qoption ccfe -y-xmemalign=1s
endif
endif
endif
COMPILER_FLAGS_1=-D_NO_GUI
COMPILER_FLAGS_2=-D_NO_GUI -DHAVE_CONFIG_H
CXXFLAGS+=-DPCRE_STATIC -DUSE_STATIC
CFLAGS+=-D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
DEPFLAGS+=-DLINK_SIZE=2 -D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
.PHONY: all clean
all: libmdRightFielder.so
cp -fp ../Include/mdRightFielder.h ../../util/mdEnums.h libmdRightFielder.so $(SHIP)
if [ `uname` = HP-UX ] ; \
then \
/bin/mv -f $(SHIP)/libmdRightFielder.so $(SHIP)/libmdRightFielder.sl ; \
fi
clean:
rm -f *.o *.so *.sl deps
rm -f core core.[0-9]*
$(APIOBJ): CXXFLAGS+=$(COMPILER_FLAGS_1)
$(PCREOBJ): CXXFLAGS+=$(COMPILER_FLAGS_2)
MARYOBJS = $(APIOBJ) $(PCREOBJ)
libmdRightFielder.so: \
$(MARYOBJS)
-$(CXX) $(CXXFLAGS) $(INCL) $(SHARED) $^ -o $# $(LDLIBS)
mary:
%.o : %.cpp # cancel implicit CPP compilation rule
%.o : %.cpp
$(CXX) $(CXXFLAGS) $(INCL) $(PIC) $< -o $# -c
%.o : %.c # cancel implicit C compilation rule
%.o : %.c
$(CC) $(CFLAGS) $(INCL) $(PIC) $< -o $# -c
endif