3
votes

I'm trying to compile a c++ program, which is something I didn't do for a long time...

What I'm trying is:

g++ -c A.cpp  -o A.o

g++ -c dir/B.h -o B.o

which seem to work, and then I try:

g++ A.o B.o -o A -lX11 -lpthread

and get:

B.o: file not recognized: File format not recognized

collect2: ld returned 1 exit status

What is the problem?

Thanks a lot :)

3

3 Answers

8
votes

Omit the -o argument when you compile b.h, and you will likely see that g++ creates a file named b.h.gch rather than b.o. That file is a "pre-compiled header file". By renaming in b.o, you are lying to the subsequent invocation of g++ about the contents of the file. If b.h is a header file, then you should include it in a.cpp. If b.h contains function definitions, you should rename it b.cpp.

3
votes
g++ -c dir/B.h -o B.o

Why are you compiling a header file?

I assume A.cpp includes dir/B.h - so you don't need a separate compiler invocation to compile the header.

0
votes

You're compiling a header file. No good can come of that. :-)