# Compiler
CXX := g++
CF := -g
# c files
CFILES := $(wildcard src/*.cpp)
SOURCES := $(CFILES) # $(CFILES2) all CFILES
# o files
OBJECTS := $(SOURCES:.cpp=.o)
OBJECTS_A:= $(SOURCES:.cpp=_a.o)
# Macros
MACRO = -D
INFO = -DINFO // optional
DCDREAD = -DDCDREAD
# More Macros
DEFAULT = -DDEFAULT
CONTACT = -DDEFAULT -DCONTACT -DCONTACTPERSIST -DTENSION_COSTHETA
MTCONTACT = -DDEFAULT -DMTCON1 -DMTCON2
DCDCON = -DDCDREAD -DCONTACT -DCONTACTPERSIST -DTENSION_COSTHETA
DCDMT = -DDCDREAD -DMTCON1 -DMTCON2
CENMOV = -DDCDREAD -DMTCON1 -DCENTROIDMOVEMENT
ANGLE = -DDCDREAD -DINERTIA -DANGLE
ANG3 = -DDCDREAD -DMTCON1 -DANGLE3CENTROID
ang3o: $(OBJECTS_A)
$(CXX) $(OBJECTS_A) -o test/$(EXEC)_dcd_angle3centroid
cd test && ./$(EXEC)_dcd_angle3centroid mtonly_seamup.ref.pdb mtonly_seamup_d1_indent.dcd 1 209 1
ang3: $(OBJECTS)
$(CXX) $(SOURCES) $(CF) $(INC) $(LIB) $(ANG3) -o test/$(EXEC)_dcd_angle3centroid
cd test && ./$(EXEC)_dcd_angle3centroid mtonly_seamup.ref.pdb mtonly_seamup_d1_indent.dcd 1 209 1
# To obtain object files
$(OBJECTS_A) : $(SOURCES)
$(CXX) $(CF) $(INC) $(LIB) $(ANG3) -c $< -o $@
%.o: %.cpp
$(CXX) $(CC_FLAGS) $(INC) $(LIB) -c $< -o $@
I know the objects are unnecessary in the ANG3 rule. The c files are compiled directly, the executable runs correctly! I'd like to switch to objects, an ANG3O rule. HERE is my main question, am I trying to link .o files incorrectly at the end of the ANG3O rule?
ANG3 works. ANG3O fails. Multiple Definitions of Function Error.
make ang3
g++ -g -O3 -Iinclude -pthread -c src/readfile.cpp -o src/readfile.o
g++ -g -O3 -Iinclude -pthread -c src/main.cpp -o src/main.o
g++ -g -O3 -Iinclude -pthread -c src/md.cpp -o src/md.o
g++ -g -O3 -Iinclude -pthread -c src/chain.cpp -o src/chain.o
g++ src/readfile.cpp src/main.cpp src/md.cpp src/chain.cpp -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -o test/run_segment_dcd_angle3centroid
make ang3o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/readfile_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/main_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/md_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/chain_a.o
g++ src/readfile_a.o src/main_a.o src/md_a.o src/chain_a.o -o test/run_segment_dcd_angle3centroid
src/main_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/main_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
src/md_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/md_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
src/chain_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/chain_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:172: recipe for target 'ang3o' failed
make: *** [ang3o] Error 1
NOTES:
Related question: Error with multiple definitions of function
Possibly related: One definition rule and different class definitions in two translation units
However, in my situation: the headers are in the include directory. The c files are in a src directory. All functions are declared in the headers. Only headers are included, no c/cpp files. Makefile examples seem to halt just before this level of complexity, the use of different define segments of code, and the need to compile objects with/without certain defined sections.