I've made a little project to get experience with OOP in C++, i'm on Windows (with Cygwin).
The classes are point.h (the base class), point.cpp (implementation of point.h), coloredPoint.h (the derivated class), coloredPoint.cpp (implementation of the derivated class) and main.cpp (whic creates a point object and a coloredPoint object).
To run the main I type
g++ point.cpp coloredPoint.cpp main.cpp -o main
and all goes well! I know that this may could sound stupid...but is this the right way to do it?
For C++ you will want to replace gcc with g++ or add -lstdc++ (if you want to use the std c++ library) to your gcc command line:
gcc point.cpp coloredPoint.cpp main.cpp -o main -lstdc++
or
g++ point.cpp coloredPoint.cpp main.cpp -o main
which links the std c++ library with your compiled code. With g++ you do not have to add this step.
Related
Seen this post:
How do you compile a C++ program with multiple class files from OS X Terminal?
I see that to compile with gcc a project with a class (with .h and .cpp) is easy as:
g++ [list of all source files] -o [executableName]
And it actually works for me, but when i try to use Clang:
clang++-11 main.cpp Person.hpp Person.cpp -o main
I get:
clang: error: cannot specify -o when generating multiple output files
NOTE: i know how to use Makefile's, but i would like to have a quick way to compile in Clang like in gcc.
If you run clang++-11 main.cpp Person.hpp Person.cpp -o main with the header file, clang notices this and compiles the pre-compiled header "Person.pch", the default -o Person.pch is implied. Meanwhile you want getting yet another output file "main". Thus, clang complains it can't generate multiple output files, -o Person.pch and -o main.
Pre-compiled headers should be compiled separately.
clang++-11 Person.hpp
clang++-11 main.cpp Person.cpp -o main
The first step is not required, if skipped, no pre-compiled headers are used.
I wrote a c++ class(files class.h and class.cpp) that I want to use in a c++ application app.cpp from multiple directories on my linux system. Therefore, I moved the class.h and class.cpp files to /usr/local/include/. I tried to compile app.cpp using g++ -o app app.cpp which results in an undefined reference error(probably since g++ does not find the class.cpp file).
I figured out that I can fix this error by either writing the content of class.cpp directly into class.h or by explicitly specifying the location of class.cpp with the extended command g++ -o app app.cpp /usr/local/include/class.cpp. Obviously, both solutions are not satisfactory.
Presumably, someone who does this stuff more regularly(I'm not an computer science student) can give me a quick solution to my problem. Therefore, I ask now instead of investing more hours into searching the web.
Create a static compiled library using g++ -c class.cpp and ar rvs libclass.a class.o(can also create a shared library here)
Compile using g++ -o app app.cpp -lclass
I am trying to make my own header file which will contain a class Vector.
To create a header file, as advised by some websites, I have created two different files :-
1) collections.h (contains declarations)
2) collections.cpp (contains definition)
Another file is main.cpp, which contains the main function, and in which I will be using collections.h
All these files are in the same directory
The trouble I am having is that compiler is showing the error
Undefined reference to Vector::Vector(int, int)
and so on for all the functions in my class.
I have made sure that there is a #include "collections.h" line in both collections.cpp as well as main.cpp
How can I solve the above problem?
I am using gcc compiler under ubuntu 12.04
First build the object files:
gcc -c main.cpp -o main.o
gcc -c collections.cpp -o collections.o
then link them together:
gcc collections.o main.o -o main
You need to build both source files and link them together.
On a Linux command line, you can do it simplest by providing both source files to gcc:
$ g++ -Wall main.cpp collections.cpp -o my_program
Note: I added the -Wall option to enable more warnings by default. It's always a very good habit to enable more warnings, as they can often point out logical errors or possible places where you have undefined behavior.
I'm writing a small program known as "Alert Center", and the main classes/objects I'm using are: Point, Line, and Building. The contents of my Makefile are:
building.o: building.h line.h point.h building.cpp
g++ -c building.cpp
line.o: line.h point.h line.cpp
g++ -c line.cpp
point.o: point.h point.cpp
g++ -c point.cpp
alert_center: point.o line.o building.o
g++ point.o line.o building.o runner.cpp -o alert_center
The problem is that it only considers the first file listed. For example, if I change line.cpp, and then run make, nothing will happen and it will say that building.o has not changed. The change will only be compiled if I make the rule for line.o the first one. Does anybody know why this is happening?
Make alert_center the default target. You can do that explicitly with .default (see this), or simply list it as the first target to make it the default.
In the case of multiple targets, it is common to have a pseudo target named all and have it be the default. See the make recommended targets here.
Recently I have tried to compile a program in g++ (on Ubuntu). Usually i use Dev-C++ (on Windows) and it works fine there as long as I make a project and put all the necessary files in there.
The error that occurs when compiling the program is:
$filename.cpp: undefined reference to '[Class]::[Class Member Function]'
The files used are as following:
The source code (.cpp) file with the main function.
The header file with the function prototypes.
The .cpp file with the definitions for each function.
Any help will be appreciated.
You probably tried to either compile and link instead of just compiling source files or somehow forgot something.
Variation one (everything in one line; recompiles everything all the time):
g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]
Variation two (step by step; if no -o is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):
g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]
Variation three (some placeholders):
Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp will compile all cpp files in current directory to o(bject) files of the same name.
Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.
The command line of gcc should look like:
g++ -o myprogram class1.cpp class2.cpp class3.cpp main.cpp
Check in which cpp file the missing class member function is defined. You may have not given it to gcc.
You can also check for correct #include tags within filename.cpp. Assume that filename.cpp uses code contained in myclass.h present in the same directory as filename.cpp. Assume that the class that g++ says is undefined is contained in myclass.h and defined in myclass.cpp. So, to correctly include myclass.h within filename.cpp, do the following:
In filename.cpp:
#include <iostream>
#include <myclass.h>
//..source code.
In the makefile:
filename.o: myclass.C myclass.h filename.cpp
g++ -I./ -c filename.cpp -o filename.o
myclass.o: myclass.C myclass.h
g++ -c myclass.C -o myclass.o
In the above, note the use of -I. option when compiling filename.cpp. The -I<directory> asks g++ to include the path following the -I part into the search path. That way myclass.h is correctly included.
In the absence of more information (the source maybe), it is difficult to say with any accuracy where the problem lies. All attempts will be but stabs in the dark.
I assume that you have declared a member function (usually in a .h or .hpp file) but have ommited the respective definition of the member function (usually in a .cpp file).
In c++, it is possible to declare a class like so:
class foo {
void x();
void y();
}
with a cpp file that goes like so
void foo::x() {
do_something()
}
Note, there is no foo::y().
This poses no problem to the compiling/linking process as long as the member function foo::y() is referenced nowhere throughout the compiled code.