Agrim Pathak
Agrim Pathak

Reputation: 3207

Header files compiled into object files?

Below is a snippet of my Makefile. I read somewhere that header files should never be compiled on their own (i.e. without a .c or .cpp file), but below seems to work and I get a constants.o file. Is this file useful in any way? What if I use C++ templates: Should I still avoid compiling .h and .tcc files?

CXX = g++
CXXFLAGS = -Wall -c -std=c++11
SRC_DIR = src
TARGET_DIR = Debug

main: constants.o

constants.o: \
    $(SRC_DIR)/util/constants.h
    $(CXX) $(CXXFLAGS) -o $(TARGET_DIR)/$@ $<

Upvotes: 0

Views: 3074

Answers (3)

user3629249
user3629249

Reputation: 16540

header files can be pre-compiled and there are good reasons to do so.
this link: <http://en.wikipedia.org/wiki/Precompiled_header>
discusses pre-compiled header files.  The following is from the GCC description
for pre-compiled header files from the above link:

GCC[edit]

Precompiled headers are supported in GCC (3.4 and newer). GCC's approach is similar to these of VC and compatible compilers. GCC saves precompiled versions of header files using a ".gch" suffix. When compiling a source file, the compiler checks whether this file is present in the same directory and uses it if possible.

GCC can only use the precompiled version if the same compiler switches are set as when the header was compiled and it may use at most one. Further, only preprocessor instructions may be placed before the precompiled header (because it must be directly or indirectly included through another normal header, before any compilable code).

GCC automatically identifies most header files by their extension. However, if this fails (e.g. because of non-standard header extensions), the -x switch can be used to ensure that GCC treats the file as a header.

another excellent discussion of pre-compiled header files is located at: https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

Upvotes: 1

kuroi neko
kuroi neko

Reputation: 8661

C++ is the realm of things that seem to work.

If spending nights eating cold pizzas in your office is not your thing, I advise you to learn the difference between "the compiler did not complain about this" and "this actually works" quickly.

In this particular case, you might very well put a main() inside your .h. The compiler and linker will happily work on it.

The question is, what will you do when someone (possibly your own self 3 weeks older) tries to include the said .h and gets a complaint about duplicate main symbol? (and that's not even grazing the über cryptic template error messages issue).

Upvotes: 1

Deduplicator
Deduplicator

Reputation: 45664

The file-name is actually pretty arbitrary.

If you say your header-file is actually a translation-unit the compiler should compile, it will follow orders.

But violating conventions has a stiff penalty in confusing others (and your future selves 5 minutes from now); thus, don't do so needlessly, and there's no need here.

BTW: There is exactly one use to compiling header-files on their own, which is making sure they are self-contained.
Still, that should be unneccessary, as you have an implementation-file for every header-file, which includes its header-file on the first line anyway, right?

Upvotes: 3

Related Questions