Probelm with a c++ makefile

Dear forum members,

I am writng in relation to a problem I have, when trying to compile a c++ source with a makefile.
The code has only one peculiarity that it contains a FORTRAN subroutine. This is not an issue because I can link and compile it without problems when I use the command line. I first create an object from the FORTRAN subroutine and then can link it without any problems. However if I try to do the same with my make file, I gate an linking error. My guess is that I have something wrong in the make file.
The first thing I did is to use the –MM option of g++ to generate the dependencies, for compiling i use intel compilers:

1
2
3
4
5
6
a.o: a.cpp a.h
b.o: b.cpp a.h f.h b.h
f.o: f.cpp f.h
main.o: main.cpp a.h b.h f.h matt.h fortm.h
matt.o: matt.cpp
fortm.o: fortm.F90


The actual make file is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/bash -l

TARGET = MAZ
files1 = $(shell find *.cpp)
files2 = $(shell find *.F90)
SRC = ${files1} ${files2}
OBJ1 = $(files1:%.cpp=%.o)
OBJ2 = $(files2:%.F90=%.o)
OBJ = ${OBJ1} ${OBJ2}
DEPENDFILE = DEPP

CC = icc
FC = ifort
OPT =

${TARGET} : ${OBJ}
        ${CC}  $< -o $@


%.o : %.F90
        ${FC} -c $< -o $@

%.o : %.cpp
        ${CC} -c $< -o $@

dep : ${SRC}
        gcc -MM $(SRC) > $(DEPENDFILE)

.PHONY : clean
clean:
        rm -f *.o

obj: ${SRC}
        echo ${OBJ}

-include $(DEPENDFILE)


When I run this I get the following linking errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
a.o: In function `A::~A()':
a.cpp:(.text+0x42): undefined reference to `std::cout'
a.cpp:(.text+0x4c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
a.cpp:(.text+0x54): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
a.cpp:(.text+0x59): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
a.o: In function `A::~A()':
a.cpp:(.text+0x62): undefined reference to `std::cout'
a.cpp:(.text+0x6c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
a.cpp:(.text+0x74): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
a.cpp:(.text+0x79): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
a.o: In function `__sti__$E':
a.cpp:(.text+0x97): undefined reference to `std::ios_base::Init::Init()'
a.cpp:(.text+0x9c): undefined reference to `std::ios_base::Init::~Init()'
a.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
make: *** [MAZ] Fehler 1
 



I am trying to learn the whole issue with the make files and would appreciate if the knowledgeable people on this forum can provide me with assistance.
Thanks
Wronski
$< is the first dependency
$^ are all the dependencies

Also, tabs.
Hi
I think that the problem is contained in the follwoing line:

1
2
        ${TARGET} : ${OBJ}
        ${CC}  $< -o $@


if I call the following statement form the command line, I can compile without any problems :

 
icc -o MM a.o f.o  b.o fortm.o matt.o main.cpp


In case I change :

 
icc -o MM a.o f.o  b.o fortm.o matt.o main.o


Everyting crashes. I think that the problem is in the make file.

If i change $< with $^ I get more error messages than before.
$ make --just-print


icc -o MM a.o f.o  b.o fortm.o matt.o main.cpp
icc -o MM a.o f.o  b.o fortm.o matt.o main.o
should be equivalent.
`Everyting crashes' is not a meaningful message, I don't understand why you blame the makefile when the error also occurs with the command line.

If i change $< with $^ I get more error messages than before.
¿Do they say the same? If your code fails to build, maybe the problem is with your code.

Also, please note that the tags don't wrap text, so fmt the messages or don't use tags
I haven't used icc, but the behaviour you mention suggests icc is expecting at least one cpp files.

Should you be using ld for linking rather than icc?

(I see you're passing -c to icc to tell it to compile only earlier in the makefile)

Andy
Last edited on
man wrote:
Using icpc always links in C++ libraries. Using icc only links in C++ libraries if C++ source is provided on the command line.
Use icpc (international collegiate programming contest)
I see the example makefile given in this thread

makefile question?
http://www.cplusplus.com/forum/general/83338/

is using

1
2
$(APP) : $(OBJS)  
	$(CXX) $(LDFLAGS) $^ $(LIBS) -o $@ 


but here the compiler is prob. the GCC one.

Andy
Well I can compile and run the code via the command line without any problems. The following works perfectly fine:

1
2
ifort -c matt.F90
icc -o EXE a.cpp f.cpp  b.cpp fortm.o matt.cpp main.cpp




According to my experience, concerning this project the commands are not equivalent

1
2
icc -o EXE a.o f.o  b.o fortm.o matt.o main.cpp
icc -o EXE a.o f.o  b.o fortm.o matt.o main.



The problem comes in my opinion on the linking stage. Concerning the loose statement ‘Everything crashes’ – well I get more than 250 lines of error messages. The problem is that this project will grow in its files numbers, and I would like to learn what am I doing wrong?
Last edited on
I thing you should try ne555's suggestions, if you haven't already done so (use icpc rather than icc).
Last edited on
I just tried replacing icc with g++ / icpc and everythig is fine. I hear for the first time of icpc, well thank you all for the help.
Last edited on
Topic archived. No new replies allowed.