I've been teaching myself cpp sporadically from 'accelerated C++' and recently I noticed that when I forgot my #include <algorithm>
statement, my code (which includes transform and find_if) compiled and ran successfully anyways. After this, I tried removing all standard header include statements altogether and found that my code still ran.
I assume my inability to understand preprocessor commands will be resolved by the time I finish the book, but for now I just need to know how to make sure that my terminal yells at me when 开发者_运维百科I make the header improperly so I can learn where things are located in the std library.
I am running OS 10.6.5 so I have to compile my code with the following unix exe file:
CC = g++
CFLAGS = -Wall
PROG = TrainingProject23
SRCS = TrainingProject23.cpp
ifeq ($(shell uname),Darwin)
LIBS = -framework OpenGL -framework GLUT
else
LIBS = -lglut
endif
all: $(PROG)
$(PROG): $(SRCS)
$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)
clean:
rm -f $(PROG)
it includes the build protocol for OpenGL because I am learning that as well and it easy enough to use this file to compile all my C++ projects. I don't really understand the Makefile besides how to change the src file and program name, I just got it off the internet.
Standard library headers are allowed to include other standard library headers. So if you e.g. #include <string>
; your implementation is allowed (but not required to) include every other standard library header there is, including <algorithm>
. In your case, that probably happened, but it's nothing you should rely on.
It's hard to know for sure without seeing your exact code, but one possible option is that you are including other header files (Kristopher in comments is guessing OpenGL and/or GLUT) which in turn include the libraries your code didn't include explicitly.
E.g.
# File: my_incl.h
#include <algorithm>
# File: main.c
#include "my_incl.h"
# yay - you just included the algorithm.h without even trying.
Please note that this works, but relying on it is Bad Practice, for a variety of reasons:
If your project stops including "my_incl.h", it will suddenly stop compiling, without 100% immediately obvious cause.
It makes the code harder to read/understand since a list of includes allows you a quick summary of which libraries are used by your code.
It's Just Sloppy
精彩评论