I want to open an output file in main.cpp
, then write to it in another file calculate.cpp
.
main.cpp:
#include main.hpp
using namespace std;
int main() {
outputfile.open("output.txt");
}
using the global variable from the header file main.hpp
extern std::ofstream outputfile;
Then write to it in another file calculate.cpp
#include main.hpp
void calculate() {
outputfile << "write this to the external file" << endl;
}
When I do this, I get the error
undefined reference to 'outputfile' in main.cpp
undefined reference to 'outputfile' in calculate.cpp
I'm working on a large code that has a premade make file, so I don't think proper linking is the issue.