C++ When does class object file get compiled? - c++

I'm new to C++, and having difficulty understanding the steps of class object files getting created and compiled.
Let's say I create 3 files: 1. class header file 2. class cpp file (member function definitions present) 3. main cpp file
/*
When I run the main cpp file which includes the class header file (say as "#include class.h"), when does class cpp file get compiled, object file created, and linked?
The reason why I'm having difficulty is that from the compiler point of view, when it sees the main cpp file, there is only the header definition, no member function definition. However, even in the class header file, there is no class cpp file included. How would compiler know to run the class cpp file when it is not referred to in either class header file and the main cpp file?
*/
Let me clarify my question.
// Maybe I've said things I don't even understand lol.
So, basically I'm trying to run a main function in a say 'main.cpp' file. This 'main.cpp' file includes the 'class.h' header (include "class.h"). How would compiler execute the functions defined in header file when member function is not declared in 'class.h?' All my member function declaration is in 'class.cpp,' which is not included in 'class.h' or 'main.cpp.'

I suspect you're using an IDE, since you mention running cpp files - compilers don't know how to run anything.
Your IDE manages these dependencies for you.
When you press "Run", the IDE will decide which files in your project need compiling, and when the compilation is done it will link all the object files together.
If compilation and linking succeeded, the IDE then launches the executable program.
If you want a better understanding of the concepts, step away from your IDE and do all your compilation and linking on the command line for a while.
(It's not complicated, only tedious.)

You need to compile every .cpp file. Headers; .h files, are just for declarations, means that to let the compiler determine if you use the functions correctly. Each .cpp contains code and should be translated to machine code (.o files). After all these compilations, you need to link them to build the executable so that every function used is contained in the same file. The following commands can help you (using g++ compiler):
$ g++ -c myclass.cpp // produces the myclass.o file
$ g++ -c main.cpp // produces the main.o file
$ g++ -o myapp main.o myclass.o // produces the myapp executable
This is oversimplified for the sake of understanding.

Related

Can I include a cpp file in the main when there is a hpp?

For example:
I have a class called A. And there is:
A.hpp
A.cpp
main.cpp
for my project
By default, I only need to include A.hpp in the main so I can compile it, either using IDE such as Xcode or using:
g++ main.cpp A.cpp -o xxxxx
But the submission system only allows me to use:
g++ main.cpp -o xxxx
I tried to include A.cpp in the main, but the IDE says: main file cannot be included recursively when building a preamble
Is there any solution? I want to keep my hpp and cpp separately.
Can I include a cpp file
In theory, any file can be included.
But as a convention, you should never include cpp files.
But the submission system only allows me to use:
g++ main.cpp -o xxxx
If you cannot compile A.cpp then don't write such file at all. Write the definitions that you would have written in A.cpp into main.cpp instead. This achieves the same as including with a macro, but there won't be duplicate definitions in another cpp file.
You can #include any file you want. #include is automatic copy-paste. It looks in the file you tell it to include, and it reads whatever's in the file, and it pretends you wrote that in your original file. It doesn't care what's in the file, it just does that. You can include a .h file, a .hpp file, a .cpp file, a .txt file, a .py file, a .jpg file, or anything you want, as long as it's got valid C++ code in it.
Note that including a .cpp file is not the same as compiling it separately. And people expect that .cpp files are compiled separately, not included. To avoid confusing other programmers or the future version of yourself, you should rename the file to something else if you want to include it. You don't have to, but you should. If it's not a normal header file either (because you can't include it more than once), then you can make up some completely different extension, like .inc.

SWIG undefined symbols

I am using SWIG to wrap C++ code in Ruby.
I have eight classes defined in eight separate files in a specific location. I had two approaches to wrapping them in Ruby.
In the first approach, I put all the classes in one file, placed that file in the same directory as the SWIG interface file and everything is okay.
I am, however, requested to link to the original location of the files, and have my interface file in a different directory. When I compile, I compile all the files in their directory plus the wrapper code and there are no errors produced. However, I get undefined symbols.
A part of my compile shell script is:
g++ -std=c++11 -fPIC -c ../../dir1/dir2/Class.cpp
g++ -std=c++11 -fPIC -c mymodule_wrap.cxx -I/usr/include/ruby-1.9.1 -I/usr/include/ruby-1.9.1/x86_64-linux
I compile all the other seven files in the same way as the "File.cpp" one. No compilation errors.
Then, when I try the following
require 'mymodule'
c = Mymodule::Class.new
I get an undefined symbol for the Class' constructor (I demangled the undefined symbol using c++filt), which is declared and defined.
Is there something wrong in the way I compile? Or are there some problems when it comes to different locations of the header/source files and the SWIG interface file? Because this is in no way different from when I have all the classes in one file, except for the location.
EDIT:
If i move the definitions of the declared functions in the header files I get no undefined symbols. That means that it actually doesn't even reach the definitions in the cpp files. But why? When I had all classes unseparated I still kept the definitions in a cpp files and the declarations in a header file...
When creating the shared library, it didn't know where the object files of the source code were, therefore it never knew the definitions of whatever was declared in the header files.
For me, all the object files were created in the same folder as the interface file where I was compiling everything, and I added this:
g++ -shared Class.o mymodule_wrap.o -o mymodule.so
to my compile shell script. Before I was using the extconf makefile creating script, and I am not sure where it searched for the object files.

Use outer class in my main.cpp

I'm using other's class for generating delaunay triangle. Its class has two files: VoronoiDiagramGenerator.h and VoronoiDiagramGenerator.cpp. It is all encapusulated into a class.
I want to call the class method in my main.cpp file, so I should include the VoronoiDiagramGenerator.h file.
If i want to use gcc or g++, how do I set the cmd parameter? Before I just used gcc -o main.cpp or something similar.
If I want to use makefile, how would I write it?
If I want to compile the two files (VoronoiDiagramGenerator.h && VoronoiDiagramGenerator.cpp) into a So file, how should I do?
I just test the souce code. When i under windows vc++, add the .cpp and .h into the project workspace, it will be OK. If i just include the .h file in my main file and it give some similar error like linux.
some unreference error.
so i think in my main file just include the out class headfile and gcc main.cpp is error.
In your main.cpp include VoronoiDiagramGenerator.h and use it.
If .h file is not in your current or project directory, make sure to include -Idirectory

How do you compile just a .h file in a makefile?

I have a makefile that creates object files for two classes (and main) and one of those classes is just defined in a .h file. In my makefile I have a line that says
FileName.o: FileName.h
g++ -c FileName.h
but when I try to compile it says it can't find FileName.o
Do I have to create FileName.cpp in order to get this to compile?
You are using your class from FileName.h somewhere, aren't you? So at least one of your .cpp files should contain #include "FileName.h", and .h's code will be compiled with this .cpp and you needn't compile .h's code separately.
You don't normally attempt to compile a header (.h) file by itself. Including it into an otherwise empty .cpp file will let you compile it and produce a .o file, but it probably won't do much (if any) real good unless you've put things in the header that don't really belong in a header.

C++ Header and CPP includes

quick question.
I am trying to get C++ nailed down, and today I spent hours with a double definition linker error("this has already been defined!") and I finally realised it's because I had the layout as such:
main.cpp
#include Dog.cpp
Dog.cpp
#include Dog.h
Dog.h
// (Dog class and prototype of test function)
And now that I've cleared that up by including the Dog.h instead of the Dog.cpp in the main.cpp.
By including the .h file, does the .cpp file with the identical prefix get compiled with the program?
I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp. I spent ages Googling but no answers really helped me understand what was going on.
Edit: I forgot to add that I prototyped in the .h, and defined the function for the class in the .cpp and that's what gave me the "already defined" error.
By including the .h file, does the .cpp file with the identical prefix get compiled with the program? I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp.
No.
Your program is built in phases.
For the compilation phase, only declarations are needed in each translation unit (roughly equivalent to a single .cpp file with #includes resolved). The reason that declarations even exist in the first place is as a kind of "promise" that the full function definition will be found later.
g++ -c Dog.cpp # produces `Dog.o`
g++ -c main.cpp # produces `main.o`
For the linking phase, symbols are resolved between translation units. You must be linking together the result of compiling Dog.cpp and of compiling main.cpp (perhaps your IDE is doing this for you?), and this link process finds all the correct function definitions between them to produce the final executable.
g++ Dog.o main.o -o program # produces executable `program`
(Either that, or you actually haven't got to the link phase yet, and merely have an object file (Dog.o); you can't execute it, partially because it doesn't have all the function definitions in.)
The two phases can be done at the same time, with the "shorthand":
g++ Dog.cpp main.cpp -o program # compiles, links and produces executable
No, the .cpp file does NOT automatically get compiled. You can either do that manually, create a makefile, or use an IDE that has both of them in the same project.
You don't specify how you are compiling it. If you are using an IDE and have a new .h and .cpp to the project automatically then it will all be compiled and linked automatically.
There are 2 stages to making an executable to run: compiling and linking. Compiling is where the code gets interpretted and translated into lower level code. Linking is where all of the functions that you used get resolved. This is where you got the duplicate function error.
Inclusion does not automatically cause compilation, no.
In fact, the actual compiler never sees the #include statement at all. It's removed by an earlier step (called the preprocessor).
I'm not sure how it could build if you never compiled the Dog.cpp file. Did you reference any objects with code defined in that file?