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...
Related
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
I want to use a function that from the "func.h" file in the Wireshark open source project.
I need to use the funct() function in multiple .cpp files, but I get the a multiple definition error.
func.h:
#ifndef func_h
#define func_h
#include<string>
void *funct(char *cName)
{
std::string name = cName;
cName+= ".extension";
}
In the .cpp files I include the func.h:
#include "func.h"
And call the funct() function from 2 .cpp files:
funct("program");
What should I do so I don't get the multiple definition error? A workaround is to copy and paste the function defition in every .cpp file and change the function name, but this is ugly.
Many thanks.
You have two options:
You move the implementation of the function in a separate .c/.cpp file, leaving in the header file only the declaration. Then you compile this source file, and link it with the rest of the program. Example:
func.h
#ifndef func_h
#define func_h
#include<string>
void *funct(char *cName);
#endif
func.cpp
#include "func.h"
void *funct(char *cName)
{
std::string name = cName;
cName+= ".extension";
}
You compile and link as usual, e.g.
g++ -c -o func.o func.cpp
g++ -c -o main.o main.cpp
g++ -o prog main.o func.o
You specify the keyword inline in the definition of the function. This allows appearance of the function in multiple compile units. See this question.
func.cpp:
#include "func.h"
void *funct(char *cName)
{
std::string name = cName;
cName+= ".extension";
}
func.h:
#ifndef func_h
#define func_h
#include<string>
void *funct(char *cName);
#endif
Then in your build script (CMakeLists.txt, or a simple Makefile), compile both .cpp files and link them into one executable.
You may want to put using namespace std; or using std::string; (check the latter syntax; I usually use the former), in which case you don't have to write std::.
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.
In my first.cpp I put #include second.h.
As a consequence first.cpp sees the content of second.cpp.
In second.cpp I put #include third.h.
My question is: will first.cpp see the content of the third.cpp?
ADDED
I thought that if I include second.h I will be able to use functions declared in second.h and described (defined, written) in the second.cpp. In this sense the content of the second.cpp becomes available to the first.cpp
When using #include the actual file content of that file appears as part of the input for the compiler. This means that first.cpp will appear to the compiler as if it has second.h and third.h inside it. The source code in second.cpp and third.cpp are separate files [1], that need to be compiled separately, and they are all combined at the linking stage at the end of compilation.
[1] unless second.h contains #include "second.cpp"or something to that effect - this is not typically how to do this, however, so I'm going to ignore that option.
You can think about #include as a simple text insert. But if you include second.h you "see" second.h and not second.cpp.
With a concrete example
//second.h
#ifndef SECOND_INCLUDED
#define SECOND_INCLUDED
void foo();
#endif
//first.cpp
#include second.h
void bar()
{
foo();//ok
}
void no_good()
{
wibble();//not ok - can't see declaration from here
}
//third.h
#ifndef THIRD_INCLUDED
#define THIRD_INCLUDED
void wibble();
#endif
//second.cpp
#include second.h
#include third.h
void foo()
{
wibble();//ok in second.cpp since this includes third.h
}
The cpp file that includes a header file sees what's in the header file, not in other source files that include the header.
first.cpp will see the constent of third.h (and you will be able to use in first.cpp functions declared in third.h and defined in third.cpp). Suppose, you have in your test.h:
#include<iostream>
using namespace std;
and in your test.cpp:
#include "test.h"
int main()
{
cout << "Hello world!" << endl;
}
As you see, cout declared in <iostream> is visible in test.cpp.
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";
}