views:

127

answers:

3

Hi, 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 :)

+8  A: 

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.

William Pursell
Thanks a lot :)
A: 

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

Alec
Precompiled headers are very useful in C++ projects, as they can speed-up the compilation significantly, especially when using big header-only libraries like Boost.
PiotrLegnica
Thanks a lot :)
+3  A: 
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.

Sam Stokes
Thanks a lot :)