I’ve created some mathematical functions that will be used in main() and by member functions in multiple host classes. I was thinking it would be easiest to make these math functions global in scope, but I’m not sure how to do this.
I’ve currently put all the functions in a file called Rdraws.cpp, with the prototypes in Rdraws.h. Even with all the #includes and externs, I’m getting a “symbol not found” compiler error at the first function call in main().
Here’s what I have:
// Rdraws.cpp
#include <cstdlib>
using namespace std;
#include <cmath>
#include "Rdraws.h"
#include "rng.h"
extern RNG rgen // this is the PRNG used in the simulation; global scope
void rmultinom( double p_trans[], int numTrials, int numTrans, int numEachTrans[] )
{ // function 1 def
}
void rmultinom( const double p_trans[], const int numTrials, int numTrans, int numEachTrans[])
{ // function 2 def
}
int rbinom( int nTrials, double pLeaving )
{ // function 3 def
}
// Rdraws.h
#ifndef RDRAWS
#define RDRAWS
void rmultinom( double[], int, int, int[] );
void rmultinom( const double[], const int, int, int[] );
int rbinom( int, double );
#endif
// main.cpp
...
#include "Rdraws.h"
...
extern void rmultinom(double p_trans[], int numTrials, int numTrans, int numEachTrans[]);
extern void rmultinom(const double p_trans[], const int numTrials, int numTrans, int numEachTrans[]);
extern int rbinom( int n, double p );
RNG rgen; // global PRNG object created for simulation
int main() { ... }
I’m pretty new to programming. If there’s a dramatically smarter way to do this, I’d love to know.
Update
I’m a moron and didn’t realize I still hadn’t included Rdraws.cpp in my compiler. As a poster noted, I also forgot a semicolon.
I would still appreciate suggestions if the method outlined here could be improved upon.
Which compiler are you using? You need to first compile all of the source files into object files and then link all of the object files together.
Example:
And then to get the executable…