Xcode won't detect files included from main in included file - c++

here is the structure of my program:
// File: main.cpp
#include <iostream>
#include <math.h>
using namespace std;
#include "do.cpp"
int main()
{
doit();
}
// File: do.cpp
void doit()
{
cout<<sqrt(2)<<endl;
}
When I do
g++ main.cpp
Everything goes fine. Now, if I open this as an Xcode project (I have chosen "Command line utility" as project type) and try to just build and run, it keeps saying:
Use of undeclared identifier sqrt
Unknown type name 'ostream'
What should I be supposed to do? Did I do something wrong?
Thank you very much!
Matteo

Don't do this:
#include "do.cpp"
but instead put that "do.cpp" file in the same Xcode project, alongside your main.m or main.cpp file.
And when you want to build from the command line, you can do:
g++ main.cpp do.cpp -o mytesttool
which would create the command line tool named "mytesttool".

The explanation is quite simple actually. You probably added both files to the project. Xcode tries to compile each file into an object file and then link them together.
When it tries to compile do.cpp it doesn't find the definition of cout because iostream is not included and neither math.h for sqrt, as part of do.cpp.
That file compiles fine when compiled as part of main.cpp, because it is included in the file and it finds iostream and math.h and also the using declaration.
Anyway if you remove do.cpp from the project (just the reference) everything should compile as expected.
The right way without a header file
// File: main.cpp
void doit(); // declare the function
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
The right way with a header file
// File do.h
#ifndef __DO_H_
#define __DO_H_
void doit();
#endif // __DO_H_
// File: main.cpp
#include "do.h"
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
#include "do.h"
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}

When making the new file, I forgot to de-check the "target" selection, so that when it tried to build the project it tried to build all the single files and then link them together. By disabling the "target", I got it to work.

Related

How to link multiple .cpp files in Code::Blocks for a single project?

While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp

How to divide code into multiple .cpp files C++

I want to create a function outside the main.cpp file
i've tried creating a header file but it doesn't work:
Main.cpp:
#include "other.h"
int main() {
MyFunc();
}
Other.cpp
#include <iostream>
#include "other.h"
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}
Other.h
#include <iostream>
#include "other.cpp"
void MyFunc();
nor CPP, G++, GCC compiler work
GCC Compiling error
Errors shown by vs code
You must include a header file and not a C++ file.
And therefore, you need to remove:
#include "other.cpp"
from other.h & use the following command-line for compiling:
g++ -o output main.cpp other.cpp
You'll get it linked and then compiled, then everything should be working fine.
You must remove #include "other.cpp" in header file.
erase the line "#include "other.cpp" in your other.h and you will be fine...
Edit: you also need a header guard...

Classes in separate files

I'm trying to work with classes in separate files in CodeBlocks but I'm getting the following problem.
I have 3 files: main.cpp, clasa.h and clasa.cpp.
clasa.h
#pragma once
class clasa
{
public:
clasa();
};
clasa.cpp
#include "clasa.h"
#include <iostream>
using namespace std;
clasa::clasa()
{
cout<<"hi";
}
main.cpp
#include <iostream>
#include "clasa.h"
using namespace std;
int main()
{
clasa obj;
return 0;
}
When I include these 3 files into a project, the output is hi.
When I DON'T include them into a project, main.cpp just doesn't build. But if i replace "clasa.h" with "clasa.cpp" it works again.
Why does it not work otherwise?
TL;DR - It looks like you are not compiling the header file (*.h) in the the built executable.
When you click the run button the computer does two things. First it compiles the code and makes an executable. Then it runs the executable.
First how does the compiler work?
It reads *.cpp and when it comes across "#include" it replaces that line the code from the specified file. After the compiler processes the #include "clasa.h" line the main.cpp file will look like this:
#include <iostream>
#pragma once
class clasa
{
public:
clasa();
};
using namespace std;
int main()
{
clasa obj;
return 0;
}
It does the same for the also.
When you remove the *.h file from the project the compiler does not include the code in the executable.
The reason it works with the *.cpp variant is because the compiler does not include *.cpp files. They are accessed as the program in run.
Hope this helped you.

How to use header files?

I wanted to learn using header files. and I got an error. here is my code:
printmyname.h:
void printMyName();
printmyname.cpp:
#include "printmyname.h"
void printMyName() {
cout << "omer";
}
try.cpp (main file):
#include <iostream>
#include "printmyname.h"
using namespace std;
int main() {
printMyName();
return 0;
}
Here is the error:
undefined reference to `printMyName()`
What's is the problem?
Undefine reference has nothing to do with your header file in this case. It means the linker cannot find the implementation of printMyName which is in printmyname.cpp. If you are using g++, you should try:
g++ try.cpp printmyname.cpp -o yourBinaryName
If you are using a makefile, you should add dependency(printmyname.cpp) correctly for try.cpp.
Edit:
As #zmo suggest in his comment:
you can also do it through a two times compilation (more suitable with Makefiles):
g++ -c printmyname.cpp
g++ try.cpp printmyname.o -o yourBinaryName
If you are using Windows, you need to add the printmyname.cpp to your project too.
Consider adding an include guard to your header
#ifndef PRINTMYNAME_INCLUDED
#define PRINTMYNAME_INCLUDED
void printMyName();
#endif
You will also need to move the #include <iostream> and using namespace std; from the try.cpp to the printmyname.cpp file.
You need to add code/definition in printMyName.cpp inside printMyName.h only.
void printMyName();
{
cout << "omer";
}

g++ is unable to find my header file

I'm trying to compile the file q1.cpp but I keep getting the compilation error:
q1.cpp:2:28: fatal error: SavingsAccount.h: No such file or directory
compilation terminated.
The header file and the implementation of the header file are both in the exact same directory as q1.cpp.
The files are as follows:
q1.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
int main() {
SavingsAccount s1(2000.00);
SavingsAccount s2(3000.00);
}
SavingsAccount.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
//constrauctor
SavingsAccount::SavingsAccount(double initialBalance) {
savingsBalance = initialBalance;
}
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12
}
SavingsAccount::modifyInterestRate(double new_rate) {
annualInterestRate = new_rate;
}
SavingsAccount.h:
class SavingsAccount {
public:
double annualInterestRate;
SavingsAccount(double);
double calculateMonthlyInterest();
double modifyInterestRate(double);
private:
double savingsBalance;
};
I'd like to reiterate that all files are in the SAME directory. I'm trying to compile by using this line at a windows command prompt:
C:\MinGW\bin\g++ q1.cpp -o q1
Any input into this problem would be appreciated.
#include <SavingsAccount.h>
should be
#include "SavingsAccount.h"
since SavingsAccount.h is the header file you defined, you should not ask the compiler to search for system headers by using <> around it.
Meanwhile, when you compile it, you should compile both cpp files: SavingsAccount.cpp and q1.cpp.
g++ SavingsAccount.cpp q1.cpp -o q1
BTW: you missed a ; here:
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12;
//^^; cannot miss it
}