ksiomelo
ksiomelo

Reputation: 1908

C++ compilation error: .o file not found

I'm trying to compile a small CPP project (http://sourceforge.net/projects/ibmquestdatagen/) on a Mac using g++, however I'm getting the following error:

sudo make -f Makefile.txt

g++ -O ran1.o expdev.o gammln.o gasdev.o poidev.o dist.o gen.o main.o command.o -lm -o gen
i686-apple-darwin11-llvm-g++-4.2: ran1.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: expdev.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: gammln.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: gasdev.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: poidev.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: dist.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: gen.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: main.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: command.o: No such file or directory
make: *** [gen] Error 1

It seems that I don't have the required .o files. Is there a way of generating them?

Here's the makefile:

HFILES1 = glob.h dist.h
HFILES2 = gen.h

CFILES1 =  ran1.C expdev.C gammln.C gasdev.C poidev.C dist.C
CFILES2 =  gen.C main.C command.C

OBJECTS1 = ran1.o expdev.o gammln.o gasdev.o poidev.o dist.o
OBJECTS2 = gen.o main.o command.o

LIBES = -lm
CC = g++
CFLAGS = -O

.SUFFIXES: .C

.C.o: 
    $(CC) $(CFLAGS) -c $*.c

gen:    $(OBJECTS1) $(OBJECTS2)
    $(CC) $(CFLAGS) $(OBJECTS1) $(OBJECTS2) $(LIBES) -o gen

test:   $(OBJECTS1) test.o
    $(CC) $(CFLAGS) $(OBJECTS1) test.o $(LIBES) -o test

clean:
    /bin/rm $(OBJECTS1) $(OBJECTS2)

$(OBJECTS1): $(HFILES1)
$(OBJECTS2): $(HFILES1) $(HFILES2)

GNU Make 3.81

Upvotes: 0

Views: 5847

Answers (3)

Markku K.
Markku K.

Reputation: 3908

You can check to see if your "make" knows how to create .o files from .C files by removing or commenting out the last two lines of your makefile.

#$(OBJECTS1): $(HFILES1)
#$(OBJECTS2): $(HFILES1) $(HFILES2)

Those lines list the .h files as prerequisites for the .o files, so it seems that make finds the .h files and assumes the .o files are good to go. If "make" doesn't have an implicit rule for building the .o files, you can add it as indicated by Jerry in his answer.

update: If Jerry's rule does not work in your makefile, try this:

%.o : %.C
    $(CC) $(CFLAGS) -c $<
#make sure the line above is indented with a tab, not spaces

If that works, try adding the two lines back in.

update 2: For some reason, none of these pattern rules seem to be working for the OP. Here is the brute force way:

foo.o : foo.C foo.h
    $(CC) $(CFLAGS) -c foo.C

You can add one of these rules for each of the .C files, replacing "foo" with the appropriate filename. If you do it this way, you don't need the $(OBJECTS): $(HFILES) lines.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490623

Try adding a couple lines to your Makefile like:

.SUFFIXES: .C

.C.o:
    $(CC) $(CFLAGS) -c $*.c

Most Makes have something like that built in (which is probably why it's not present in the makefile you got) but apparently yours doesn't. It may only recognize other extensions of C++, such as .cpp or .c++.

Upvotes: 4

user529758
user529758

Reputation:

"The way" for generating .o (object code) files is compiling the source files.

You may want to read this to get an idea about what's going on.

Upvotes: 1

Related Questions