Hello,

So I am just starting to really use Make files in Linux, and I am getting the following error when I try to "make":


c++ -c main.cxx
In file included from main.cxx:2:
LinkedList.h:39:26: error: LinkedList.cxx: No such file or directory
make: *** [main.o] Error 1


Here is my make file, I have no experience with make files so I am not sure now to read the error:


program2: main.o LinkedList.o
c++ -o program2 main.cxx LinkedList.cxx
main.o: main.cxx LinkedList.h
c++ -c main.cxx
LinkedList.o: LinkedList.cxx LinkedList.h
c++ -c LinkedList.cxx
clean:
rm program2 LinkedList.o main.o

ah so the way to read the error would be to say that:

the file LinkedList.cxx was never found/declared in the file LinkedList.h?

ah so the way to read the error would be to say that:

the file LinkedList.cxx was never found/declared in the file LinkedList.h?

Well, that depends on what's on line 39 in that file. Are you trying to include LinkedList.cxx?

here is the file in question


1 class LinkedList
2 {
3 private:
4
5 struct LL_Ele
6 {
7 int i_nt;
8 LL_Ele * next;
9 LL_Ele * pred;
10 };
11
12 typedef LL_Ele Link_t;
13 Link_t curr;
14 Link_t pred;
15 Link_t start;
16
17 public:
18
19 bool empty(); //written
20 int size(); //
21 void begin(); //
22 void clear(); //
23 void erase(); //
24 void pushback(int); //
25 void pushfront(int); //
26 void next(); //
27 void previous(); //
28 void insertInOrder(int); //
29 void remove(int); //
30 int data(); //
31 void insert(int); //
32 void sort(); //
33 void merge(LinkedList); //
34 int popfront(); //
35 int popback(); //
36 LinkedList(); //
37 LinkedList(const LinkedList&); //
38 };
39 #include <LinkedList.cxx>

I can guess that isn't going to work well!! You really don't need to include any cxx files in your .h files. [With the possible exceptions of some template classes but even then you should avoid it].

First: Small point , don't use #include <file.h> for your files, the < > searchs the whole include path e.g. /usr/include etc, and your files are going to be local to the project directory, so you have two problems, it takes longer and you have the possibility of getting the wrong file, if you are unlucky with your naming.

So what you really want is to compile the .o files (the object files) for LinkedList and then main etc. Let us do it manually and then see how to construct a simple Makefile.

g++ -o LinkedList.o LinkedList.cxx
g++ -o main.o main.cxx
// Linking bit (putting the object files together)
g++ -o Program2 main.o LinkedList.o 

So what you have done here is compiled two object files, and then after the fact, linked them together. Now if you were to change LinkedList.cxx, then you would only do the first and third step, since main has not changed. If you change linkedlist.h, since both main.cxx and LinkedList.cxx have that file included you will do all three steps.
Now the purpose of make, is to automate those decisions.

The Makefile is to do that as well, but only compile those bits that need compiling if something has changed OR the dependencies of the file (the include files) have changed.

# Makefiles [simple]
all: program2          ## All build program 2 only at the moment

program2: main.o LinkedList.o
<TAB>      g++ -o program2 main.o LinkedList.o

main.o: main.cxx LinkedList.h
<TAB>     g++ -o main.o main.cxx

LinkedList.o: LinkedList.cxx LinkedList.h 
<TAB>     g++ -o LinkedList.o LinkedList.cxx

That is the simple makefile, the <TAB> represent a real tab spacing, [don't write <TAB> ;) ]
Note the idea of dependency, i.e. LinkedList.o depends on LinkedList.cxx and LinkedList.h, i.e if either of
those .cxx or h files are more recent than the .o file an new .o file will be made using the rule give after the
tab.

However, note that program2 depends on the rules main.o and LinkedList.o so those rules are checked. Rules are more important than files in a makefile, and thus even if the rule main.o did not make a main.o then the rule would still be checked an carried out.

You will not need to include any cxx files in your .h files. You MAY have to add
-I./myproject/include if your .h files are in subdirectories etc.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.