How can I access the standard cpp library without placing the '#include' statements?

I taught 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 anyway. After that, I tried to completely remove all the standard include headers and found that my code was still working.

I assume that my inability to understand the preprocessor commands will be resolved by the time the book ends, but for now I just need to know how to make sure that my terminal screams at me when I make the header incorrectly, so I can learn where things are in the std library.

I am running OS 10.6.5, so I need to compile my code with the following exix exix 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 learn it too, and it's easy enough to use this file to compile all of my C ++ projects. I really don't understand the Makefile, in addition to changing the src file and the program name, I just got it from the Internet.

+4
source share
2 answers

Standard library headers may include other standard library headers. So if you, for example, #include <string> ; your implementation is allowed (but not required), including every other standard library header, including <algorithm> . In your case, this probably happened, but you should not rely on it.

+5
source

It's hard to know exactly without seeing your exact code, but one of the possible options is that you include other header files (Kristopher guesses OpenGL and / or GLUT in the comments), which, in turn, include libraries in which your code do not explicitly enable.

eg.

 # File: my_incl.h #include <algorithm> # File: main.c #include "my_incl.h" # yay - you just included the algorithm.h without even trying. 

Note that this works, but relying on this Bad Practice for various reasons:

  • If your project stops, including "my_incl.h", it will suddenly stop compiling without 100% immediately obvious reason.

  • This makes the code harder to read / understand, since the inclusion list allows you to quickly see which libraries are used by your code.

  • It's just sloppy

+5
source

Source: https://habr.com/ru/post/1333424/


All Articles