Including header files and source files for classes
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
This would also work:
g++ -Wall -o main main.cpp test.cpp
In my first method I build a non-linked object file (the -c option does this) that contains the binary code for the test.cpp class. This binary must be included in the 2nd compile for linking purposes. The 2nd method gets rid of the middle step... but is less ideal if you are building a large project with lots and lots of files.
BTW, to compile that example I did have to change endl to std::endl in the cout statement. It would have been equally correct to just set the namespace to standard by putting in a "use namespace std;" line in main.cpp.
Originally posted by jtshaw If you find yourself having to include cpp files then typically one of the following is true:
1. Your missing a declaration in your header file.
2. Your doing some obscure embedded programming with a poor compiler.
3. Your not linking your program properly.
4. You have a major design problem.
I have seen this only once, in glibc, but there it is used very, very often. Entering
grep -r '#include.*\.c[">]' glibc-2.3.5 | wc -l
gives me 1607 lines. I don't know why it is done there, certainly not for point 1., 2., or 3., and probably not for 4.; it seems that sometimes there are good reasons to do so.