header-files

Makefile, header dependencies

被刻印的时光 ゝ 提交于 2019-11-25 23:59:23
Let's say I have a makefile with the rule %.o: %.c gcc -Wall -Iinclude ... I want *.o to be rebuilt whenever a header file changes. Rather than work out a list of dependencies, whenever any header file in /include changes, then all objects in the dir must be rebuilt. I can't think of a nice way to change the rule to accomodate this, I'm open to suggestions. Bonus points if the list of headers doesn't have to be hard-coded dmckee If you are using a GNU compiler, the compiler can assemble a list of dependencies for you. Makefile fragment: depend: .depend .depend: $(SRCS) rm -f ./.depend $(CC) $

Why should I not include cpp files and instead use a header?

自闭症网瘾萝莉.ら 提交于 2019-11-25 23:46:08
问题 So I finished my first C++ programming assignment and received my grade. But according to the grading, I lost marks for including cpp files instead of compiling and linking them . I\'m not too clear on what that means. Taking a look back at my code, I chose not to create header files for my classes, but did everything in the cpp files (it seemed to work fine without header files...). I\'m guessing that the grader meant that I wrote \'#include \"mycppfile.cpp\";\' in some of my files. My

Why aren't my include guards preventing recursive inclusion and multiple symbol definitions?

笑着哭i 提交于 2019-11-25 23:09:37
问题 Two common questions about include guards: FIRST QUESTION: Why aren\'t include guards protecting my header files from mutual, recursive inclusion ? I keep getting errors about non-existing symbols which are obviously there or even weirder syntax errors every time I write something like the following: \"a.h\" #ifndef A_H #define A_H #include \"b.h\" ... #endif // A_H \"b.h\" #ifndef B_H #define B_H #include \"a.h\" ... #endif // B_H \"main.cpp\" #include \"a.h\" int main() { ... } Why do I get

“using namespace” in c++ headers

谁说我不能喝 提交于 2019-11-25 22:29:03
问题 In all our c++ courses, all the teachers always put using namespace std; right after the #include s in their .h files. This seems to me to be dangerous since then by including that header in another program I will get the namespace imported into my program, maybe without realizing, intending or wanting it (header inclusion can be very deeply nested). So my question is double: Am I right that using namespace should not be used in header files, and/or is there some way to undo it, something

Why have header files and .cpp files? [closed]

天大地大妈咪最大 提交于 2019-11-25 21:44:33
问题 Why does C++ have header files and .cpp files? 回答1: Well, the main reason would be for separating the interface from the implementation. The header declares "what" a class (or whatever is being implemented) will do, while the cpp file defines "how" it will perform those features. This reduces dependencies so that code that uses the header doesn't necessarily need to know all the details of the implementation and any other classes/headers needed only for that. This will reduce compilation