tags:

views:

97

answers:

3

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.

+2  A: 

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:

g++ -c -Wall -O2 main.cpp
g++ -c -Wall -O2 Rdraws.cpp

And then to get the executable...

g++ -s main.o Rdraws.o
Brian R. Bondy
Holy... I'm sorry, I wasn't including Rdraws.cpp in the compile. <very embarrassed> Now dealing with a "Rdraws.cpp error: expected initializer before 'void'."
Sarah
@Sarah: That will be the missing semicolon after `extern RNG rgen`
e.James
@Sarah: That's because you left out a semicolon in the line just before the first `void`. Since it's a variable declaration, the compiler was looking for an initializer or semicolon.
David Thornley
@David and @e.James. Thank you. I'm going for more coffee before I continue programming today.
Sarah
@Brian. I am using g++. I am currently typing g++ main.cpp rng.cpp [... *.cpp] -o stuff.out and then running ./stuff.out. Should I not do this?
Sarah
@Sarah: Yup that's ok.
Brian R. Bondy
@Sarah: I've never understood why compilers don't recognize low caffeine errors. It might be difficult, but think of the problems it would solve!
David Thornley
+1  A: 

Few things:

  • I don't see the variable rgen defined anywhere. Its been declared in the Rdraws.cpp but the definition is missing.
  • To use the global variable and functions you need to separately compile(just compile no linking) main.cpp and Rdraws.cpp into main.o and Rdraws.o and then link them to get the final executable.
codaddict
A: 

It looks like Brian R. Bondy has your answer. I would just like to point out that you do not need those extern function declarations in your main.cpp. The declarations are already made in the Rdraws.h header file, which you include.

e.James
Thanks very much!
Sarah