Reputation: 313
I always have problems with c++ on this, I spend more time trying to solve dependencies instead of programming when I setup a new project. I search the internet a way to do this automatic, or softwares that do that. In fact, I always program on geany and compile with shell script files...
So, is there a software to manage this? Do IDE's do that?
I always include .cpp files on my main.cpp and then I include the .hpp files on these .cpp. So, if I have a main.cpp, a object.hpp and a object.cpp, I will include the object.cpp in the main.cpp and the object.hpp on the object.cpp. Is there a better way to do that?
Can I just include the .hpp files and in the build script add every .cpp file?
I just cant find the answer on the internet, maybe im doing the wrong question...
Upvotes: 0
Views: 466
Reputation: 3454
I see a lot of good suggestions with good practices but your mistake (including .cpp
files from a .cpp
file) suggest you're missing some concept in the C
/C++
build process, I hope a little explanation would help you understand better and avoid the mistake.
Think of .c .cc .cxx .cpp
files as modules, a .cpp
file is a module, with your implementation of something, .h .hpp
are just headers where usually you don't put implementations but declarations to be shared with multiple modules.
Usually each .cpp
module is compiled to a binary object g++ -c -o mymod1.o mymod1.cpp
then (once all modules are compiled) linked together g++ -o myprog mymod1.o mymod2.o ...
.
Even if you compile and link with a single command g++ -o myprog mymod1.cpp mymod2.cpp
behind the scene g++
handle each module as single object.
I think is important you understand that each module/object know nothing about others, and if you need some other module (your main.cpp
) to know something about mymod1.cpp
a header file is required .h .hpp
(mymod1.h
) with the declarations needed to be shared: module global variables, defines, enums, function prototypes or class declarations, then just include mymod1.h
in the module(s) where you want to use something of your mymod1
implementation (main.cpp
).
Also, you write you're using a shell script to build, that's ok if your project are few files, better would be to use something like make
, learn how to use it will require some time but then I bet geany
have some facility to build projects based on Makefiles
, make
is the way to handle C
/C++
projects from a long time.
Upvotes: 0
Reputation: 392
I have found a nice article dealing with including files.
Common practice for all c++ header files is to simply define inclusion guards.
#ifndef TEST_H
#define TEST_H
// class definitions goes here
#endif
If there are some cyclic dependencies, consider forward declaration.
Every-time this header is included, the compiler checks, whether symbol TEST_H has been defined already. This basically guarantees, that contents of this file are included only once, and so that there is single declaration of the classes, defined in header file.
Good to know is, that directive "#include <>" does copy and paste all the contents of the included file.
Including .cpp file is not strictly disallowed, and sometimes good choice, it is considered a bad practice. As I mentioned, including file, means that all contents of the file are being duplicated at the place of inclusion. This is okay, for the header file with inclusion guard, but not okay for .cpp file, since every function definition inside this file, will be duplicated.
Not including file in the build script means, that only the those duplicated data are included in the build, otherwise you would end up with multiple function redefinition errors.
If you are looking for IDE, consider:
IDE won't do all the work, but you can be significantly more productive using good IDE.
TLDR:
Upvotes: 3